Rev 16906 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from dtr.storage import DataServicefrom dtr.storage.DataService import app_offers, app_affiliates, appmastersfrom dtr.utils.utils import AFFILIATE_OFFER_API, AFFILIATE_OFFER_DESC_API, fetchResponseUsingProxyimport jsonimport mathfrom dtr.utils.MailSender import Emailimport tracebackfrom dtr.storage import MongoAFFILIATE_ID = 1DEVICE_ID = "SpiceRetail"RETAILER_CODE = 123ACTIVE_OFFERS = []GOT_RESPONSE = FalseDataService.initialize(db_hostname='localhost' )def _sendAlertForNewApp(packageName, appName):m = Email('localhost')mFrom = "dtr@shop2020.in"m.setFrom(mFrom)mTo = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com','manish.sharma@shop2020.in','yatin.singh@saholic.com']#mTo = ['manish.sharma@shop2020.in']for receipient in mTo:m.addRecipient(receipient)m.setSubject("New App have been found:- Package Name:-"+ packageName+" App Name:- "+appName)m.setTextBody("Please check App Master Data")m.send()def dumpOffers():global ACTIVE_OFFERSglobal GOT_RESPONSEoffer_url = AFFILIATE_OFFER_API.get(AFFILIATE_ID)%(DEVICE_ID,RETAILER_CODE)response = fetchResponseUsingProxy(offer_url, proxy=False)input_json = json.loads(response)if input_json['status'].strip()=='ok' and input_json['message'] =="Success":GOT_RESPONSE = Trueoffers = (input_json['payload'])['offers']if offers is not None and len(offers) >0:print 'Offers as Given in Spice Api... '+ str(offers)for offer in offers:ACTIVE_OFFERS.append(str(offer['offerId']))print ACTIVE_OFFERSfor offer in offers:fetchOfferDescriptionAndDump(offer)def fetchOfferDescriptionAndDump(offer):offer_desc_url = AFFILIATE_OFFER_DESC_API.get(AFFILIATE_ID)%(DEVICE_ID,offer['offerId'],RETAILER_CODE)response = fetchResponseUsingProxy(offer_desc_url,proxy=False)offer_desc = json.loads(response)input_json = offer_desc['payload']app_master = appmasters.get_by(package_name=offer['packageName'])if app_master is None:app_master = appmasters()app_master.app_name = offer.get('appName')app_master.package_name = offer.get('packageName')app_master.os_name = 'ANDROID'app_master.rank = 0try:_sendAlertForNewApp(offer.get('packageName'), offer.get('appName'))except:print traceback.print_exc()session.commit()appMasterId = app_master.idapp_offer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.affiliate_offer_id==offer['offerId']).filter(app_offers.package_name==offer['packageName']).first()if app_offer is None:app_offer = app_offers()app_offer.affiliate_id = AFFILIATE_IDapp_offer.affiliate_offer_id = offer['offerId']try:app_offer.offer_price = float(offer['offerPrice'])except:app_offer.offer_price = 0.0app_offer.user_payout = math.floor(.64 * app_offer.offer_price)app_offer.override_payout = Falseapp_offer.overriden_payout = 0.0app_offer.app_name = offer.get('appName')app_offer.package_name = offer.get('packageName')app_offer.description = input_json.get('description')app_offer.shortDescription = offer.get('shortDesc')app_offer.longDescription = offer.get('longDesc')app_offer.link = offer.get('url')app_offer.priority = 0app_offer.offerCategory = offer.get('offerCategory')app_offer.promoImage = input_json.get('promoImage')app_offer.ratings = offer.get('appRating')app_offer.downloads = offer.get('appDownloads')app_offer.image_url = offer.get('iconUrl')app_offer.appmaster_id = appMasterIdapp_offer.offer_active = Truelocations = str(offer.get('location')).split(',')app_offer.location = ''for location in locations:if app_offer.location =='':app_offer.location = locationelse:app_offer.location = app_offer.location +', '+ locationexistingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()if existingAppOffer is None:app_offer.show = Trueelse:if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:existingAppOffer.show = Falseapp_offer.show = Trueelse:if existingAppOffer.offer_price <= float(offer['offerPrice']):existingAppOffer.show = Falseapp_offer.show = Trueelse:existingAppOffer.show = TrueexistingAppOffer.offer_active = Trueapp_offer.show = Falseelse:try:offerPrice = float(offer['offerPrice'])if app_offer.override_payout:ratio = float(app_offer.overriden_payout)/float(app_offer.offer_price)app_offer.overriden_payout = math.floor(ratio*float(offer['offerPrice']))app_offer.offer_price = float(offer['offerPrice'])except:app_offer.offer_price = 0.0app_offer.user_payout = math.floor(.64 * float(offer['offerPrice']))app_offer.description = input_json.get('description')app_offer.shortDescription = offer.get('shortDesc')app_offer.longDescription = offer.get('longDesc')app_offer.link = offer.get('url')app_offer.offerCategory = offer.get('offerCategory')app_offer.promoImage = input_json.get('promoImage')app_offer.ratings = offer.get('appRating')app_offer.downloads = offer.get('appDownloads')app_offer.image_url = offer.get('iconUrl')app_offer.offer_active = Truelocations = str(offer.get('location')).split(',')app_offer.location = ''for location in locations:if app_offer.location =='':app_offer.location = locationelse:app_offer.location = app_offer.location +', '+ locationexistingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()if existingAppOffer is None:app_offer.show = Trueelse:if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:existingAppOffer.show = Falseapp_offer.show = Trueelse:if existingAppOffer.offer_price <= float(offer['offerPrice']):existingAppOffer.show = Falseapp_offer.show = Trueelse:existingAppOffer.show = TrueexistingAppOffer.offer_active = Trueapp_offer.show = False'''app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])if app_offer is None:app_offer = app_offers()app_offer.affiliate_id = AFFILIATE_IDapp_offer.affiliate_offer_id = offer['offerId']try:app_offer.offer_price = float(offer['offerPrice'])except:app_offer.offer_price = 0.0app_offer.user_payout = .8 * app_offer.offer_priceapp_offer.override_payout = Falseapp_offer.overriden_payout = 0.0app_offer.app_name = offer.get('appName')app_offer.package_name = offer.get('packageName')app_offer.description = input_json.get('description')app_offer.shortDescription = offer.get('shortDesc')app_offer.longDescription = offer.get('longDesc')app_offer.link = offer.get('url')app_offer.offer_active = Trueapp_offer.priority = 0app_offer.show = Falseapp_offer.offerCategory = offer.get('offerCategory')app_offer.promoImage = input_json.get('promoImage')app_offer.ratings = offer.get('appRating')app_offer.downloads = offer.get('appDownloads')app_offer.image_url = offer.get('iconUrl')else:try:app_offer.offer_price = float(offer['offerPrice'])except:app_offer.offer_price = 0.0app_offer.user_payout = .8 * app_offer.offer_priceapp_offer.description = input_json.get('description')app_offer.shortDescription = offer.get('shortDesc')app_offer.longDescription = offer.get('longDesc')app_offer.link = offer.get('url')app_offer.offerCategory = offer.get('offerCategory')app_offer.promoImage = input_json.get('promoImage')app_offer.ratings = offer.get('appRating')app_offer.downloads = offer.get('appDownloads')app_offer.image_url = offer.get('iconUrl')session.commit()'''session.commit()def markOfferAsInactive():all_active_offers = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(~app_offers.affiliate_offer_id.in_(ACTIVE_OFFERS)).filter(app_offers.offer_active==True).all()for active_offer in all_active_offers:active_offer.offer_active = Falsesession.commit()def main():try:dumpOffers()print 'GOT_RESPONSE', GOT_RESPONSEif GOT_RESPONSE:markOfferAsInactive()Mongo.populateAppOffers(AFFILIATE_ID)finally:session.close()if __name__ == '__main__':main()