Subversion Repositories SmartDukaan

Rev

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