Subversion Repositories SmartDukaan

Rev

Rev 16689 | Rev 16703 | 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
16613 manish.sha 6
import math
16537 kshitij.so 7
 
8
AFFILIATE_ID = 1
9
DEVICE_ID = "SpiceRetail"
10
RETAILER_CODE = 123
11
ACTIVE_OFFERS = []
12
 
13
DataService.initialize(db_hostname='localhost' )
14
 
15
 
16
def dumpOffers():
16686 manish.sha 17
    global ACTIVE_OFFERS
16537 kshitij.so 18
    offer_url =  AFFILIATE_OFFER_API.get(AFFILIATE_ID)%(DEVICE_ID,RETAILER_CODE)
19
    response = fetchResponseUsingProxy(offer_url, proxy=False)
20
    input_json = json.loads(response)
21
    if input_json['status'].strip()=='ok' and input_json['message'] =="Success":
22
        offers = (input_json['payload'])['offers']
23
        for offer in offers:
16688 manish.sha 24
            ACTIVE_OFFERS.append(str(offer['offerId']))
16691 manish.sha 25
 
26
        print ACTIVE_OFFERS
16689 manish.sha 27
        allExistingOffers = app_offers.query.all()
28
        for existingOffer in allExistingOffers:
29
            existingOffer.offer_active = False
30
            existingOffer.show = False
31
        session.commit()
32
 
16686 manish.sha 33
        for offer in offers:
16537 kshitij.so 34
            fetchOfferDescriptionAndDump(offer)
35
 
36
 
37
def fetchOfferDescriptionAndDump(offer):
38
    offer_desc_url = AFFILIATE_OFFER_DESC_API.get(AFFILIATE_ID)%(DEVICE_ID,offer['offerId'],RETAILER_CODE)
39
    response = fetchResponseUsingProxy(offer_desc_url,proxy=False)
40
    offer_desc = json.loads(response)
41
    input_json = offer_desc['payload']
16600 manish.sha 42
    app_master = appmasters.get_by(package_name=offer['packageName'])
16595 manish.sha 43
    if app_master is None:
44
        app_master = appmasters()
45
        app_master.app_name = offer.get('appName')
46
        app_master.package_name = offer.get('packageName')
16598 manish.sha 47
        app_master.os_name = 'ANDROID'
16595 manish.sha 48
    session.commit()
49
    appMasterId = app_master.id
16603 manish.sha 50
    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()
51
 
52
    if app_offer is None:
53
        app_offer = app_offers()
54
        app_offer.affiliate_id = AFFILIATE_ID
55
        app_offer.affiliate_offer_id = offer['offerId']
56
        try:
16595 manish.sha 57
            app_offer.offer_price = float(offer['offerPrice'])
16603 manish.sha 58
        except:
59
            app_offer.offer_price = 0.0
16615 manish.sha 60
        app_offer.user_payout = math.floor(.64 * app_offer.offer_price)
16603 manish.sha 61
        app_offer.override_payout = False
62
        app_offer.overriden_payout = 0.0
63
        app_offer.app_name = offer.get('appName')
64
        app_offer.package_name = offer.get('packageName')
65
        app_offer.description = input_json.get('description')
66
        app_offer.shortDescription = offer.get('shortDesc')
67
        app_offer.longDescription = offer.get('longDesc')
68
        app_offer.link = offer.get('url')
69
        app_offer.priority = 0
70
        app_offer.offerCategory = offer.get('offerCategory')
71
        app_offer.promoImage = input_json.get('promoImage')
72
        app_offer.ratings = offer.get('appRating')
73
        app_offer.downloads = offer.get('appDownloads')
74
        app_offer.image_url = offer.get('iconUrl')
75
        app_offer.appmaster_id = appMasterId
76
        existingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()
16686 manish.sha 77
 
16603 manish.sha 78
        if existingAppOffer is None:
16595 manish.sha 79
            app_offer.show = True
16689 manish.sha 80
            app_offer.offer_active = True
16595 manish.sha 81
        else:
16686 manish.sha 82
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 83
                existingAppOffer.show = False
84
                app_offer.show = True
16689 manish.sha 85
                app_offer.offer_active = True
16603 manish.sha 86
            else:
16686 manish.sha 87
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
88
                    existingAppOffer.show = False
89
                    app_offer.show = True
16689 manish.sha 90
                    app_offer.offer_active = True
16686 manish.sha 91
                else:
92
                    existingAppOffer.show = True
93
                    existingAppOffer.offer_active = True
94
                    app_offer.show = False
16603 manish.sha 95
    else:
96
        try:
97
            app_offer.offer_price = float(offer['offerPrice'])
98
        except:
99
            app_offer.offer_price = 0.0
16615 manish.sha 100
        app_offer.user_payout = math.floor(.64 * app_offer.offer_price)
16603 manish.sha 101
        app_offer.description = input_json.get('description')
102
        app_offer.shortDescription = offer.get('shortDesc')
103
        app_offer.longDescription = offer.get('longDesc')
104
        app_offer.link = offer.get('url')
105
        app_offer.offerCategory = offer.get('offerCategory')
106
        app_offer.promoImage = input_json.get('promoImage')
107
        app_offer.ratings = offer.get('appRating')
108
        app_offer.downloads = offer.get('appDownloads')
109
        app_offer.image_url = offer.get('iconUrl')
110
        existingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()
111
        if existingAppOffer is None:
112
            app_offer.show = True
16689 manish.sha 113
            app_offer.offer_active = True
16603 manish.sha 114
        else:
16686 manish.sha 115
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 116
                existingAppOffer.show = False
117
                app_offer.show = True
16689 manish.sha 118
                app_offer.offer_active = True
16595 manish.sha 119
            else:
16686 manish.sha 120
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
121
                    existingAppOffer.show = False
122
                    app_offer.show = True
16689 manish.sha 123
                    app_offer.offer_active = True
16686 manish.sha 124
                else:
125
                    existingAppOffer.show = True
16689 manish.sha 126
                    existingAppOffer.offer_active = True
16686 manish.sha 127
                    app_offer.show = False
16603 manish.sha 128
 
16595 manish.sha 129
    '''            
130
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])
131
 
16537 kshitij.so 132
    if app_offer is None:
133
        app_offer = app_offers()
134
        app_offer.affiliate_id = AFFILIATE_ID
135
        app_offer.affiliate_offer_id = offer['offerId']
136
        try:
137
            app_offer.offer_price = float(offer['offerPrice'])
138
        except:
139
            app_offer.offer_price = 0.0
140
        app_offer.user_payout = .8 * app_offer.offer_price
141
        app_offer.override_payout = False
142
        app_offer.overriden_payout = 0.0
143
        app_offer.app_name = offer.get('appName')
144
        app_offer.package_name = offer.get('packageName')
145
        app_offer.description = input_json.get('description')
146
        app_offer.shortDescription = offer.get('shortDesc')
147
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 148
        app_offer.link = offer.get('url')
16537 kshitij.so 149
        app_offer.offer_active = True
150
        app_offer.priority = 0
151
        app_offer.show = False
152
        app_offer.offerCategory = offer.get('offerCategory')
153
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 154
        app_offer.ratings = offer.get('appRating')
155
        app_offer.downloads = offer.get('appDownloads')
156
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 157
    else:
158
        try:
159
            app_offer.offer_price = float(offer['offerPrice'])
160
        except:
161
            app_offer.offer_price = 0.0
162
        app_offer.user_payout = .8 * app_offer.offer_price
163
        app_offer.description = input_json.get('description')
164
        app_offer.shortDescription = offer.get('shortDesc')
165
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 166
        app_offer.link = offer.get('url')
16537 kshitij.so 167
        app_offer.offerCategory = offer.get('offerCategory')
168
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 169
        app_offer.ratings = offer.get('appRating')
170
        app_offer.downloads = offer.get('appDownloads')
171
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 172
    session.commit()
16595 manish.sha 173
    '''
16685 manish.sha 174
    session.commit()
175
 
16537 kshitij.so 176
def markOfferAsInactive():
16540 kshitij.so 177
    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 178
    for active_offer in all_active_offers:
179
        active_offer.offer_active = False
180
    session.commit()
181
 
182
 
183
 
184
 
185
 
186
def main():
187
    try:
188
        dumpOffers()
189
        markOfferAsInactive()
190
    finally:
191
        session.close()
192
 
193
if __name__ == '__main__':
194
    main()