Subversion Repositories SmartDukaan

Rev

Rev 16414 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16407 kshitij.so 1
import pymongo
2
from dtr.utils.utils import to_java_date, getNlcPoints
3
from datetime import datetime, timedelta
4
from operator import itemgetter
5
from dtr.utils import PaytmOfferScraper, PaytmScraper
6
from multiprocessing import Pool as ThreadPool
7
from multiprocessing import cpu_count
8
import optparse
9
from dtr.storage.MemCache import MemCache
10
from dtr.utils.utils import getCashBack
11
import traceback
12
 
13
con = None
14
 
15
parser = optparse.OptionParser()
16
parser.add_option("-m", "--m", dest="mongoHost",
17
                      default="localhost",
18
                      type="string", help="The HOST where the mongo server is running",
19
                      metavar="mongo_host")
20
 
21
(options, args) = parser.parse_args()
22
 
23
mc = MemCache(options.mongoHost)
24
 
25
ignoreItems = []
26
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5,'PAYTM.COM':6}
27
 
28
def get_mongo_connection(host=options.mongoHost, port=27017):
29
    global con
30
    if con is None:
31
        print "Establishing connection %s host and port %d" %(host,port)
32
        try:
33
            con = pymongo.MongoClient(host, port)
34
        except Exception, e:
35
            print e
36
            return None
37
    return con
38
 
39
def populate():
40
    toScrapMap = {}
41
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
42
    for bestSeller in bestSellers: 
43
        paytmBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':6}))
44
        for data in paytmBestSellers:
45
            if not toScrapMap.has_key(data['_id']):
46
                data['dealFlag'] = 0
47
                data['dealType'] = 0
48
                data['dealPoints'] = 0
49
                data['manualDealThresholdPrice'] = None
50
                toScrapMap[data['_id']] = data
51
    dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':6,'showDeal':1,'totalPoints':{'$gt':-100}}))
52
    for deal in dealFlagged:
53
        if not toScrapMap.has_key(deal['_id']):
54
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
55
            data[0]['dealFlag'] = 0
56
            data[0]['dealType'] = 0
57
            data[0]['dealPoints'] = 0
58
            data[0]['manualDealThresholdPrice'] = None
59
            toScrapMap[deal['_id']] = data[0]
60
    manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':6}))
61
    for manualDeal in manualDeals:
62
        if not toScrapMap.has_key(manualDeal['sku']):
63
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
64
            if len(data) > 0:
65
                data[0]['dealFlag'] = 1
66
                data[0]['dealType'] = manualDeal['dealType']
67
                data[0]['dealPoints'] = manualDeal['dealPoints']
68
                data[0]['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
69
                toScrapMap[manualDeal['sku']] = data[0]
70
        else:
71
            data = toScrapMap.get(manualDeal['sku'])
72
            data['dealFlag'] = 1
73
            data['dealType'] = manualDeal['dealType']
74
            data['dealPoints'] = manualDeal['dealPoints']
75
            data['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
76
    pool = ThreadPool(cpu_count() *2)
77
    pool.map(scrapePaytm,toScrapMap.values())
78
    pool.close()
79
    pool.join()
80
    print "joining threads at %s"%(str(datetime.now()))
81
 
82
def scrapePaytm(data):
83
    if data['source_id']!=6:
84
        return
85
    retryCount = 0
86
    if data['identifier'] is None or len(data['identifier'].strip())==0:
87
        print "returning in valid identifier"
88
        return
89
 
90
    if data['_id'] in ignoreItems:
91
        print "Ignored items returning for %d"%(data['_id'])
92
        return 
93
 
94
    try:
95
        if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
96
            print "sku id is already updated %d" %(data['_id']) 
97
            return
98
    except:
99
        pass
100
 
101
    paytmScraper = PaytmScraper.PaytmScraper()
102
    url = "https://catalog.paytm.com/v1/mobile/product/%s"%(data['identifier'].strip())
103
    try:
104
        result = paytmScraper.read(url)
105
        effective_price = result.get('offerPrice')
106
        coupon = ""
107
        try:
108
            offers = PaytmOfferScraper.fetchOffers(result['offerUrl'])
109
            bestOffer = {}
110
            for offer_data in offers.get('codes'):
111
                if effective_price < offer_data.get('effective_price'):
112
                    effective_price = offer_data.get('effective_price')
113
                    bestOffer = offer_data
114
                    coupon = bestOffer.get('code')
115
        except:
116
            pass
117
        available_price = effective_price 
118
        if result['inStock']:
119
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':1,'buyBoxFlag':1,'codAvailable':result.get('cod'),'coupon':coupon}})
120
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':available_price , 'in_stock':1,'codAvailable':result.get('cod')}})
121
        else:
122
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':0,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':1,'codAvailable':result.get('cod'),'coupon':coupon}})
123
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0,'codAvailable':result.get('cod')}})
124
 
125
    except:
126
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':0,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':1}})
127
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0}})
128
 
129
 
130
 
131
    try:
132
        recomputeDeal(data)
133
    except:
134
        print "Unable to compute deal for %s"%(data['skuBundleId'])
135
 
136
def recomputePoints(item, deal):
137
    try:
138
        if item.get('available_price') == deal['available_price']:
139
            print "No need to compute points for %d , as price is still same" %(item['_id'])
140
            raise
141
        nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
142
    except:
143
        print traceback.print_exc()
144
        nlcPoints = deal['nlcPoints']
145
    if item['manualDealThresholdPrice'] >= deal['available_price']:
146
        dealPoints = item['dealPoints']
147
    else:
148
        dealPoints = 0
149
    get_mongo_connection().Catalog.Deals.update({'_id':deal['_id']},{"$set":{'totalPoints':deal['totalPoints'] - deal['nlcPoints'] + nlcPoints - deal['dealPoints'] +dealPoints , 'nlcPoints': nlcPoints, 'dealPoints': dealPoints, 'manualDealThresholdPrice': item['manualDealThresholdPrice']}})
150
 
151
def populateNegativeDeals():
152
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
153
    mc.set("negative_deals", negativeDeals, 600)  
154
 
155
def recomputeDeal(item):
156
    """Lets recompute deal for this bundle"""
157
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
158
    skuBundleId = item['skuBundleId']
159
 
160
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
161
    bestPrice = float("inf")
162
    bestOne = None
163
    bestSellerPoints = 0
164
    toUpdate = []
165
    prepaidBestPrice = float("inf")
166
    prepaidBestOne = None
167
    prepaidBestSellerPoints = 0
168
    for similarItem in similarItems:
169
        if similarItem['_id'] == item['_id']:
170
            try:
171
                recomputePoints(item, similarItem)
172
            except:
173
                traceback.print_exc()
174
        if similarItem['codAvailable'] ==1:
175
            if mc.get("negative_deals") is None:
176
                populateNegativeDeals()
177
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
178
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
179
                continue
180
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
181
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
182
                continue
183
            if similarItem['available_price'] < bestPrice:
184
                bestOne = similarItem
185
                bestPrice = similarItem['available_price']
186
                bestSellerPoints = similarItem['bestSellerPoints']
187
            elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
188
                bestOne = similarItem
189
                bestPrice = similarItem['available_price']
190
                bestSellerPoints = similarItem['bestSellerPoints']
191
            else:
192
                pass
193
        else:
194
            if mc.get("negative_deals") is None:
195
                populateNegativeDeals()
196
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
197
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
198
                continue
199
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
200
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
201
                continue
202
            if similarItem['available_price'] < prepaidBestPrice:
203
                prepaidBestOne = similarItem
204
                prepaidBestPrice = similarItem['available_price']
205
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
206
            elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
207
                prepaidBestOne = similarItem
208
                prepaidBestPrice = similarItem['available_price']
209
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
210
            else:
211
                pass
212
    if bestOne is not None or prepaidBestOne is not None:
213
        for similarItem in similarItems:
214
            toUpdate.append(similarItem['_id'])
215
        if bestOne is not None:
216
            toUpdate.remove(bestOne['_id'])
217
            get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
218
        if prepaidBestOne is not None:
219
            if bestOne is not None:
220
                if prepaidBestOne['available_price'] < bestOne['available_price']: 
221
                    toUpdate.remove(prepaidBestOne['_id'])
222
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
223
            else:
224
                toUpdate.remove(prepaidBestOne['_id'])
225
                get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
226
    if len(toUpdate) > 0:
227
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
228
 
229
def main():
230
    populate()
231
 
232
if __name__=='__main__':
233
    main()