Subversion Repositories SmartDukaan

Rev

Rev 16539 | Rev 16573 | 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
3
from dtr.storage.DataService import app_offers, app_affiliates
4
from dtr.utils.utils import AFFILIATE_OFFER_API, AFFILIATE_OFFER_DESC_API, fetchResponseUsingProxy
5
import json
6
 
7
AFFILIATE_ID = 1
8
DEVICE_ID = "SpiceRetail"
9
RETAILER_CODE = 123
10
ACTIVE_OFFERS = []
11
 
12
DataService.initialize(db_hostname='localhost' )
13
 
14
 
15
def dumpOffers():
16
    offer_url =  AFFILIATE_OFFER_API.get(AFFILIATE_ID)%(DEVICE_ID,RETAILER_CODE)
17
    response = fetchResponseUsingProxy(offer_url, proxy=False)
18
    input_json = json.loads(response)
19
    if input_json['status'].strip()=='ok' and input_json['message'] =="Success":
20
        offers = (input_json['payload'])['offers']
21
        for offer in offers:
22
            fetchOfferDescriptionAndDump(offer)
23
 
24
 
25
def fetchOfferDescriptionAndDump(offer):
26
    global ACTIVE_OFFERS
27
    offer_desc_url = AFFILIATE_OFFER_DESC_API.get(AFFILIATE_ID)%(DEVICE_ID,offer['offerId'],RETAILER_CODE)
28
    response = fetchResponseUsingProxy(offer_desc_url,proxy=False)
29
    offer_desc = json.loads(response)
30
    input_json = offer_desc['payload']
31
    app_offer = app_offers.get_by(affiliate_id=AFFILIATE_ID, affiliate_offer_id=offer['offerId'])
32
    ACTIVE_OFFERS.append(offer['offerId'])
33
    if app_offer is None:
34
        app_offer = app_offers()
35
        app_offer.affiliate_id = AFFILIATE_ID
36
        app_offer.affiliate_offer_id = offer['offerId']
37
        try:
38
            app_offer.offer_price = float(offer['offerPrice'])
39
        except:
40
            app_offer.offer_price = 0.0
41
        app_offer.user_payout = .8 * app_offer.offer_price
42
        app_offer.override_payout = False
43
        app_offer.overriden_payout = 0.0
44
        app_offer.app_name = offer.get('appName')
45
        app_offer.package_name = offer.get('packageName')
46
        app_offer.image_url = ""
47
        app_offer.description = input_json.get('description')
48
        app_offer.shortDescription = offer.get('shortDesc')
49
        app_offer.longDescription = offer.get('longDesc')
50
        app_offer.link = ""
51
        app_offer.offer_active = True
52
        app_offer.priority = 0
53
        app_offer.show = False
54
        app_offer.offerCategory = offer.get('offerCategory')
55
        app_offer.promoImage = input_json.get('promoImage')
56
    else:
57
        try:
58
            app_offer.offer_price = float(offer['offerPrice'])
59
        except:
60
            app_offer.offer_price = 0.0
61
        app_offer.user_payout = .8 * app_offer.offer_price
62
        app_offer.image_url = ""
63
        app_offer.description = input_json.get('description')
64
        app_offer.shortDescription = offer.get('shortDesc')
65
        app_offer.longDescription = offer.get('longDesc')
66
        app_offer.link = ""
67
        app_offer.offerCategory = offer.get('offerCategory')
68
        app_offer.promoImage = input_json.get('promoImage')
69
    session.commit()
70
 
71
def markOfferAsInactive():
16540 kshitij.so 72
    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 73
    for active_offer in all_active_offers:
74
        active_offer.offer_active = False
75
    session.commit()
76
 
77
 
78
 
79
 
80
 
81
def main():
82
    try:
83
        dumpOffers()
84
        markOfferAsInactive()
85
    finally:
86
        session.close()
87
 
88
if __name__ == '__main__':
89
    main()