Subversion Repositories SmartDukaan

Rev

Rev 16941 | 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
16713 manish.sha 7
from dtr.utils.MailSender import Email
8
import traceback
16896 manish.sha 9
from dtr.storage import Mongo
16537 kshitij.so 10
 
11
AFFILIATE_ID = 1
12
DEVICE_ID = "SpiceRetail"
13
RETAILER_CODE = 123
14
ACTIVE_OFFERS = []
16735 manish.sha 15
GOT_RESPONSE = False
16537 kshitij.so 16
 
17
DataService.initialize(db_hostname='localhost' )
18
 
16713 manish.sha 19
def _sendAlertForNewApp(packageName, appName):
20
    m = Email('localhost')
21
    mFrom = "dtr@shop2020.in"
22
    m.setFrom(mFrom)
20172 aman.kumar 23
    mTo = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','ritesh.chauhan@saholic.com','khushal.bhatia@saholic.com','yatin.singh@saholic.com']
16744 manish.sha 24
    #mTo = ['manish.sharma@shop2020.in']
16713 manish.sha 25
    for receipient in mTo:
26
        m.addRecipient(receipient)
27
 
28
    m.setSubject("New App have been found:- Package Name:-"+ packageName+" App Name:- "+appName)
29
 
16738 manish.sha 30
    m.setTextBody("Please check App Master Data")
16713 manish.sha 31
 
32
    m.send()
16537 kshitij.so 33
 
16713 manish.sha 34
 
16537 kshitij.so 35
def dumpOffers():
16686 manish.sha 36
    global ACTIVE_OFFERS
16735 manish.sha 37
    global GOT_RESPONSE
16537 kshitij.so 38
    offer_url =  AFFILIATE_OFFER_API.get(AFFILIATE_ID)%(DEVICE_ID,RETAILER_CODE)
39
    response = fetchResponseUsingProxy(offer_url, proxy=False)
40
    input_json = json.loads(response)
41
    if input_json['status'].strip()=='ok' and input_json['message'] =="Success":
16735 manish.sha 42
        GOT_RESPONSE = True
16537 kshitij.so 43
        offers = (input_json['payload'])['offers']
16734 manish.sha 44
        if offers is not None and len(offers) >0:
16746 manish.sha 45
            print 'Offers as Given in Spice Api... '+ str(offers)
16734 manish.sha 46
            for offer in offers:
47
                ACTIVE_OFFERS.append(str(offer['offerId']))
16689 manish.sha 48
 
16734 manish.sha 49
            print ACTIVE_OFFERS
50
 
51
            for offer in offers:
52
                fetchOfferDescriptionAndDump(offer)
16537 kshitij.so 53
 
54
 
55
def fetchOfferDescriptionAndDump(offer):
56
    offer_desc_url = AFFILIATE_OFFER_DESC_API.get(AFFILIATE_ID)%(DEVICE_ID,offer['offerId'],RETAILER_CODE)
57
    response = fetchResponseUsingProxy(offer_desc_url,proxy=False)
58
    offer_desc = json.loads(response)
59
    input_json = offer_desc['payload']
16600 manish.sha 60
    app_master = appmasters.get_by(package_name=offer['packageName'])
16595 manish.sha 61
    if app_master is None:
62
        app_master = appmasters()
63
        app_master.app_name = offer.get('appName')
64
        app_master.package_name = offer.get('packageName')
16598 manish.sha 65
        app_master.os_name = 'ANDROID'
16941 manish.sha 66
        app_master.rank = 0
16713 manish.sha 67
        try:
68
            _sendAlertForNewApp(offer.get('packageName'), offer.get('appName'))
69
        except:
70
            print traceback.print_exc()
71
 
16595 manish.sha 72
    session.commit()
73
    appMasterId = app_master.id
16603 manish.sha 74
    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()
75
 
76
    if app_offer is None:
77
        app_offer = app_offers()
78
        app_offer.affiliate_id = AFFILIATE_ID
79
        app_offer.affiliate_offer_id = offer['offerId']
80
        try:
16595 manish.sha 81
            app_offer.offer_price = float(offer['offerPrice'])
16603 manish.sha 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.override_payout = False
86
        app_offer.overriden_payout = 0.0
87
        app_offer.app_name = offer.get('appName')
88
        app_offer.package_name = offer.get('packageName')
89
        app_offer.description = input_json.get('description')
90
        app_offer.shortDescription = offer.get('shortDesc')
91
        app_offer.longDescription = offer.get('longDesc')
92
        app_offer.link = offer.get('url')
93
        app_offer.priority = 0
94
        app_offer.offerCategory = offer.get('offerCategory')
95
        app_offer.promoImage = input_json.get('promoImage')
96
        app_offer.ratings = offer.get('appRating')
97
        app_offer.downloads = offer.get('appDownloads')
98
        app_offer.image_url = offer.get('iconUrl')
99
        app_offer.appmaster_id = appMasterId
16703 manish.sha 100
        app_offer.offer_active = True
16844 manish.sha 101
        locations = str(offer.get('location')).split(',')
102
        app_offer.location = ''
103
        for location in locations:
16846 manish.sha 104
            if app_offer.location =='':
105
                app_offer.location = location
106
            else:
107
                app_offer.location = app_offer.location +', '+ location
16603 manish.sha 108
        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 109
 
16603 manish.sha 110
        if existingAppOffer is None:
16595 manish.sha 111
            app_offer.show = True
112
        else:
16686 manish.sha 113
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 114
                existingAppOffer.show = False
115
                app_offer.show = True
116
            else:
16686 manish.sha 117
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
118
                    existingAppOffer.show = False
119
                    app_offer.show = True
120
                else:
121
                    existingAppOffer.show = True
122
                    existingAppOffer.offer_active = True
123
                    app_offer.show = False
16603 manish.sha 124
    else:
125
        try:
16742 manish.sha 126
            offerPrice = float(offer['offerPrice'])
127
            if app_offer.override_payout:
128
                ratio = float(app_offer.overriden_payout)/float(app_offer.offer_price)
129
                app_offer.overriden_payout = math.floor(ratio*float(offer['offerPrice']))
130
 
16603 manish.sha 131
            app_offer.offer_price = float(offer['offerPrice'])
132
        except:
133
            app_offer.offer_price = 0.0
16742 manish.sha 134
        app_offer.user_payout = math.floor(.64 * float(offer['offerPrice']))
16603 manish.sha 135
        app_offer.description = input_json.get('description')
136
        app_offer.shortDescription = offer.get('shortDesc')
137
        app_offer.longDescription = offer.get('longDesc')
138
        app_offer.link = offer.get('url')
139
        app_offer.offerCategory = offer.get('offerCategory')
140
        app_offer.promoImage = input_json.get('promoImage')
141
        app_offer.ratings = offer.get('appRating')
142
        app_offer.downloads = offer.get('appDownloads')
143
        app_offer.image_url = offer.get('iconUrl')
16713 manish.sha 144
        app_offer.offer_active = True
16844 manish.sha 145
        locations = str(offer.get('location')).split(',')
146
        app_offer.location = ''
147
        for location in locations:
16846 manish.sha 148
            if app_offer.location =='':
149
                app_offer.location = location
150
            else:
151
                app_offer.location = app_offer.location +', '+ location
16603 manish.sha 152
        existingAppOffer = app_offers.query.filter(app_offers.affiliate_id==AFFILIATE_ID).filter(app_offers.package_name==offer['packageName']).filter(app_offers.show==True).first()
153
        if existingAppOffer is None:
154
            app_offer.show = True
155
        else:
16686 manish.sha 156
            if existingAppOffer.affiliate_offer_id not in ACTIVE_OFFERS:
16603 manish.sha 157
                existingAppOffer.show = False
158
                app_offer.show = True
16595 manish.sha 159
            else:
16686 manish.sha 160
                if existingAppOffer.offer_price <= float(offer['offerPrice']):
161
                    existingAppOffer.show = False
162
                    app_offer.show = True
163
                else:
164
                    existingAppOffer.show = True
16689 manish.sha 165
                    existingAppOffer.offer_active = True
16686 manish.sha 166
                    app_offer.show = False
16603 manish.sha 167
 
16595 manish.sha 168
    '''            
169
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'], package_name=offer['package_name'])
170
 
16537 kshitij.so 171
    if app_offer is None:
172
        app_offer = app_offers()
173
        app_offer.affiliate_id = AFFILIATE_ID
174
        app_offer.affiliate_offer_id = offer['offerId']
175
        try:
176
            app_offer.offer_price = float(offer['offerPrice'])
177
        except:
178
            app_offer.offer_price = 0.0
179
        app_offer.user_payout = .8 * app_offer.offer_price
180
        app_offer.override_payout = False
181
        app_offer.overriden_payout = 0.0
182
        app_offer.app_name = offer.get('appName')
183
        app_offer.package_name = offer.get('packageName')
184
        app_offer.description = input_json.get('description')
185
        app_offer.shortDescription = offer.get('shortDesc')
186
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 187
        app_offer.link = offer.get('url')
16537 kshitij.so 188
        app_offer.offer_active = True
189
        app_offer.priority = 0
190
        app_offer.show = False
191
        app_offer.offerCategory = offer.get('offerCategory')
192
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 193
        app_offer.ratings = offer.get('appRating')
194
        app_offer.downloads = offer.get('appDownloads')
195
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 196
    else:
197
        try:
198
            app_offer.offer_price = float(offer['offerPrice'])
199
        except:
200
            app_offer.offer_price = 0.0
201
        app_offer.user_payout = .8 * app_offer.offer_price
202
        app_offer.description = input_json.get('description')
203
        app_offer.shortDescription = offer.get('shortDesc')
204
        app_offer.longDescription = offer.get('longDesc')
16578 manish.sha 205
        app_offer.link = offer.get('url')
16537 kshitij.so 206
        app_offer.offerCategory = offer.get('offerCategory')
207
        app_offer.promoImage = input_json.get('promoImage')
16573 manish.sha 208
        app_offer.ratings = offer.get('appRating')
209
        app_offer.downloads = offer.get('appDownloads')
210
        app_offer.image_url = offer.get('iconUrl')
16537 kshitij.so 211
    session.commit()
16595 manish.sha 212
    '''
16685 manish.sha 213
    session.commit()
214
 
16537 kshitij.so 215
def markOfferAsInactive():
16540 kshitij.so 216
    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 217
    for active_offer in all_active_offers:
218
        active_offer.offer_active = False
219
    session.commit()
220
 
221
 
222
 
223
 
224
 
225
def main():
226
    try:
227
        dumpOffers()
16735 manish.sha 228
        print 'GOT_RESPONSE', GOT_RESPONSE
229
        if GOT_RESPONSE:
230
            markOfferAsInactive()
16906 manish.sha 231
            Mongo.populateAppOffers(AFFILIATE_ID)
16537 kshitij.so 232
    finally:
233
        session.close()
234
 
235
if __name__ == '__main__':
236
    main()