Subversion Repositories SmartDukaan

Rev

Rev 16691 | Rev 16713 | 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
16703 manish.sha 76
        app_offer.offer_active = True
16603 manish.sha 77
        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 78
 
16603 manish.sha 79
        if existingAppOffer is None:
16595 manish.sha 80
            app_offer.show = True
16689 manish.sha 81
            app_offer.offer_active = True
16595 manish.sha 82
        else:
16686 manish.sha 83
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 84
                existingAppOffer.show = False
85
                app_offer.show = True
16689 manish.sha 86
                app_offer.offer_active = True
16603 manish.sha 87
            else:
16686 manish.sha 88
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
89
                    existingAppOffer.show = False
90
                    app_offer.show = True
16689 manish.sha 91
                    app_offer.offer_active = True
16686 manish.sha 92
                else:
93
                    existingAppOffer.show = True
94
                    existingAppOffer.offer_active = True
95
                    app_offer.show = False
16603 manish.sha 96
    else:
97
        try:
98
            app_offer.offer_price = float(offer['offerPrice'])
99
        except:
100
            app_offer.offer_price = 0.0
16615 manish.sha 101
        app_offer.user_payout = math.floor(.64 * app_offer.offer_price)
16603 manish.sha 102
        app_offer.description = input_json.get('description')
103
        app_offer.shortDescription = offer.get('shortDesc')
104
        app_offer.longDescription = offer.get('longDesc')
105
        app_offer.link = offer.get('url')
106
        app_offer.offerCategory = offer.get('offerCategory')
107
        app_offer.promoImage = input_json.get('promoImage')
108
        app_offer.ratings = offer.get('appRating')
109
        app_offer.downloads = offer.get('appDownloads')
110
        app_offer.image_url = offer.get('iconUrl')
111
        existingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()
112
        if existingAppOffer is None:
113
            app_offer.show = True
16689 manish.sha 114
            app_offer.offer_active = True
16603 manish.sha 115
        else:
16686 manish.sha 116
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 117
                existingAppOffer.show = False
118
                app_offer.show = True
16689 manish.sha 119
                app_offer.offer_active = True
16595 manish.sha 120
            else:
16686 manish.sha 121
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
122
                    existingAppOffer.show = False
123
                    app_offer.show = True
16689 manish.sha 124
                    app_offer.offer_active = True
16686 manish.sha 125
                else:
126
                    existingAppOffer.show = True
16689 manish.sha 127
                    existingAppOffer.offer_active = True
16686 manish.sha 128
                    app_offer.show = False
16603 manish.sha 129
 
16595 manish.sha 130
    '''            
131
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])
132
 
16537 kshitij.so 133
    if app_offer is None:
134
        app_offer = app_offers()
135
        app_offer.affiliate_id = AFFILIATE_ID
136
        app_offer.affiliate_offer_id = offer['offerId']
137
        try:
138
            app_offer.offer_price = float(offer['offerPrice'])
139
        except:
140
            app_offer.offer_price = 0.0
141
        app_offer.user_payout = .8 * app_offer.offer_price
142
        app_offer.override_payout = False
143
        app_offer.overriden_payout = 0.0
144
        app_offer.app_name = offer.get('appName')
145
        app_offer.package_name = offer.get('packageName')
146
        app_offer.description = input_json.get('description')
147
        app_offer.shortDescription = offer.get('shortDesc')
148
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 149
        app_offer.link = offer.get('url')
16537 kshitij.so 150
        app_offer.offer_active = True
151
        app_offer.priority = 0
152
        app_offer.show = False
153
        app_offer.offerCategory = offer.get('offerCategory')
154
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 155
        app_offer.ratings = offer.get('appRating')
156
        app_offer.downloads = offer.get('appDownloads')
157
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 158
    else:
159
        try:
160
            app_offer.offer_price = float(offer['offerPrice'])
161
        except:
162
            app_offer.offer_price = 0.0
163
        app_offer.user_payout = .8 * app_offer.offer_price
164
        app_offer.description = input_json.get('description')
165
        app_offer.shortDescription = offer.get('shortDesc')
166
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 167
        app_offer.link = offer.get('url')
16537 kshitij.so 168
        app_offer.offerCategory = offer.get('offerCategory')
169
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 170
        app_offer.ratings = offer.get('appRating')
171
        app_offer.downloads = offer.get('appDownloads')
172
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 173
    session.commit()
16595 manish.sha 174
    '''
16685 manish.sha 175
    session.commit()
176
 
16537 kshitij.so 177
def markOfferAsInactive():
16540 kshitij.so 178
    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 179
    for active_offer in all_active_offers:
180
        active_offer.offer_active = False
181
    session.commit()
182
 
183
 
184
 
185
 
186
 
187
def main():
188
    try:
189
        dumpOffers()
190
        markOfferAsInactive()
191
    finally:
192
        session.close()
193
 
194
if __name__ == '__main__':
195
    main()