Subversion Repositories SmartDukaan

Rev

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