Subversion Repositories SmartDukaan

Rev

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