| 13569 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Jan 15, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
| 14618 |
amit.gupta |
6 |
from datetime import datetime, timedelta
|
| 14413 |
amit.gupta |
7 |
from dtr.config import PythonPropertyReader, PythonPropertyReader
|
| 14618 |
amit.gupta |
8 |
from dtr.storage.Mysql import getOrdersAfterDate1, getOrdersAfterDate
|
| 13868 |
amit.gupta |
9 |
from pprint import pprint
|
| 13569 |
amit.gupta |
10 |
from pymongo.mongo_client import MongoClient
|
| 14413 |
amit.gupta |
11 |
import base64
|
| 13569 |
amit.gupta |
12 |
import importlib
|
| 13662 |
amit.gupta |
13 |
import json
|
|
|
14 |
import math
|
| 13569 |
amit.gupta |
15 |
import mechanize
|
| 14413 |
amit.gupta |
16 |
import time
|
| 13631 |
amit.gupta |
17 |
import traceback
|
| 13662 |
amit.gupta |
18 |
import urllib
|
|
|
19 |
import urllib2
|
| 14848 |
amit.gupta |
20 |
sourceMap = {1:"amazon", 2:"flipkart", 3:"snapdeal", 4:"spice", 5:"homeshop18", 6:"paytm"}
|
| 13662 |
amit.gupta |
21 |
headers = {
|
|
|
22 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
|
|
23 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
24 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
25 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
|
|
26 |
}
|
| 14145 |
amit.gupta |
27 |
CASHBACK_URL = PythonPropertyReader.getConfig('CASHBACK_URL')
|
|
|
28 |
USER_LOOKUP_URL = PythonPropertyReader.getConfig('USER_LOOKUP_URL')
|
|
|
29 |
WALLET_CREDIT_URL = PythonPropertyReader.getConfig('WALLET_CREDIT_URL')
|
| 13569 |
amit.gupta |
30 |
|
|
|
31 |
def getStore(source_id):
|
|
|
32 |
#module = sourceMap[source_id]
|
|
|
33 |
store = Store(source_id)
|
| 13631 |
amit.gupta |
34 |
try:
|
|
|
35 |
module = importlib.import_module("dtr.sources." + sourceMap[source_id])
|
|
|
36 |
store = getattr(module, "Store")(source_id)
|
|
|
37 |
return store
|
|
|
38 |
except:
|
| 13781 |
amit.gupta |
39 |
traceback.print_exc()
|
| 13631 |
amit.gupta |
40 |
return None
|
| 13569 |
amit.gupta |
41 |
|
|
|
42 |
class ScrapeException(Exception):
|
|
|
43 |
"""Exception raised for errors in the input.
|
|
|
44 |
|
|
|
45 |
Attributes:
|
|
|
46 |
expr -- input expression in which the error occurred
|
|
|
47 |
msg -- explanation of the error
|
|
|
48 |
"""
|
|
|
49 |
|
|
|
50 |
def __init__(self, expr, msg):
|
|
|
51 |
self.expr = expr
|
|
|
52 |
self.msg = msg
|
|
|
53 |
|
|
|
54 |
class ParseException(Exception):
|
|
|
55 |
"""Exception raised for errors in the input.
|
|
|
56 |
|
|
|
57 |
Attributes:
|
|
|
58 |
expr -- input expression in which the error occurred
|
|
|
59 |
msg -- explanation of the error
|
|
|
60 |
"""
|
|
|
61 |
|
|
|
62 |
def __init__(self, expr, msg):
|
|
|
63 |
self.expr = expr
|
|
|
64 |
self.msg = msg
|
|
|
65 |
|
| 13677 |
amit.gupta |
66 |
client = MongoClient('mongodb://localhost:27017/')
|
|
|
67 |
|
| 13569 |
amit.gupta |
68 |
class Store(object):
|
|
|
69 |
|
|
|
70 |
ORDER_PLACED = 'Order Placed'
|
|
|
71 |
ORDER_DELIVERED = 'Delivered'
|
|
|
72 |
ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
|
|
|
73 |
ORDER_CANCELLED = 'Cancelled'
|
|
|
74 |
|
| 13662 |
amit.gupta |
75 |
CB_INIT = 'Waiting Confirmation'
|
| 13610 |
amit.gupta |
76 |
CB_PENDING = 'Pending'
|
| 13955 |
amit.gupta |
77 |
CB_CREDIT_IN_PROCESS = 'Credit in process'
|
| 13610 |
amit.gupta |
78 |
CB_CREDITED = 'Credited to wallet'
|
|
|
79 |
CB_NA = 'Not Applicable'
|
|
|
80 |
CB_APPROVED = 'Approved'
|
| 14618 |
amit.gupta |
81 |
CB_REJECTED = 'Rejected'
|
|
|
82 |
CB_ONHOLD = 'On hold'
|
| 13610 |
amit.gupta |
83 |
CB_CANCELLED = 'Cancelled'
|
| 13569 |
amit.gupta |
84 |
|
| 13662 |
amit.gupta |
85 |
CONF_CB_SELLING_PRICE = 0
|
|
|
86 |
CONF_CB_DISCOUNTED_PRICE = 1
|
| 13610 |
amit.gupta |
87 |
|
| 13569 |
amit.gupta |
88 |
def __init__(self, store_id):
|
| 13662 |
amit.gupta |
89 |
self.db = client.Dtr
|
| 13569 |
amit.gupta |
90 |
self.store_id = store_id
|
| 13576 |
amit.gupta |
91 |
self.store_name = sourceMap[store_id]
|
| 13569 |
amit.gupta |
92 |
|
| 13662 |
amit.gupta |
93 |
'''
|
|
|
94 |
To Settle payback for respective stores.
|
|
|
95 |
Also ensures that settlement happens only for approved orders
|
|
|
96 |
'''
|
| 13569 |
amit.gupta |
97 |
|
|
|
98 |
def getName(self):
|
|
|
99 |
raise NotImplementedError
|
|
|
100 |
|
|
|
101 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
|
|
102 |
raise NotImplementedError
|
|
|
103 |
|
|
|
104 |
def saveToAffiliate(self, offers):
|
|
|
105 |
raise NotImplementedError
|
|
|
106 |
|
| 14624 |
amit.gupta |
107 |
def getUpdateMap(self, subOrder):
|
|
|
108 |
updateMap = {}
|
|
|
109 |
closed = False
|
|
|
110 |
for (key,value) in subOrder.iteritems():
|
|
|
111 |
if key=="status":
|
|
|
112 |
if value==Store.ORDER_CANCELLED:
|
|
|
113 |
closed = True
|
|
|
114 |
updateMap['subOrders.$.closed'] = True
|
|
|
115 |
if subOrder['cashBackStatus'] == Store.CB_PENDING:
|
|
|
116 |
updateMap['subOrders.$.cashBackStatus'] = Store.CB_CANCELLED
|
|
|
117 |
if value==Store.ORDER_DELIVERED:
|
|
|
118 |
closed = True
|
|
|
119 |
updateMap['subOrders.$.closed'] = True
|
|
|
120 |
if subOrder['cashBackStatus'] == Store.CB_PENDING:
|
|
|
121 |
updateMap['subOrders.$.cashBackStatus'] = Store.CB_APPROVED
|
|
|
122 |
if key=='cashBackStatus':
|
|
|
123 |
continue
|
|
|
124 |
updateMap['subOrders.$.' + key] = value
|
|
|
125 |
subOrder['closed'] = closed
|
|
|
126 |
return updateMap
|
|
|
127 |
|
| 14618 |
amit.gupta |
128 |
def saveOrder(self, merchantOrder):
|
|
|
129 |
#merchantOrder = Order()
|
|
|
130 |
#merchantOrder.status
|
|
|
131 |
#mercha
|
|
|
132 |
pass
|
|
|
133 |
|
|
|
134 |
|
| 13569 |
amit.gupta |
135 |
def scrapeStoreOrders(self,):
|
|
|
136 |
raise NotImplementedError
|
| 13610 |
amit.gupta |
137 |
|
| 14618 |
amit.gupta |
138 |
def updateSubOrder(self, subOrder):
|
|
|
139 |
pass
|
|
|
140 |
#if subOrder.get
|
|
|
141 |
|
| 13690 |
amit.gupta |
142 |
def _saveToOrder(self, order):
|
|
|
143 |
collection = self.db.merchantOrder
|
|
|
144 |
try:
|
|
|
145 |
order = collection.insert(order)
|
| 14273 |
amit.gupta |
146 |
return True
|
| 13690 |
amit.gupta |
147 |
except Exception as e:
|
| 14273 |
amit.gupta |
148 |
return False
|
| 14081 |
amit.gupta |
149 |
|
|
|
150 |
def _updateToOrder(self, order):
|
|
|
151 |
collection = self.db.merchantOrder
|
|
|
152 |
try:
|
|
|
153 |
collection.update({"orderId":order['orderId']},{"$set":order}, upsert = True)
|
|
|
154 |
#merchantOder
|
|
|
155 |
except Exception as e:
|
|
|
156 |
traceback.print_exc()
|
| 13662 |
amit.gupta |
157 |
|
|
|
158 |
def getCashbackAmount(self, productCode, amount):
|
| 13995 |
amit.gupta |
159 |
alagvar = CASHBACK_URL % (productCode,self.store_id)
|
| 13662 |
amit.gupta |
160 |
filehandle = urllib2.Request(alagvar,headers=headers)
|
|
|
161 |
x= urllib2.urlopen(filehandle)
|
| 13995 |
amit.gupta |
162 |
rmap = json.loads(str(x.read()))
|
|
|
163 |
if len(rmap)==0:
|
| 13721 |
amit.gupta |
164 |
return (0,0)
|
| 13662 |
amit.gupta |
165 |
else:
|
| 13995 |
amit.gupta |
166 |
if rmap['cash_back_description'] == 'PERCENTAGE':
|
| 14295 |
amit.gupta |
167 |
return (math.floor((amount * rmap['cash_back'])/100), rmap['cash_back'])
|
| 13662 |
amit.gupta |
168 |
else:
|
| 14295 |
amit.gupta |
169 |
return (rmap['cash_back'], 0)
|
| 13721 |
amit.gupta |
170 |
|
| 13662 |
amit.gupta |
171 |
|
| 13569 |
amit.gupta |
172 |
'''
|
|
|
173 |
Parses the order for specific store
|
|
|
174 |
|
|
|
175 |
order id, total amount, created on(now() if could not parse
|
|
|
176 |
suborder id, title, quantity, unit price, expected delivery date,
|
|
|
177 |
status (default would be Order placed)
|
|
|
178 |
|
|
|
179 |
once products are identified, each suborder can then be updated
|
|
|
180 |
with respective cashback.
|
|
|
181 |
|
|
|
182 |
Possible fields to display for Not yet delivered orders are
|
|
|
183 |
Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
|
|
|
184 |
No need to show cancelled orders.
|
| 13610 |
amit.gupta |
185 |
CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
|
| 13569 |
amit.gupta |
186 |
OrderStatus - Placed/Cancelled/Delivered
|
|
|
187 |
'''
|
| 13576 |
amit.gupta |
188 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
|
| 13569 |
amit.gupta |
189 |
|
|
|
190 |
pass
|
|
|
191 |
|
| 13721 |
amit.gupta |
192 |
def _updateOrdersPayBackStatus(self, searchMap, updateMap):
|
| 13781 |
amit.gupta |
193 |
searchMap['subOrders.missingAff'] = False
|
|
|
194 |
updateMap['subOrders.$.missingAff'] = True
|
| 13927 |
amit.gupta |
195 |
self.db.merchantOrder.update(searchMap, { '$set': updateMap }, multi=True)
|
| 13721 |
amit.gupta |
196 |
|
| 13781 |
amit.gupta |
197 |
def _getActiveOrders(self, searchMap={}, collectionMap={}):
|
| 13721 |
amit.gupta |
198 |
collection = self.db.merchantOrder
|
| 13781 |
amit.gupta |
199 |
searchMap = dict(searchMap.items()+ {"closed": False, "storeId" : self.store_id}.items())
|
| 14274 |
amit.gupta |
200 |
collectionMap = dict(collectionMap.items() + {"orderSuccessUrl":1, "orderId":1,"subOrders":1, "placedOn":1}.items())
|
|
|
201 |
stores = collection.find(searchMap, collectionMap)
|
| 13721 |
amit.gupta |
202 |
return [store for store in stores]
|
|
|
203 |
|
| 14081 |
amit.gupta |
204 |
def _getMissingOrders(self,searchMap={}):
|
|
|
205 |
collection = self.db.merchantOrder
|
| 14292 |
amit.gupta |
206 |
searchMap = dict(searchMap.items()+ {"requireDetail":True}.items())
|
| 14081 |
amit.gupta |
207 |
orders = collection.find(searchMap)
|
|
|
208 |
return list(orders)
|
|
|
209 |
|
|
|
210 |
|
| 13721 |
amit.gupta |
211 |
def _isSubOrderActive(self,order, merchantSubOrderId):
|
|
|
212 |
subOrders = order.get("subOrders")
|
|
|
213 |
for subOrder in subOrders:
|
|
|
214 |
if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
|
|
|
215 |
return subOrder
|
|
|
216 |
return None
|
| 14663 |
amit.gupta |
217 |
|
| 14618 |
amit.gupta |
218 |
def settlePayBack(runtype='dry'):
|
| 14663 |
amit.gupta |
219 |
userAmountMap = {}
|
|
|
220 |
searchMapList = []
|
| 14666 |
amit.gupta |
221 |
for mo in client.Dtr.merchantOrder.find({"subOrders.cashBackStatus":Store.CB_APPROVED}):
|
| 14663 |
amit.gupta |
222 |
userId = mo.get("userId")
|
| 14664 |
amit.gupta |
223 |
if mo.get('subOrders') is not None:
|
| 14663 |
amit.gupta |
224 |
for so in mo['subOrders']:
|
| 14666 |
amit.gupta |
225 |
if so.get('cashBackStatus') == Store.CB_APPROVED:
|
| 14663 |
amit.gupta |
226 |
searchMapList.append({"orderId":mo.get("orderId"), "subOrders.merchantSubOrderId":so.get("merchantSubOrderId")})
|
|
|
227 |
if not userAmountMap.has_key(userId):
|
|
|
228 |
userAmountMap[userId] = so.get('cashBackAmount')
|
|
|
229 |
else:
|
|
|
230 |
userAmountMap[userId] += so.get('cashBackAmount')
|
|
|
231 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
|
|
232 |
else:
|
|
|
233 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
|
|
234 |
for searchMap in searchMapList:
|
|
|
235 |
print "%s\t%s"%(searchMap.get('orderId'),searchMap.get('subOrders.merchantSubOrderId'))
|
| 14664 |
amit.gupta |
236 |
for key,val in userAmountMap.iteritems():
|
|
|
237 |
print "%s\t%s"%(key,val)
|
| 14663 |
amit.gupta |
238 |
if (runtype=='live'):
|
| 14666 |
amit.gupta |
239 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
| 14663 |
amit.gupta |
240 |
if len(searchMapList) == 0:
|
| 14618 |
amit.gupta |
241 |
return
|
| 14666 |
amit.gupta |
242 |
for searchMap in searchMapList:
|
|
|
243 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}})
|
|
|
244 |
bulk.execute()
|
| 14663 |
amit.gupta |
245 |
|
| 13927 |
amit.gupta |
246 |
datetimeNow = datetime.now()
|
|
|
247 |
batchId = int(time.mktime(datetimeNow.timetuple()))
|
|
|
248 |
if refundToWallet(batchId, userAmountMap):
|
| 14666 |
amit.gupta |
249 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
|
|
250 |
for searchMap in searchMapList:
|
|
|
251 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_CREDITED, "subOrders.$.batchId":batchId}})
|
|
|
252 |
print bulk.execute()
|
|
|
253 |
for key, value in userAmountMap.iteritems():
|
|
|
254 |
client.Dtr.refund.insert({"userId": key, "batch":batchId, "userAmount":value, "timestamp":datetime.strftime(datetimeNow,"%Y-%m-%d %H:%M:%S")})
|
|
|
255 |
client.Dtr.user.update({"userId":key}, {'$inc': { "credited": value}}, upsert=True)
|
| 13952 |
amit.gupta |
256 |
tprint("PayBack Settled")
|
| 14666 |
amit.gupta |
257 |
else:
|
|
|
258 |
tprint("Error Occurred while running batch. Rolling Back")
|
|
|
259 |
bulk = client.Dtr.merchantOrder.initialize_ordered_bulk_op()
|
|
|
260 |
for searchMap in searchMapList:
|
|
|
261 |
bulk.find(searchMap).update({'$set' : {'subOrders.$.cashBackStatus':Store.CB_APPROVED}})
|
|
|
262 |
bulk.execute()
|
| 14663 |
amit.gupta |
263 |
|
|
|
264 |
|
|
|
265 |
def settlePayBack1(runtype='dry'):
|
|
|
266 |
approvedUserMap = {}
|
|
|
267 |
pendingUserMap = {}
|
|
|
268 |
creditedToWalletUserMap = {}
|
|
|
269 |
#client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_APPROVED},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}}, multi=True)
|
|
|
270 |
for mo in client.Dtr.merchantOrder.find({"subOrders.cashBackStatus":Store.CB_CREDITED}):
|
|
|
271 |
userId = mo.get("userId")
|
|
|
272 |
for so in mo['subOrders']:
|
|
|
273 |
print "%s\t%s\t%s\t%s\t%s\t%s\t%s"%(userId, mo.get("orderId"), so.get("merchantSubOrderId"),so.get("productTitle") ,so.get("cashBackStatus"), so.get("cashBackAmount"), so.get("batchId"))
|
|
|
274 |
|
|
|
275 |
for refund in client.Dtr.refund.find():
|
|
|
276 |
print "%s\t%s\t%s\t%s"%(refund.get("userId") ,refund.get("timestamp") ,refund.get("userAmount"), refund.get("batch"))
|
|
|
277 |
|
|
|
278 |
|
| 13927 |
amit.gupta |
279 |
def refundToWallet(batchId, userAmountMap):
|
| 14413 |
amit.gupta |
280 |
base64string = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 13927 |
amit.gupta |
281 |
batchUpdateMap = {}
|
|
|
282 |
try :
|
|
|
283 |
saholicUserAmountMap = {}
|
|
|
284 |
for key, value in userAmountMap.iteritems():
|
|
|
285 |
userLookupRequest = urllib2.Request(USER_LOOKUP_URL %(key), headers=headers)
|
| 14413 |
amit.gupta |
286 |
userLookupRequest.add_header("Authorization", "Basic %s" % base64string)
|
| 13927 |
amit.gupta |
287 |
try:
|
|
|
288 |
response = urllib2.urlopen(userLookupRequest).read()
|
| 13939 |
amit.gupta |
289 |
saholicUserId = json.loads(response)['account_key']
|
| 13927 |
amit.gupta |
290 |
saholicUserAmountMap[saholicUserId] = value
|
|
|
291 |
except:
|
| 13934 |
amit.gupta |
292 |
tprint("Could not fetch saholic id for user : " + str(key))
|
| 13927 |
amit.gupta |
293 |
continue
|
| 13939 |
amit.gupta |
294 |
if len(saholicUserAmountMap) > 0:
|
|
|
295 |
batchUpdateMap['userAmount'] = json.dumps(saholicUserAmountMap)
|
|
|
296 |
batchUpdateMap['batchId'] = batchId
|
|
|
297 |
request = urllib2.Request(WALLET_CREDIT_URL, headers=headers)
|
|
|
298 |
data = urllib.urlencode(batchUpdateMap)
|
|
|
299 |
response = urllib2.urlopen(request, data)
|
|
|
300 |
return json.loads(response.read())['response']['credited']
|
|
|
301 |
else:
|
|
|
302 |
tprint("Nothing to Refund")
|
|
|
303 |
return False
|
| 13927 |
amit.gupta |
304 |
except:
|
|
|
305 |
traceback.print_exc()
|
|
|
306 |
tprint("Could not batch refund")
|
|
|
307 |
return False
|
| 13868 |
amit.gupta |
308 |
|
| 13662 |
amit.gupta |
309 |
def main():
|
| 14618 |
amit.gupta |
310 |
# client = MongoClient('mongodb://root:ecip$dtrMay2014@dtr:27017/')
|
|
|
311 |
# s = Store(1)
|
|
|
312 |
# orders = s.db.Dtr.find({"subOrders.imgUrl":{"$exists":1}}, {"merchantOrderId":1, "subOrders.productCode":1,"subOrders.imgUrl":1,"_id": 0})
|
|
|
313 |
# db = client.Dtr
|
|
|
314 |
# for order in orders:
|
|
|
315 |
# for subOrder in order.get("subOrders"):
|
|
|
316 |
# #db.merchantOrder.update({"merchantOrderId":order.getMerchantOrderId,"subOrders.imgUrl":{"$exists":0}, "subOrders.productCode":subOrder.get("productCode")}, {"$set":{"subOrders.$.imgUrl":subOrder.get("imgUrl")}})
|
|
|
317 |
# db.merchantOrder.findOne()
|
| 14663 |
amit.gupta |
318 |
settlePayBack('dry')
|
| 13569 |
amit.gupta |
319 |
|
|
|
320 |
|
| 13868 |
amit.gupta |
321 |
###
|
|
|
322 |
#Settlement process is suposed to be a batch and run weekly
|
|
|
323 |
#It is should be running on first hour of mondays. As cron should
|
|
|
324 |
# Maintain a batch id.
|
|
|
325 |
|
| 13677 |
amit.gupta |
326 |
|
| 13569 |
amit.gupta |
327 |
def getBrowserObject():
|
|
|
328 |
import cookielib
|
|
|
329 |
br = mechanize.Browser(factory=mechanize.RobustFactory())
|
|
|
330 |
cj = cookielib.LWPCookieJar()
|
|
|
331 |
br.set_cookiejar(cj)
|
|
|
332 |
br.set_handle_equiv(True)
|
|
|
333 |
br.set_handle_redirect(True)
|
|
|
334 |
br.set_handle_referer(True)
|
|
|
335 |
br.set_handle_robots(False)
|
|
|
336 |
br.set_debug_http(False)
|
|
|
337 |
br.set_debug_redirects(False)
|
|
|
338 |
br.set_debug_responses(False)
|
|
|
339 |
|
|
|
340 |
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
|
|
|
341 |
|
|
|
342 |
br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
|
|
|
343 |
('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
|
|
|
344 |
('Accept-Encoding', 'gzip,deflate,sdch'),
|
|
|
345 |
('Accept-Language', 'en-US,en;q=0.8'),
|
|
|
346 |
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
| 13677 |
amit.gupta |
347 |
return br
|
|
|
348 |
|
| 13690 |
amit.gupta |
349 |
def ungzipResponse(r):
|
|
|
350 |
headers = r.info()
|
|
|
351 |
if headers['Content-Encoding']=='gzip':
|
|
|
352 |
import gzip
|
|
|
353 |
gz = gzip.GzipFile(fileobj=r, mode='rb')
|
|
|
354 |
html = gz.read()
|
|
|
355 |
gz.close()
|
|
|
356 |
return html
|
| 13724 |
amit.gupta |
357 |
|
|
|
358 |
def tprint(*msg):
|
| 13927 |
amit.gupta |
359 |
print datetime.now(), "-", msg
|
| 13939 |
amit.gupta |
360 |
|
|
|
361 |
if __name__ == '__main__':
|
| 14848 |
amit.gupta |
362 |
main()
|
|
|
363 |
|