| Line 29... |
Line 29... |
| 29 |
import pymongo
|
29 |
import pymongo
|
| 30 |
import re
|
30 |
import re
|
| 31 |
import time
|
31 |
import time
|
| 32 |
import traceback
|
32 |
import traceback
|
| 33 |
import urllib
|
33 |
import urllib
|
| - |
|
34 |
import urllib2
|
| 34 |
|
35 |
|
| 35 |
USERNAME='profittill2@gmail.com'
|
36 |
USERNAME='profittill2@gmail.com'
|
| 36 |
PASSWORD='spice@2020'
|
37 |
PASSWORD='spice@2020'
|
| 37 |
AFFILIATE_URL='http://affiliate.snapdeal.com/login/'
|
38 |
AFFILIATE_URL='http://affiliate.snapdeal.com/login/'
|
| 38 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
39 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
| 39 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
40 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
| 40 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
41 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
| 41 |
|
42 |
|
| 42 |
NEW_REPORT_URI_TEMPLATE = "http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=%s&toDate=%s&dump_report=True&request_type=product&status=%s"
|
43 |
#NEW_REPORT_URI_TEMPLATE = "http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=%s&toDate=%s&dump_report=True&request_type=product&status=%s"
|
| - |
|
44 |
AFF_URL_TEMPLATE = "http://affiliate-feeds.snapdeal.com/feed/api/order?startDate=%s&endDate=%s&status=%s"
|
| 43 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=2015-04-01&toDate=2015-09-15&dump_report=True&request_type=product&status=cancelled
|
45 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=2015-04-01&toDate=2015-09-15&dump_report=True&request_type=product&status=cancelled
|
| 44 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=07-09-2015&toDate=13-09-2015&dump_report=True&request_type=product&status=cancelled
|
46 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=07-09-2015&toDate=13-09-2015&dump_report=True&request_type=product&status=cancelled
|
| 45 |
|
47 |
|
| - |
|
48 |
AFF_ID = "33550"
|
| - |
|
49 |
AFF_TOKEN="66d526141b9d39c4b2b4ff76eadc34"
|
| - |
|
50 |
|
| 46 |
class Store(MStore):
|
51 |
class Store(MStore):
|
| 47 |
|
52 |
|
| 48 |
'''
|
53 |
'''
|
| 49 |
This is to map order statuses of our system to order statuses of snapdeal.
|
54 |
This is to map order statuses of our system to order statuses of snapdeal.
|
| 50 |
And our statuses will change accordingly.
|
55 |
And our statuses will change accordingly.
|
| Line 67... |
Line 72... |
| 67 |
|
72 |
|
| 68 |
def getName(self):
|
73 |
def getName(self):
|
| 69 |
return "snapdeal"
|
74 |
return "snapdeal"
|
| 70 |
|
75 |
|
| 71 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
76 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
| 72 |
br = getBrowserObject()
|
- |
|
| 73 |
br.open(AFFILIATE_URL)
|
- |
|
| 74 |
br.select_form(nr=0)
|
- |
|
| 75 |
#br.form['data[User][password]'] = PASSWORD
|
- |
|
| 76 |
#br.form['data[User][email]'] = USERNAME
|
- |
|
| 77 |
br.form['email'] = USERNAME
|
- |
|
| 78 |
br.form['password'] = PASSWORD
|
- |
|
| 79 |
br.submit()
|
- |
|
| 80 |
|
- |
|
| 81 |
#after logged in successfully
|
- |
|
| 82 |
|
77 |
|
| 83 |
#response = br.open(CONFIG_URL)
|
- |
|
| 84 |
#find token to fetch api
|
- |
|
| 85 |
#token = re.findall('"session_token":"(.*?)"', ungzipResponse(response), re.IGNORECASE)[0]
|
- |
|
| 86 |
#print token
|
- |
|
| 87 |
#allOffers = self._getAllOffers(br, token)
|
- |
|
| 88 |
|
- |
|
| 89 |
#fetch api using new report uri
|
- |
|
| 90 |
endDate=date.today() - timedelta(days=1)
|
78 |
endDate=date.today() - timedelta(days=1)
|
| 91 |
if startDate is None:
|
79 |
if startDate is None:
|
| 92 |
startDate = endDate - timedelta(days=45)
|
80 |
startDate = endDate - timedelta(days=45)
|
| 93 |
|
81 |
|
| 94 |
endDate = endDate.strftime('%d-%m-%Y')
|
82 |
endDate = endDate.strftime('%Y-%m-%d')
|
| 95 |
startDate = startDate.strftime('%d-%m-%Y')
|
83 |
startDate = startDate.strftime('%Y-%m-%d')
|
| 96 |
statuses=['cancelled', 'approved']
|
84 |
statuses=['cancelled', 'approved']
|
| 97 |
directory = "/SnapAff/"
|
- |
|
| 98 |
curDate = directory + datetime.now().strftime('%Y-%m-%d')
|
- |
|
| 99 |
if not os.path.exists(directory):
|
- |
|
| 100 |
os.makedirs(directory)
|
85 |
|
| 101 |
for status in statuses:
|
86 |
for status in statuses:
|
| 102 |
if status == "approved":
|
87 |
nextUrl = AFF_URL_TEMPLATE%(startDate, endDate, status)
|
| 103 |
print "app"
|
88 |
while nextUrl:
|
| 104 |
reportUrl = NEW_REPORT_URI_TEMPLATE%(startDate, endDate, status)
|
89 |
req = urllib2.Request(nextUrl)
|
| 105 |
print reportUrl
|
90 |
nextUrl=''
|
| 106 |
filename=curDate + "-" + status
|
91 |
req.add_header('Snapdeal-Affiliate-Id', AFF_ID)
|
| 107 |
fileobj = open(filename,"wb")
|
92 |
req.add_header('Snapdeal-Token-Id', AFF_TOKEN)
|
| 108 |
fileobj.write(utils.ungzipResponse(br.open(reportUrl)))
|
93 |
req.add_header('Accept', 'application/json')
|
| 109 |
fileobj.close()
|
94 |
resp = urllib2.urlopen(req)
|
| 110 |
with open(filename, 'rb') as csvfile:
|
95 |
ordersDict = json.loads(resp.read())
|
| 111 |
reader = csv.reader(csvfile)
|
96 |
|
| 112 |
self._saveToAffiliate(reader, status)
|
97 |
self._saveToAffiliate(ordersDict['productDetails'], status)
|
| 113 |
|
98 |
|
| 114 |
def _setLastSaleDate(self, saleDate):
|
99 |
def _setLastSaleDate(self, saleDate):
|
| 115 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
100 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
| 116 |
|
101 |
|
| 117 |
|
102 |
|
| Line 812... |
Line 797... |
| 812 |
|
797 |
|
| 813 |
def _saveToAffiliate(self, offers, status):
|
798 |
def _saveToAffiliate(self, offers, status):
|
| 814 |
collection = self.db.snapdealOrderAffiliateInfo1
|
799 |
collection = self.db.snapdealOrderAffiliateInfo1
|
| 815 |
#mcollection = self.db.merchantOrder
|
800 |
#mcollection = self.db.merchantOrder
|
| 816 |
for offer in offers:
|
801 |
for offer in offers:
|
| 817 |
count = 0
|
- |
|
| 818 |
for row in offers:
|
- |
|
| 819 |
if count==0:
|
- |
|
| 820 |
count += 1
|
- |
|
| 821 |
continue
|
- |
|
| 822 |
offer = self.covertToObj(row)
|
802 |
offer = self.covertToObj(offer)
|
| 823 |
if offer.orderId:
|
803 |
if offer.orderId:
|
| 824 |
dict1 = todict(offer)
|
804 |
dict1 = todict(offer)
|
| 825 |
dict1["_id"] = dict1["orderId"] + "-" + dict1["productCode"]
|
805 |
dict1["_id"] = dict1["orderId"] + "-" + dict1["productCode"]
|
| 826 |
dict1['status'] = status
|
806 |
dict1['status'] = status
|
| 827 |
collection.save(dict1)
|
807 |
collection.save(dict1)
|
| Line 885... |
Line 865... |
| 885 |
continue
|
865 |
continue
|
| 886 |
finally:
|
866 |
finally:
|
| 887 |
session.close()
|
867 |
session.close()
|
| 888 |
|
868 |
|
| 889 |
def covertToObj(self,offer):
|
869 |
def covertToObj(self,offer):
|
| 890 |
product,category,order_code,quantity,price,sales,commission_rate,commission_earned,datetime1,affiliate_sub_id1,affiliate_sub_id2 = offer
|
- |
|
| - |
|
870 |
|
| 891 |
offer1 = AffiliateInfo(affiliate_sub_id1, 3, None, None, utils.toTimeStamp(datetime.strptime(datetime1, "%m-%d-%Y %H:%M")),
|
871 |
offer1 = AffiliateInfo(offer["affiliateSubId1"], 3, None, None, utils.toTimeStamp(datetime.strptime(offer["dateTime"], "%m/%d/%Y %H:%M:%S")),
|
| 892 |
0 if commission_earned=="-" else int(float(commission_earned)), None, None, int(float(sales)))
|
872 |
offer["commissionEarned"], None, None, offer["sale"])
|
| 893 |
offer1.orderId = order_code if order_code else None
|
873 |
offer1.orderId = offer.get('orderCode') if offer.get('orderCode') else None
|
| 894 |
offer1.productCode = product
|
874 |
offer1.productCode = offer["product"]
|
| 895 |
offer1.unitPrice = int(float(price))
|
875 |
offer1.unitPrice = offer["price"]
|
| 896 |
offer1.quantity = int(quantity)
|
876 |
offer1.quantity = offer["quantity"]
|
| 897 |
offer1.payOut
|
877 |
offer1.saleTime = offer["dateTime"]
|
| 898 |
return offer1
|
878 |
return offer1
|
| 899 |
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
|
879 |
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
|
| 900 |
endDate=date.today() + timedelta(days=1)
|
880 |
endDate=date.today() + timedelta(days=1)
|
| 901 |
startDate=endDate - timedelta(days=31)
|
881 |
startDate=endDate - timedelta(days=31)
|
| 902 |
|
882 |
|
| Line 928... |
Line 908... |
| 928 |
|
908 |
|
| 929 |
def main():
|
909 |
def main():
|
| 930 |
#print todict([1,2,"3"])
|
910 |
#print todict([1,2,"3"])
|
| 931 |
store = getStore(3)
|
911 |
store = getStore(3)
|
| 932 |
#store.parseOrderRawHtml(332221, "3232311", 2, readSSh("/home/amit/snapdeal.html"), "https://m.snapdeal.com/purchaseMobileComplete?code=b92753bd7236bb3efbd6e8a0df46b962&order=9627657388")
|
912 |
#store.parseOrderRawHtml(332221, "3232311", 2, readSSh("/home/amit/snapdeal.html"), "https://m.snapdeal.com/purchaseMobileComplete?code=b92753bd7236bb3efbd6e8a0df46b962&order=9627657388")
|
| 933 |
store.scrapeStoreOrders()
|
913 |
store.scrapeAffiliate()
|
| 934 |
#https://m.snapdeal.com/purchaseMobileComplete?code=3fbc8a02a1c4d3c4e906f46886de0464&order=5808451506
|
914 |
#https://m.snapdeal.com/purchaseMobileComplete?code=3fbc8a02a1c4d3c4e906f46886de0464&order=5808451506
|
| 935 |
#https://m.snapdeal.com/purchaseMobileComplete?code=9f4dfa49ff08a16d04c5e4bf519506fc&order=9611672826
|
915 |
#https://m.snapdeal.com/purchaseMobileComplete?code=9f4dfa49ff08a16d04c5e4bf519506fc&order=9611672826
|
| 936 |
|
916 |
|
| 937 |
# orders = list(session.query(OrdersRaw).filter_by(store_id=3).filter_by(status='ORDER_NOT_CREATED').all())
|
917 |
# orders = list(session.query(OrdersRaw).filter_by(store_id=3).filter_by(status='ORDER_NOT_CREATED').all())
|
| 938 |
# for o in orders:
|
918 |
# for o in orders:
|