Subversion Repositories SmartDukaan

Rev

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