Subversion Repositories SmartDukaan

Rev

Rev 16599 | Rev 16603 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16537 kshitij.so 1
from elixir import *
2
from dtr.storage import DataService
16595 manish.sha 3
from dtr.storage.DataService import app_offers, app_affiliates, appmasters
16537 kshitij.so 4
from dtr.utils.utils import AFFILIATE_OFFER_API, AFFILIATE_OFFER_DESC_API, fetchResponseUsingProxy
5
import json
6
 
7
AFFILIATE_ID = 1
8
DEVICE_ID = "SpiceRetail"
9
RETAILER_CODE = 123
10
ACTIVE_OFFERS = []
11
 
12
DataService.initialize(db_hostname='localhost' )
13
 
14
 
15
def dumpOffers():
16
    offer_url =  AFFILIATE_OFFER_API.get(AFFILIATE_ID)%(DEVICE_ID,RETAILER_CODE)
17
    response = fetchResponseUsingProxy(offer_url, proxy=False)
18
    input_json = json.loads(response)
19
    if input_json['status'].strip()=='ok' and input_json['message'] =="Success":
20
        offers = (input_json['payload'])['offers']
21
        for offer in offers:
22
            fetchOfferDescriptionAndDump(offer)
23
 
24
 
25
def fetchOfferDescriptionAndDump(offer):
26
    global ACTIVE_OFFERS
27
    offer_desc_url = AFFILIATE_OFFER_DESC_API.get(AFFILIATE_ID)%(DEVICE_ID,offer['offerId'],RETAILER_CODE)
28
    response = fetchResponseUsingProxy(offer_desc_url,proxy=False)
29
    offer_desc = json.loads(response)
30
    input_json = offer_desc['payload']
16600 manish.sha 31
    app_master = appmasters.get_by(package_name=offer['packageName'])
16595 manish.sha 32
    if app_master is None:
33
        app_master = appmasters()
34
        app_master.app_name = offer.get('appName')
35
        app_master.package_name = offer.get('packageName')
16598 manish.sha 36
        app_master.os_name = 'ANDROID'
16595 manish.sha 37
    session.commit()
38
    appMasterId = app_master.id
16600 manish.sha 39
    single_app_offers = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).all()
16595 manish.sha 40
    ACTIVE_OFFERS.append(offer['offerId']) 
41
    for app_offer in single_app_offers:
42
        if app_offer.affiliate_offer_id == offer['offerId']:
43
            app_offer.offer_price = float(offer['offerPrice'])
44
            app_offer.show = True
16599 manish.sha 45
            app_offer.user_payout = round(.64 * app_offer.offer_price,0)
16595 manish.sha 46
            app_offer.description = input_json.get('description')
47
            app_offer.shortDescription = offer.get('shortDesc')
48
            app_offer.longDescription = offer.get('longDesc')
49
            app_offer.link = offer.get('url')
50
            app_offer.offerCategory = offer.get('offerCategory')
51
            app_offer.promoImage = input_json.get('promoImage')
52
            app_offer.ratings = offer.get('appRating')
53
            app_offer.downloads = offer.get('appDownloads')
54
            app_offer.image_url = offer.get('iconUrl')
55
            if not app_offer.appmaster_id:
56
                app_offer.appmaster_id = appMasterId
57
        else:
58
            app_offerObj = app_offers()
59
            app_offerObj.affiliate_id = AFFILIATE_ID
60
            app_offerObj.affiliate_offer_id = offer['offerId']
61
            try:
62
                app_offerObj.offer_price = float(offer['offerPrice'])
63
            except:
64
                app_offerObj.offer_price = 0.0
16599 manish.sha 65
            app_offerObj.user_payout = round(.64 * app_offer.offer_price,0)
16595 manish.sha 66
            app_offerObj.override_payout = False
67
            app_offerObj.overriden_payout = 0.0
68
            app_offerObj.app_name = offer.get('appName')
69
            app_offerObj.package_name = offer.get('packageName')
70
            app_offerObj.description = input_json.get('description')
71
            app_offerObj.shortDescription = offer.get('shortDesc')
72
            app_offerObj.longDescription = offer.get('longDesc')
73
            app_offerObj.link = offer.get('url')
74
            app_offerObj.offer_active = True
75
            app_offerObj.priority = 0
76
            app_offerObj.offerCategory = offer.get('offerCategory')
77
            app_offerObj.promoImage = input_json.get('promoImage')
78
            app_offerObj.ratings = offer.get('appRating')
79
            app_offerObj.downloads = offer.get('appDownloads')
80
            app_offerObj.image_url = offer.get('iconUrl')
81
            app_offerObj.appmaster_id = appMasterId
82
            if app_offer.offer_price < float(offer['offerPrice']):
83
                app_offer.show = False
84
                if not app_offer.appmaster_id:
85
                    app_offer.appmaster_id = appMasterId
86
                app_offerObj.show = True
87
            else:
88
                app_offerObj.show = False
89
    session.commit()
90
    '''            
91
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])
92
 
16537 kshitij.so 93
    if app_offer is None:
94
        app_offer = app_offers()
95
        app_offer.affiliate_id = AFFILIATE_ID
96
        app_offer.affiliate_offer_id = offer['offerId']
97
        try:
98
            app_offer.offer_price = float(offer['offerPrice'])
99
        except:
100
            app_offer.offer_price = 0.0
101
        app_offer.user_payout = .8 * app_offer.offer_price
102
        app_offer.override_payout = False
103
        app_offer.overriden_payout = 0.0
104
        app_offer.app_name = offer.get('appName')
105
        app_offer.package_name = offer.get('packageName')
106
        app_offer.description = input_json.get('description')
107
        app_offer.shortDescription = offer.get('shortDesc')
108
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 109
        app_offer.link = offer.get('url')
16537 kshitij.so 110
        app_offer.offer_active = True
111
        app_offer.priority = 0
112
        app_offer.show = False
113
        app_offer.offerCategory = offer.get('offerCategory')
114
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 115
        app_offer.ratings = offer.get('appRating')
116
        app_offer.downloads = offer.get('appDownloads')
117
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 118
    else:
119
        try:
120
            app_offer.offer_price = float(offer['offerPrice'])
121
        except:
122
            app_offer.offer_price = 0.0
123
        app_offer.user_payout = .8 * app_offer.offer_price
124
        app_offer.description = input_json.get('description')
125
        app_offer.shortDescription = offer.get('shortDesc')
126
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 127
        app_offer.link = offer.get('url')
16537 kshitij.so 128
        app_offer.offerCategory = offer.get('offerCategory')
129
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 130
        app_offer.ratings = offer.get('appRating')
131
        app_offer.downloads = offer.get('appDownloads')
132
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 133
    session.commit()
16595 manish.sha 134
    '''
16537 kshitij.so 135
 
136
def markOfferAsInactive():
16540 kshitij.so 137
    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()
16537 kshitij.so 138
    for active_offer in all_active_offers:
139
        active_offer.offer_active = False
140
    session.commit()
141
 
142
 
143
 
144
 
145
 
146
def main():
147
    try:
148
        dumpOffers()
149
        markOfferAsInactive()
150
    finally:
151
        session.close()
152
 
153
if __name__ == '__main__':
154
    main()