Subversion Repositories SmartDukaan

Rev

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