Subversion Repositories SmartDukaan

Rev

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