Subversion Repositories SmartDukaan

Rev

Rev 16578 | Rev 16598 | 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']
16595 manish.sha 31
    app_master = appmasters.get_by(package_name=offer['package_name'])
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')
36
    session.commit()
37
    appMasterId = app_master.id
38
    single_app_offers = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['package_name']).all()
39
    ACTIVE_OFFERS.append(offer['offerId']) 
40
    for app_offer in single_app_offers:
41
        if app_offer.affiliate_offer_id == offer['offerId']:
42
            app_offer.offer_price = float(offer['offerPrice'])
43
            app_offer.show = True
44
            app_offer.user_payout = .8 * app_offer.offer_price
45
            app_offer.description = input_json.get('description')
46
            app_offer.shortDescription = offer.get('shortDesc')
47
            app_offer.longDescription = offer.get('longDesc')
48
            app_offer.link = offer.get('url')
49
            app_offer.offerCategory = offer.get('offerCategory')
50
            app_offer.promoImage = input_json.get('promoImage')
51
            app_offer.ratings = offer.get('appRating')
52
            app_offer.downloads = offer.get('appDownloads')
53
            app_offer.image_url = offer.get('iconUrl')
54
            if not app_offer.appmaster_id:
55
                app_offer.appmaster_id = appMasterId
56
        else:
57
            app_offerObj = app_offers()
58
            app_offerObj.affiliate_id = AFFILIATE_ID
59
            app_offerObj.affiliate_offer_id = offer['offerId']
60
            try:
61
                app_offerObj.offer_price = float(offer['offerPrice'])
62
            except:
63
                app_offerObj.offer_price = 0.0
64
            app_offerObj.user_payout = .8 * app_offer.offer_price
65
            app_offerObj.override_payout = False
66
            app_offerObj.overriden_payout = 0.0
67
            app_offerObj.app_name = offer.get('appName')
68
            app_offerObj.package_name = offer.get('packageName')
69
            app_offerObj.description = input_json.get('description')
70
            app_offerObj.shortDescription = offer.get('shortDesc')
71
            app_offerObj.longDescription = offer.get('longDesc')
72
            app_offerObj.link = offer.get('url')
73
            app_offerObj.offer_active = True
74
            app_offerObj.priority = 0
75
            app_offerObj.offerCategory = offer.get('offerCategory')
76
            app_offerObj.promoImage = input_json.get('promoImage')
77
            app_offerObj.ratings = offer.get('appRating')
78
            app_offerObj.downloads = offer.get('appDownloads')
79
            app_offerObj.image_url = offer.get('iconUrl')
80
            app_offerObj.appmaster_id = appMasterId
81
            if app_offer.offer_price < float(offer['offerPrice']):
82
                app_offer.show = False
83
                if not app_offer.appmaster_id:
84
                    app_offer.appmaster_id = appMasterId
85
                app_offerObj.show = True
86
            else:
87
                app_offerObj.show = False
88
    session.commit()
89
    '''            
90
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])
91
 
16537 kshitij.so 92
    if app_offer is None:
93
        app_offer = app_offers()
94
        app_offer.affiliate_id = AFFILIATE_ID
95
        app_offer.affiliate_offer_id = offer['offerId']
96
        try:
97
            app_offer.offer_price = float(offer['offerPrice'])
98
        except:
99
            app_offer.offer_price = 0.0
100
        app_offer.user_payout = .8 * app_offer.offer_price
101
        app_offer.override_payout = False
102
        app_offer.overriden_payout = 0.0
103
        app_offer.app_name = offer.get('appName')
104
        app_offer.package_name = offer.get('packageName')
105
        app_offer.description = input_json.get('description')
106
        app_offer.shortDescription = offer.get('shortDesc')
107
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 108
        app_offer.link = offer.get('url')
16537 kshitij.so 109
        app_offer.offer_active = True
110
        app_offer.priority = 0
111
        app_offer.show = False
112
        app_offer.offerCategory = offer.get('offerCategory')
113
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 114
        app_offer.ratings = offer.get('appRating')
115
        app_offer.downloads = offer.get('appDownloads')
116
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 117
    else:
118
        try:
119
            app_offer.offer_price = float(offer['offerPrice'])
120
        except:
121
            app_offer.offer_price = 0.0
122
        app_offer.user_payout = .8 * app_offer.offer_price
123
        app_offer.description = input_json.get('description')
124
        app_offer.shortDescription = offer.get('shortDesc')
125
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 126
        app_offer.link = offer.get('url')
16537 kshitij.so 127
        app_offer.offerCategory = offer.get('offerCategory')
128
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 129
        app_offer.ratings = offer.get('appRating')
130
        app_offer.downloads = offer.get('appDownloads')
131
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 132
    session.commit()
16595 manish.sha 133
    '''
16537 kshitij.so 134
 
135
def markOfferAsInactive():
16540 kshitij.so 136
    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 137
    for active_offer in all_active_offers:
138
        active_offer.offer_active = False
139
    session.commit()
140
 
141
 
142
 
143
 
144
 
145
def main():
146
    try:
147
        dumpOffers()
148
        markOfferAsInactive()
149
    finally:
150
        session.close()
151
 
152
if __name__ == '__main__':
153
    main()