Subversion Repositories SmartDukaan

Rev

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