| 13569 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Jan 15, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
| 13576 |
amit.gupta |
6 |
from BeautifulSoup import BeautifulSoup
|
| 13569 |
amit.gupta |
7 |
from bson.binary import Binary
|
| 13680 |
amit.gupta |
8 |
from datetime import datetime, date, timedelta
|
| 13569 |
amit.gupta |
9 |
from dtr import main
|
| 13576 |
amit.gupta |
10 |
from dtr.dao import AffiliateInfo, Order, SubOrder
|
| 13680 |
amit.gupta |
11 |
from dtr.main import getBrowserObject, ScrapeException, getStore, ParseException, Store as MStore,\
|
|
|
12 |
ungzipResponse
|
| 13662 |
amit.gupta |
13 |
from pprint import pprint
|
| 13569 |
amit.gupta |
14 |
from pymongo import MongoClient
|
|
|
15 |
import json
|
|
|
16 |
import pymongo
|
|
|
17 |
import re
|
| 13662 |
amit.gupta |
18 |
import traceback
|
| 13569 |
amit.gupta |
19 |
import urllib
|
| 13603 |
amit.gupta |
20 |
|
| 13721 |
amit.gupta |
21 |
USERNAME='profittill2@gmail.com'
|
| 13569 |
amit.gupta |
22 |
PASSWORD='spice@2020'
|
|
|
23 |
AFFILIATE_URL='http://affiliate.snapdeal.com'
|
|
|
24 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
|
|
25 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
|
|
26 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
|
|
27 |
|
| 13662 |
amit.gupta |
28 |
class Store(MStore):
|
| 13569 |
amit.gupta |
29 |
|
|
|
30 |
'''
|
|
|
31 |
This is to map order statuses of our system to order statuses of snapdeal.
|
|
|
32 |
And our statuses will change accordingly.
|
|
|
33 |
|
|
|
34 |
'''
|
|
|
35 |
OrderStatusMap = {
|
| 13662 |
amit.gupta |
36 |
MStore.ORDER_PLACED : ['In Progress','N/A'],
|
|
|
37 |
MStore.ORDER_DELIVERED : ['Delivered'],
|
|
|
38 |
MStore.ORDER_SHIPPED : ['In Transit'],
|
| 13809 |
amit.gupta |
39 |
MStore.ORDER_CANCELLED : ['Closed For Vendor Reallocation', 'Cancelled', 'Product returned by courier', 'Returned']
|
| 13569 |
amit.gupta |
40 |
}
|
| 13662 |
amit.gupta |
41 |
|
|
|
42 |
CONF_CB_AMOUNT = MStore.CONF_CB_DISCOUNTED_PRICE
|
| 13569 |
amit.gupta |
43 |
def __init__(self,store_id):
|
|
|
44 |
super(Store, self).__init__(store_id)
|
|
|
45 |
|
|
|
46 |
def getName(self):
|
|
|
47 |
return "snapdeal"
|
|
|
48 |
|
|
|
49 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
|
|
50 |
br = getBrowserObject()
|
|
|
51 |
br.open(AFFILIATE_URL)
|
|
|
52 |
br.select_form(nr=0)
|
|
|
53 |
br.form['data[User][password]'] = PASSWORD
|
|
|
54 |
br.form['data[User][email]'] = USERNAME
|
|
|
55 |
br.submit()
|
|
|
56 |
response = br.open(CONFIG_URL)
|
|
|
57 |
|
| 13680 |
amit.gupta |
58 |
token = re.findall('"session_token":"(.*?)"', ungzipResponse(response), re.IGNORECASE)[0]
|
| 14145 |
amit.gupta |
59 |
print token
|
| 13569 |
amit.gupta |
60 |
allOffers = self._getAllOffers(br, token)
|
|
|
61 |
|
| 13662 |
amit.gupta |
62 |
allPyOffers = []
|
|
|
63 |
maxSaleDate = self._getLastSaleDate()
|
|
|
64 |
newMaxSaleDate = maxSaleDate
|
|
|
65 |
for offer in allOffers:
|
| 13680 |
amit.gupta |
66 |
pyOffer = self.covertToObj(offer).__dict__
|
|
|
67 |
allPyOffers.append(pyOffer)
|
|
|
68 |
saleDate = datetime.strptime(pyOffer['saleDate'],"%Y-%m-%d %H:%M:%S")
|
| 13662 |
amit.gupta |
69 |
if maxSaleDate < saleDate:
|
| 13721 |
amit.gupta |
70 |
self._updateOrdersPayBackStatus({'subTagId':pyOffer['subTagId'], 'saleDate':pyOffer['saleDate']}, {})
|
| 13662 |
amit.gupta |
71 |
if newMaxSaleDate < saleDate:
|
|
|
72 |
newMaxSaleDate = saleDate
|
|
|
73 |
|
|
|
74 |
self._setLastSaleDate(newMaxSaleDate)
|
| 13569 |
amit.gupta |
75 |
self._saveToAffiliate(allPyOffers)
|
|
|
76 |
|
| 13662 |
amit.gupta |
77 |
def _setLastSaleDate(self, saleDate):
|
| 13680 |
amit.gupta |
78 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
| 13569 |
amit.gupta |
79 |
|
| 13662 |
amit.gupta |
80 |
|
|
|
81 |
|
|
|
82 |
def _getLastSaleDate(self,):
|
|
|
83 |
lastDaySaleObj = self.db.lastDaySale.find_one({"storeId":self.store_id})
|
|
|
84 |
if lastDaySaleObj is None:
|
|
|
85 |
return datetime.min
|
|
|
86 |
|
| 13760 |
amit.gupta |
87 |
def _parse(self, orderId, subTagId, userId, page, orderSuccessUrl):
|
| 13662 |
amit.gupta |
88 |
|
| 13760 |
amit.gupta |
89 |
#page=page.decode("utf-8")
|
|
|
90 |
soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
91 |
#orderHead = soup.find(name, attrs, recursive, text)
|
|
|
92 |
sections = soup.findAll("section")
|
|
|
93 |
|
|
|
94 |
#print sections
|
|
|
95 |
|
|
|
96 |
order = sections[1]
|
|
|
97 |
orderTrs = order.findAll("tr")
|
|
|
98 |
|
|
|
99 |
placedOn = str(orderTrs[0].findAll("td")[1].text)
|
|
|
100 |
|
|
|
101 |
#Pop two section elements
|
|
|
102 |
sections.pop(0)
|
|
|
103 |
sections.pop(0)
|
|
|
104 |
subOrders = sections
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
merchantSubOrders = []
|
|
|
108 |
|
|
|
109 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
| 14023 |
amit.gupta |
110 |
merchantOrder.merchantOrderId = re.findall(r'\d+', str(soup.find("div", {"class":"deals_heading"})))[1]
|
| 13760 |
amit.gupta |
111 |
for orderTr in orderTrs:
|
|
|
112 |
orderTrString = str(orderTr)
|
|
|
113 |
if "Total Amount" in orderTrString:
|
|
|
114 |
merchantOrder.totalAmount = re.findall(r'\d+', orderTrString)[0]
|
|
|
115 |
elif "Delivery Charges" in orderTrString:
|
|
|
116 |
merchantOrder.deliveryCharges = re.findall(r'\d+', orderTrString)[0]
|
|
|
117 |
elif "Discount Applied" in orderTrString:
|
|
|
118 |
merchantOrder.discountApplied = re.findall(r'\d+', orderTrString)[0]
|
|
|
119 |
elif "Paid Amount" in orderTrString:
|
|
|
120 |
merchantOrder.paidAmount = re.findall(r'\d+', orderTrString)[0]
|
|
|
121 |
|
|
|
122 |
for subOrderElement in subOrders:
|
| 13809 |
amit.gupta |
123 |
subOrders = self.parseSubOrder(subOrderElement, placedOn)
|
|
|
124 |
merchantSubOrders.extend(subOrders)
|
| 13760 |
amit.gupta |
125 |
|
|
|
126 |
merchantOrder.subOrders = merchantSubOrders
|
|
|
127 |
return merchantOrder
|
|
|
128 |
|
|
|
129 |
def parseSubOrder(self, subOrderElement, placedOn):
|
| 13809 |
amit.gupta |
130 |
subOrders = []
|
| 13760 |
amit.gupta |
131 |
productUrl = str(subOrderElement.find("a")['href'])
|
|
|
132 |
subTable = subOrderElement.find("table", {"class":"lrPad"})
|
|
|
133 |
subTrs = subTable.findAll("tr")
|
|
|
134 |
unitPrice=None
|
|
|
135 |
offerDiscount = None
|
|
|
136 |
deliveryCharges = None
|
|
|
137 |
amountPaid = None
|
|
|
138 |
for subTr in subTrs:
|
|
|
139 |
subTrString = str(subTr)
|
|
|
140 |
if "Unit Price" in subTrString:
|
|
|
141 |
unitPrice = re.findall(r'\d+', subTrString)[0]
|
|
|
142 |
if "Quantity" in subTrString:
|
|
|
143 |
qty = re.findall(r'\d+', subTrString)[0]
|
|
|
144 |
elif "Offer Discount" in subTrString:
|
|
|
145 |
offerDiscount = re.findall(r'\d+', subTrString)[0]
|
|
|
146 |
elif "Delivery Charges" in subTrString:
|
|
|
147 |
deliveryCharges = re.findall(r'\d+', subTrString)[0]
|
|
|
148 |
elif "Subtotal" in subTrString:
|
|
|
149 |
if int(qty) > 0:
|
|
|
150 |
amountPaid = str(int(re.findall(r'\d+', subTrString)[0])/int(qty))
|
|
|
151 |
else:
|
|
|
152 |
amountPaid = "0"
|
|
|
153 |
if self.CONF_CB_AMOUNT == MStore.CONF_CB_SELLING_PRICE or offerDiscount is None:
|
|
|
154 |
amount = int(unitPrice)
|
|
|
155 |
else:
|
|
|
156 |
amount = int(unitPrice) - int(offerDiscount)
|
|
|
157 |
|
|
|
158 |
divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
|
|
|
159 |
if len(divs)<=0:
|
|
|
160 |
raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
|
|
|
161 |
|
|
|
162 |
for div in divs:
|
|
|
163 |
productTitle = str(subOrderElement.find("a").text)
|
|
|
164 |
productUrl = "http://m.snapdeal.com/" + productUrl
|
|
|
165 |
subOrder = SubOrder(productTitle, productUrl, placedOn, amountPaid)
|
|
|
166 |
|
|
|
167 |
subOrder.amountPaid = amountPaid
|
|
|
168 |
subOrder.deliveryCharges = deliveryCharges
|
|
|
169 |
subOrder.offerDiscount = offerDiscount
|
| 13809 |
amit.gupta |
170 |
subOrder.unitPrice = int(unitPrice)
|
| 13760 |
amit.gupta |
171 |
subOrder.productCode = re.findall(r'\d+$', productUrl)[0]
|
|
|
172 |
(cashbackAmount, percentage) = self.getCashbackAmount(subOrder.productCode, amount)
|
| 13770 |
amit.gupta |
173 |
cashbackStatus = Store.CB_PENDING
|
| 13760 |
amit.gupta |
174 |
if cashbackAmount <= 0:
|
|
|
175 |
cashbackStatus = Store.CB_NA
|
|
|
176 |
subOrder.cashBackStatus = cashbackStatus
|
|
|
177 |
subOrder.cashBackAmount = cashbackAmount
|
|
|
178 |
if percentage > 0:
|
|
|
179 |
subOrder.cashBackPercentage = percentage
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
trackAnchor = div.find("a")
|
|
|
183 |
if trackAnchor is not None:
|
|
|
184 |
subOrder.tracingkUrl = str(trackAnchor['href'])
|
|
|
185 |
|
|
|
186 |
divStr = str(div)
|
|
|
187 |
divStr = divStr.replace("\n","").replace("\t", "")
|
|
|
188 |
|
|
|
189 |
for line in divStr.split("<br />"):
|
|
|
190 |
if "Suborder ID" in line:
|
|
|
191 |
subOrder.merchantSubOrderId = re.findall(r'\d+', line)[0]
|
|
|
192 |
elif "Status" in line:
|
|
|
193 |
subOrder.detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
|
|
194 |
elif "Est. Shipping Date" in line:
|
|
|
195 |
subOrder.estimatedShippingDate = line.split(":")[1].strip()
|
|
|
196 |
elif "Est. Delivery Date" in line:
|
|
|
197 |
subOrder.estimatedDeliveryDate = line.split(":")[1].strip()
|
|
|
198 |
elif "Courier Name" in line:
|
|
|
199 |
subOrder.courierName = line.split(":")[1].strip()
|
|
|
200 |
elif "Tracking No" in line:
|
|
|
201 |
subOrder.trackingNumber = line.split(":")[1].strip()
|
| 13809 |
amit.gupta |
202 |
subOrders.append(subOrder)
|
|
|
203 |
return subOrders
|
| 13760 |
amit.gupta |
204 |
|
| 13576 |
amit.gupta |
205 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
|
| 13760 |
amit.gupta |
206 |
#print merchantOrder
|
| 13796 |
amit.gupta |
207 |
resp = {}
|
| 13582 |
amit.gupta |
208 |
try:
|
|
|
209 |
br = getBrowserObject()
|
|
|
210 |
url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', orderSuccessUrl,re.IGNORECASE)[0]
|
| 13791 |
amit.gupta |
211 |
page = br.open(url)
|
|
|
212 |
page = ungzipResponse(page)
|
| 14145 |
amit.gupta |
213 |
merchantOrder = self._parse(orderId, subTagId, userId, page, orderSuccessUrl)
|
|
|
214 |
merchantOrder.orderTrackingUrl = url
|
| 13582 |
amit.gupta |
215 |
self._saveToOrder(todict(merchantOrder))
|
| 13796 |
amit.gupta |
216 |
resp['result'] = 'ORDER_CREATED'
|
|
|
217 |
return resp
|
| 13582 |
amit.gupta |
218 |
except:
|
|
|
219 |
print "Error occurred"
|
| 13603 |
amit.gupta |
220 |
traceback.print_exc()
|
| 13796 |
amit.gupta |
221 |
resp['result'] = 'PARSE_ERROR'
|
|
|
222 |
return resp
|
| 13781 |
amit.gupta |
223 |
|
| 13569 |
amit.gupta |
224 |
|
|
|
225 |
#soup = BeautifulSoup(rawHtml,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
226 |
#soup.find(name, attrs, recursive, text)
|
| 13576 |
amit.gupta |
227 |
|
|
|
228 |
def _getStatusFromDetailedStatus(self, detailedStatus):
|
|
|
229 |
for key, value in Store.OrderStatusMap.iteritems():
|
|
|
230 |
if detailedStatus in value:
|
|
|
231 |
return key
|
| 14287 |
amit.gupta |
232 |
print "Detailed Status need to be mapped", detailedStatus
|
| 13576 |
amit.gupta |
233 |
raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
|
| 13569 |
amit.gupta |
234 |
|
| 13610 |
amit.gupta |
235 |
|
| 13569 |
amit.gupta |
236 |
def scrapeStoreOrders(self,):
|
| 13760 |
amit.gupta |
237 |
#collectionMap = {'palcedOn':1}
|
| 13576 |
amit.gupta |
238 |
orders = self._getActiveOrders()
|
| 13730 |
amit.gupta |
239 |
print "Found orders", orders
|
| 13576 |
amit.gupta |
240 |
br = getBrowserObject()
|
|
|
241 |
for order in orders:
|
|
|
242 |
url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', order['orderSuccessUrl'],re.IGNORECASE)[0]
|
|
|
243 |
response = br.open(url)
|
| 13680 |
amit.gupta |
244 |
page = ungzipResponse(response)
|
| 13576 |
amit.gupta |
245 |
#page=page.decode("utf-8")
|
|
|
246 |
soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
247 |
sections = soup.findAll("section")
|
| 13760 |
amit.gupta |
248 |
orderEl = sections[1]
|
|
|
249 |
orderTrs = orderEl.findAll("tr")
|
|
|
250 |
|
|
|
251 |
placedOn = str(orderTrs[0].findAll("td")[1].text)
|
| 13576 |
amit.gupta |
252 |
sections.pop(0)
|
|
|
253 |
sections.pop(0)
|
|
|
254 |
|
|
|
255 |
subOrders = sections
|
|
|
256 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
|
|
257 |
for subOrderElement in subOrders:
|
|
|
258 |
closed = True
|
|
|
259 |
divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
|
|
|
260 |
if len(divs)<=0:
|
|
|
261 |
raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
|
| 13721 |
amit.gupta |
262 |
subOrder = None
|
| 13760 |
amit.gupta |
263 |
breakFlag = False
|
| 13576 |
amit.gupta |
264 |
for div in divs:
|
|
|
265 |
divStr = str(div)
|
|
|
266 |
divStr = divStr.replace("\n","").replace("\t", "")
|
|
|
267 |
updateMap = {}
|
|
|
268 |
for line in divStr.split("<br />"):
|
|
|
269 |
if "Suborder ID" in line:
|
| 13634 |
amit.gupta |
270 |
merchantSubOrderId = re.findall(r'\d+', line)[0]
|
|
|
271 |
#break if suborder is inactive
|
| 13721 |
amit.gupta |
272 |
subOrder = self._isSubOrderActive(order, merchantSubOrderId)
|
|
|
273 |
if subOrder is None:
|
| 13809 |
amit.gupta |
274 |
subOrders = self.parseSubOrder(subOrderElement, placedOn)
|
| 14239 |
amit.gupta |
275 |
self.db.merchantOrder.update({"orderId":order['orderId']},{'$push':{"subOrders":{"$each":todict(subOrders)}}})
|
| 14241 |
amit.gupta |
276 |
print "Added new suborders to Order id - " + order['orderId']
|
| 13760 |
amit.gupta |
277 |
closed = False
|
| 14240 |
amit.gupta |
278 |
breakFlag = True
|
|
|
279 |
break
|
| 13760 |
amit.gupta |
280 |
elif subOrder['closed']:
|
|
|
281 |
breakFlag = True
|
| 13634 |
amit.gupta |
282 |
break
|
| 13760 |
amit.gupta |
283 |
else:
|
|
|
284 |
findMap = {"orderId": order['orderId'], "subOrders.merchantSubOrderId": merchantSubOrderId}
|
| 13576 |
amit.gupta |
285 |
elif "Status" in line:
|
|
|
286 |
detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
|
|
287 |
updateMap["subOrders.$.detailedStatus"] = detailedStatus
|
|
|
288 |
status = self._getStatusFromDetailedStatus(detailedStatus)
|
| 13634 |
amit.gupta |
289 |
closedStatus = status in [Store.ORDER_DELIVERED, Store.ORDER_CANCELLED]
|
| 13760 |
amit.gupta |
290 |
updateMap["subOrders.$.status"] = status
|
|
|
291 |
if detailedStatus == 'Closed For Vendor Reallocation':
|
| 13770 |
amit.gupta |
292 |
#if it is more than 6hours mark closed.
|
| 13760 |
amit.gupta |
293 |
closeAt = subOrder.get("closeAt")
|
|
|
294 |
if closeAt is None:
|
|
|
295 |
closeAt = datetime.now() + timedelta(hours=6)
|
| 13770 |
amit.gupta |
296 |
updateMap["subOrders.$.closeAt"] = datetime.strftime(closeAt,"%Y-%m-%d %H:%M:%S")
|
| 13760 |
amit.gupta |
297 |
else:
|
| 13770 |
amit.gupta |
298 |
closeAt = datetime.strptime(closeAt,"%Y-%m-%d %H:%M:%S")
|
| 13760 |
amit.gupta |
299 |
if datetime.now() > closeAt:
|
|
|
300 |
closedStatus = True
|
|
|
301 |
|
|
|
302 |
|
| 13634 |
amit.gupta |
303 |
if closedStatus:
|
|
|
304 |
#if status is closed then change the paybackStatus accordingly
|
| 13760 |
amit.gupta |
305 |
updateMap["subOrders.$.closed"] = True
|
|
|
306 |
if status == Store.ORDER_DELIVERED:
|
| 13721 |
amit.gupta |
307 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| 13634 |
amit.gupta |
308 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_APPROVED
|
| 13760 |
amit.gupta |
309 |
elif status == Store.ORDER_CANCELLED:
|
| 13721 |
amit.gupta |
310 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| 13634 |
amit.gupta |
311 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_CANCELLED
|
|
|
312 |
|
|
|
313 |
else:
|
|
|
314 |
closed = False
|
| 13576 |
amit.gupta |
315 |
elif "Est. Shipping Date" in line:
|
|
|
316 |
estimatedShippingDate = line.split(":")[1].strip()
|
|
|
317 |
updateMap["subOrders.$.estimatedShippingDate"] = estimatedShippingDate
|
|
|
318 |
elif "Est. Delivery Date" in line:
|
|
|
319 |
estimatedDeliveryDate = line.split(":")[1].strip()
|
|
|
320 |
updateMap["subOrders.$.estimatedDeliveryDate"] = estimatedDeliveryDate
|
|
|
321 |
elif "Courier Name" in line:
|
|
|
322 |
courierName = line.split(":")[1].strip()
|
|
|
323 |
updateMap["subOrders.$.courierName"] = courierName
|
|
|
324 |
elif "Tracking No" in line:
|
|
|
325 |
trackingNumber = line.split(":")[1].strip()
|
|
|
326 |
updateMap["subOrders.$.trackingNumber"] = trackingNumber
|
| 13760 |
amit.gupta |
327 |
|
|
|
328 |
if breakFlag:
|
|
|
329 |
break
|
| 13576 |
amit.gupta |
330 |
|
|
|
331 |
bulk.find(findMap).update({'$set' : updateMap})
|
| 13721 |
amit.gupta |
332 |
bulk.find({'orderId': order['orderId']}).update({'$set':{'closed': closed}})
|
| 13576 |
amit.gupta |
333 |
result = bulk.execute()
|
|
|
334 |
pprint(result)
|
|
|
335 |
|
|
|
336 |
|
| 13569 |
amit.gupta |
337 |
def _saveToAffiliate(self, offers):
|
| 13725 |
amit.gupta |
338 |
if offers is None or len(offers)==0:
|
|
|
339 |
print "no affiliate have been pushed"
|
|
|
340 |
return
|
| 13576 |
amit.gupta |
341 |
collection = self.db.snapdealOrderAffiliateInfo
|
| 13569 |
amit.gupta |
342 |
try:
|
|
|
343 |
collection.insert(offers,continue_on_error=True)
|
|
|
344 |
except pymongo.errors.DuplicateKeyError as e:
|
|
|
345 |
print e.details
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
def _getAllOffers(self, br, token):
|
|
|
349 |
allOffers = []
|
|
|
350 |
nextPage = 1
|
|
|
351 |
while True:
|
|
|
352 |
data = getPostData(token, nextPage)
|
|
|
353 |
response = br.open(POST_URL, data)
|
| 13680 |
amit.gupta |
354 |
rmap = json.loads(ungzipResponse(response))
|
| 13569 |
amit.gupta |
355 |
if rmap is not None:
|
|
|
356 |
rmap = rmap['response']
|
|
|
357 |
if rmap is not None and len(rmap['errors'])==0:
|
|
|
358 |
allOffers += rmap['data']['data']
|
|
|
359 |
nextPage += 1
|
|
|
360 |
if rmap['data']['pageCount']<nextPage:
|
|
|
361 |
break
|
|
|
362 |
|
|
|
363 |
return allOffers
|
|
|
364 |
|
|
|
365 |
def covertToObj(self,offer):
|
|
|
366 |
offerData = offer['Stat']
|
|
|
367 |
offer1 = AffiliateInfo(offerData['affiliate_info1'], self.store_id, offerData['conversion_status'], offerData['ad_id'],
|
|
|
368 |
offerData['datetime'], offerData['payout'], offer['Offer']['name'], offerData['ip'], offerData['conversion_sale_amount'])
|
| 13680 |
amit.gupta |
369 |
|
| 13569 |
amit.gupta |
370 |
return offer1
|
|
|
371 |
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
|
| 13680 |
amit.gupta |
372 |
endDate=date.today() + timedelta(days=1)
|
|
|
373 |
startDate=endDate - timedelta(days=31)
|
| 13569 |
amit.gupta |
374 |
|
|
|
375 |
parameters = (
|
|
|
376 |
("page",str(page)),
|
|
|
377 |
("limit",str(limit)),
|
|
|
378 |
("fields[]","Stat.offer_id"),
|
|
|
379 |
("fields[]","Stat.datetime"),
|
|
|
380 |
("fields[]","Offer.name"),
|
|
|
381 |
("fields[]","Stat.conversion_status"),
|
|
|
382 |
("fields[]","Stat.conversion_sale_amount"),
|
|
|
383 |
("fields[]","Stat.payout"),
|
|
|
384 |
("fields[]","Stat.ip"),
|
|
|
385 |
("fields[]","Stat.ad_id"),
|
|
|
386 |
("fields[]","Stat.affiliate_info1"),
|
|
|
387 |
("sort[Stat.datetime]","desc"),
|
|
|
388 |
("filters[Stat.date][conditional]","BETWEEN"),
|
|
|
389 |
("filters[Stat.date][values][]",startDate.strftime('%Y-%m-%d')),
|
|
|
390 |
("filters[Stat.date][values][]",endDate.strftime('%Y-%m-%d')),
|
|
|
391 |
("data_start",startDate.strftime('%Y-%m-%d')),
|
|
|
392 |
("data_end",endDate.strftime('%Y-%m-%d')),
|
|
|
393 |
("Method","getConversions"),
|
|
|
394 |
("NetworkId","jasper"),
|
|
|
395 |
("SessionToken",token),
|
|
|
396 |
)
|
|
|
397 |
#Encode the parameters
|
|
|
398 |
return urllib.urlencode(parameters)
|
|
|
399 |
|
|
|
400 |
def main():
|
| 14239 |
amit.gupta |
401 |
print todict([1,2,"3"])
|
|
|
402 |
#store = getStore(3)
|
|
|
403 |
#store.scrapeStoreOrders()
|
| 13662 |
amit.gupta |
404 |
#store._isSubOrderActive(8, "5970688907")
|
| 13760 |
amit.gupta |
405 |
#store.scrapeAffiliate()
|
| 13576 |
amit.gupta |
406 |
#store.parseOrderRawHtml(12345, "subtagId", 122323, "html", 'https://m.snapdeal.com/purchaseMobileComplete?code=1f4166d13ea799b65aa9dea68b3e9e70&order=4509499363')
|
| 13569 |
amit.gupta |
407 |
|
|
|
408 |
|
| 13576 |
amit.gupta |
409 |
|
|
|
410 |
def todict(obj, classkey=None):
|
|
|
411 |
if isinstance(obj, dict):
|
|
|
412 |
data = {}
|
|
|
413 |
for (k, v) in obj.items():
|
|
|
414 |
data[k] = todict(v, classkey)
|
|
|
415 |
return data
|
|
|
416 |
elif hasattr(obj, "_ast"):
|
|
|
417 |
return todict(obj._ast())
|
|
|
418 |
elif hasattr(obj, "__iter__"):
|
|
|
419 |
return [todict(v, classkey) for v in obj]
|
|
|
420 |
elif hasattr(obj, "__dict__"):
|
|
|
421 |
data = dict([(key, todict(value, classkey))
|
|
|
422 |
for key, value in obj.__dict__.iteritems()
|
|
|
423 |
if not callable(value) and not key.startswith('_')])
|
|
|
424 |
if classkey is not None and hasattr(obj, "__class__"):
|
|
|
425 |
data[classkey] = obj.__class__.__name__
|
|
|
426 |
return data
|
|
|
427 |
else:
|
|
|
428 |
return obj
|
| 14239 |
amit.gupta |
429 |
|
|
|
430 |
if __name__ == '__main__':
|
|
|
431 |
main()
|