Subversion Repositories SmartDukaan

Rev

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