Subversion Repositories SmartDukaan

Rev

Rev 16904 | Rev 16941 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

from elixir import *
from dtr.storage import DataService
from dtr.storage.DataService import app_offers, app_affiliates, appmasters
from dtr.utils.utils import AFFILIATE_OFFER_API, AFFILIATE_OFFER_DESC_API, fetchResponseUsingProxy
import json
import math
from dtr.utils.MailSender import Email
import traceback
from dtr.storage import Mongo

AFFILIATE_ID = 1
DEVICE_ID = "SpiceRetail"
RETAILER_CODE = 123
ACTIVE_OFFERS = []
GOT_RESPONSE = False

DataService.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_OFFERS
    global GOT_RESPONSE
    offer_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 = True
        offers = (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_OFFERS
                
            for 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'
        try:
            _sendAlertForNewApp(offer.get('packageName'), offer.get('appName'))
        except:
            print traceback.print_exc()
            
    session.commit()
    appMasterId = app_master.id
    app_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_ID
        app_offer.affiliate_offer_id = offer['offerId']
        try:
            app_offer.offer_price = float(offer['offerPrice'])
        except:
            app_offer.offer_price = 0.0
        app_offer.user_payout = math.floor(.64 * app_offer.offer_price)
        app_offer.override_payout = False
        app_offer.overriden_payout = 0.0
        app_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 = 0
        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.appmaster_id = appMasterId
        app_offer.offer_active = True
        locations = str(offer.get('location')).split(',')
        app_offer.location = ''
        for location in locations:
            if app_offer.location =='':
                app_offer.location = location
            else:
                app_offer.location = app_offer.location +', '+ location
        existingAppOffer = 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 = True
        else:
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
                existingAppOffer.show = False
                app_offer.show = True
            else:
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
                    existingAppOffer.show = False
                    app_offer.show = True
                else:
                    existingAppOffer.show = True
                    existingAppOffer.offer_active = True
                    app_offer.show = False
    else:
        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.0
        app_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 = True
        locations = str(offer.get('location')).split(',')
        app_offer.location = ''
        for location in locations:
            if app_offer.location =='':
                app_offer.location = location
            else:
                app_offer.location = app_offer.location +', '+ location
        existingAppOffer = 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 = True
        else:
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
                existingAppOffer.show = False
                app_offer.show = True
            else:
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
                    existingAppOffer.show = False
                    app_offer.show = True
                else:
                    existingAppOffer.show = True
                    existingAppOffer.offer_active = True
                    app_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_ID
        app_offer.affiliate_offer_id = offer['offerId']
        try:
            app_offer.offer_price = float(offer['offerPrice'])
        except:
            app_offer.offer_price = 0.0
        app_offer.user_payout = .8 * app_offer.offer_price
        app_offer.override_payout = False
        app_offer.overriden_payout = 0.0
        app_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 = True
        app_offer.priority = 0
        app_offer.show = False
        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')
    else:
        try:
            app_offer.offer_price = float(offer['offerPrice'])
        except:
            app_offer.offer_price = 0.0
        app_offer.user_payout = .8 * app_offer.offer_price
        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')
    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 = False
    session.commit()
        
         
        
    

def main():
    try:
        dumpOffers()
        print 'GOT_RESPONSE', GOT_RESPONSE
        if GOT_RESPONSE:
            markOfferAsInactive()
            Mongo.populateAppOffers(AFFILIATE_ID)
    finally:
        session.close()

if __name__ == '__main__':
    main()