| Line 10... |
Line 10... |
| 10 |
from dtr.dao import AffiliateInfo, Order, SubOrder
|
10 |
from dtr.dao import AffiliateInfo, Order, SubOrder
|
| 11 |
from dtr.main import getBrowserObject, ScrapeException, getStore, ParseException, \
|
11 |
from dtr.main import getBrowserObject, ScrapeException, getStore, ParseException, \
|
| 12 |
Store as MStore, ungzipResponse, tprint
|
12 |
Store as MStore, ungzipResponse, tprint
|
| 13 |
from dtr.storage import Mongo
|
13 |
from dtr.storage import Mongo
|
| 14 |
from dtr.storage.Mongo import getImgSrc, getDealRank
|
14 |
from dtr.storage.Mongo import getImgSrc, getDealRank
|
| - |
|
15 |
from dtr.utils import utils
|
| 15 |
from dtr.utils.utils import fetchResponseUsingProxy
|
16 |
from dtr.utils.utils import fetchResponseUsingProxy
|
| 16 |
from pprint import pprint
|
17 |
from pprint import pprint
|
| 17 |
from pymongo import MongoClient
|
18 |
from pymongo import MongoClient
|
| 18 |
from urlparse import urlparse, parse_qs
|
19 |
from urlparse import urlparse, parse_qs
|
| - |
|
20 |
from xlrd import open_workbook
|
| 19 |
import json
|
21 |
import json
|
| - |
|
22 |
import os.path
|
| 20 |
import pymongo
|
23 |
import pymongo
|
| 21 |
import re
|
24 |
import re
|
| 22 |
import time
|
25 |
import time
|
| 23 |
import traceback
|
26 |
import traceback
|
| 24 |
import urllib
|
27 |
import urllib
|
| - |
|
28 |
import csv
|
| 25 |
|
29 |
|
| 26 |
USERNAME='profittill2@gmail.com'
|
30 |
USERNAME='profittill2@gmail.com'
|
| 27 |
PASSWORD='spice@2020'
|
31 |
PASSWORD='spice@2020'
|
| 28 |
AFFILIATE_URL='http://affiliate.snapdeal.com'
|
32 |
AFFILIATE_URL='http://affiliate.snapdeal.com/login/'
|
| 29 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
33 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
| 30 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
34 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
| 31 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
35 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
| 32 |
|
36 |
|
| - |
|
37 |
NEW_REPORT_URI_TEMPLATE = "http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=%s&toDate=%s&dump_report=True&request_type=product&status=%s"
|
| - |
|
38 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=2015-04-01&toDate=2015-09-15&dump_report=True&request_type=product&status=cancelled
|
| - |
|
39 |
#"http://affiliate.snapdeal.com/affiliate/reports/orders/report/?fromDate=07-09-2015&toDate=13-09-2015&dump_report=True&request_type=product&status=cancelled
|
| - |
|
40 |
|
| 33 |
class Store(MStore):
|
41 |
class Store(MStore):
|
| 34 |
|
42 |
|
| 35 |
'''
|
43 |
'''
|
| 36 |
This is to map order statuses of our system to order statuses of snapdeal.
|
44 |
This is to map order statuses of our system to order statuses of snapdeal.
|
| 37 |
And our statuses will change accordingly.
|
45 |
And our statuses will change accordingly.
|
| Line 53... |
Line 61... |
| 53 |
|
61 |
|
| 54 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
62 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
| 55 |
br = getBrowserObject()
|
63 |
br = getBrowserObject()
|
| 56 |
br.open(AFFILIATE_URL)
|
64 |
br.open(AFFILIATE_URL)
|
| 57 |
br.select_form(nr=0)
|
65 |
br.select_form(nr=0)
|
| 58 |
br.form['data[User][password]'] = PASSWORD
|
66 |
#br.form['data[User][password]'] = PASSWORD
|
| 59 |
br.form['data[User][email]'] = USERNAME
|
67 |
#br.form['data[User][email]'] = USERNAME
|
| - |
|
68 |
br.form['email'] = USERNAME
|
| - |
|
69 |
br.form['password'] = PASSWORD
|
| 60 |
br.submit()
|
70 |
br.submit()
|
| 61 |
response = br.open(CONFIG_URL)
|
- |
|
| 62 |
|
71 |
|
| - |
|
72 |
#after logged in successfully
|
| - |
|
73 |
|
| - |
|
74 |
#response = br.open(CONFIG_URL)
|
| - |
|
75 |
#find token to fetch api
|
| 63 |
token = re.findall('"session_token":"(.*?)"', ungzipResponse(response), re.IGNORECASE)[0]
|
76 |
#token = re.findall('"session_token":"(.*?)"', ungzipResponse(response), re.IGNORECASE)[0]
|
| 64 |
print token
|
77 |
#print token
|
| 65 |
allOffers = self._getAllOffers(br, token)
|
78 |
#allOffers = self._getAllOffers(br, token)
|
| - |
|
79 |
|
| - |
|
80 |
#fetch api using new report uri
|
| - |
|
81 |
endDate=date.today() + timedelta(days=1)
|
| - |
|
82 |
if startDate is None:
|
| - |
|
83 |
startDate=endDate - timedelta(days=31)
|
| - |
|
84 |
|
| - |
|
85 |
endDate = endDate.strftime('%d-%m-%Y')
|
| - |
|
86 |
startDate = startDate.strftime('%d-%m-%Y')
|
| - |
|
87 |
statuses=['cancelled', 'approved']
|
| - |
|
88 |
directory = "/SnapAff/"
|
| - |
|
89 |
curDate = directory + datetime.now().strftime('%Y-%m-%d')
|
| - |
|
90 |
if not os.path.exists(directory):
|
| - |
|
91 |
os.makedirs(directory)
|
| - |
|
92 |
for status in statuses:
|
| - |
|
93 |
reportUrl = NEW_REPORT_URI_TEMPLATE%(startDate, endDate, status)
|
| - |
|
94 |
print reportUrl
|
| - |
|
95 |
filename=curDate + "-" + status
|
| - |
|
96 |
fileobj = open(filename,"wb")
|
| - |
|
97 |
fileobj.write(utils.ungzipResponse(br.open(reportUrl)))
|
| - |
|
98 |
fileobj.close()
|
| - |
|
99 |
with open(filename, 'rb') as csvfile:
|
| - |
|
100 |
reader = csv.reader(csvfile)
|
| 66 |
self._saveToAffiliate(allOffers)
|
101 |
self._saveToAffiliate(reader)
|
| 67 |
|
102 |
|
| 68 |
def _setLastSaleDate(self, saleDate):
|
103 |
def _setLastSaleDate(self, saleDate):
|
| 69 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
104 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
| 70 |
|
105 |
|
| 71 |
|
106 |
|
| Line 579... |
Line 614... |
| 579 |
result = bulk.execute()
|
614 |
result = bulk.execute()
|
| 580 |
tprint(result)
|
615 |
tprint(result)
|
| 581 |
|
616 |
|
| 582 |
|
617 |
|
| 583 |
def _saveToAffiliate(self, offers):
|
618 |
def _saveToAffiliate(self, offers):
|
| 584 |
collection = self.db.snapdealOrderAffiliateInfo
|
619 |
collection = self.db.snapdealOrderAffiliateInfo1
|
| 585 |
mcollection = self.db.merchantOrder
|
620 |
#mcollection = self.db.merchantOrder
|
| 586 |
for offer in offers:
|
621 |
for offer in offers:
|
| - |
|
622 |
count = 0
|
| - |
|
623 |
for row in offers:
|
| - |
|
624 |
if count==0:
|
| - |
|
625 |
count += 1
|
| - |
|
626 |
continue
|
| - |
|
627 |
offer = self.covertToObj(row)
|
| - |
|
628 |
if offer.orderId:
|
| - |
|
629 |
collection.insert(todict(offer))
|
| - |
|
630 |
# def _saveToAffiliate(self, offers):
|
| - |
|
631 |
# collection = self.db.snapdealOrderAffiliateInfo
|
| - |
|
632 |
# mcollection = self.db.merchantOrder
|
| - |
|
633 |
# for offer in offers:
|
| 587 |
offer = self.covertToObj(offer)
|
634 |
# offer = self.covertToObj(offer)
|
| 588 |
collection.update({"adId":offer.adId, "saleAmount":offer.saleAmount, "payOut":offer.payOut},{"$set":todict(offer)}, upsert=True)
|
635 |
# collection.update({"adId":offer.adId, "saleAmount":offer.saleAmount, "payOut":offer.payOut},{"$set":todict(offer)}, upsert=True)
|
| 589 |
mcollection.update({"subTagId":offer.subTagId, "storeId":self.store_id, "subOrders.missingAff":True}, {"$set":{"subOrders.$.missingAff":False}})
|
636 |
# mcollection.update({"subTagId":offer.subTagId, "storeId":self.store_id, "subOrders.missingAff":True}, {"$set":{"subOrders.$.missingAff":False}})
|
| 590 |
|
637 |
|
| 591 |
|
638 |
|
| 592 |
def _getAllOffers(self, br, token):
|
639 |
def _getAllOffers(self, br, token):
|
| 593 |
allOffers = []
|
640 |
allOffers = []
|
| 594 |
nextPage = 1
|
641 |
nextPage = 1
|
| Line 605... |
Line 652... |
| 605 |
if rmap['data']['pageCount']<nextPage:
|
652 |
if rmap['data']['pageCount']<nextPage:
|
| 606 |
break
|
653 |
break
|
| 607 |
|
654 |
|
| 608 |
return allOffers
|
655 |
return allOffers
|
| 609 |
|
656 |
|
| - |
|
657 |
# def covertToObj(self,offer):
|
| - |
|
658 |
# offerData = offer['Stat']
|
| - |
|
659 |
# offer1 = AffiliateInfo(offerData['affiliate_info1'], self.store_id, offerData['conversion_status'], offerData['ad_id'],
|
| - |
|
660 |
# offerData['datetime'], int(float(offerData['payout'])), offer['Offer']['name'], offerData['ip'], int(float(offerData['conversion_sale_amount'])))
|
| - |
|
661 |
# offer1.saleTime = int(time.mktime(datetime.strptime(offer1.saleDate, "%Y-%m-%d %H:%M:%S").timetuple()))
|
| - |
|
662 |
# return offer1
|
| - |
|
663 |
|
| 610 |
def covertToObj(self,offer):
|
664 |
def covertToObj(self,offer):
|
| 611 |
offerData = offer['Stat']
|
665 |
product,category,order_code,quantity,price,sales,commission_rate,commission_earned,datetime1,affiliate_sub_id1,affiliate_sub_id2 = offer
|
| 612 |
offer1 = AffiliateInfo(offerData['affiliate_info1'], self.store_id, offerData['conversion_status'], offerData['ad_id'],
|
666 |
offer1 = AffiliateInfo(affiliate_sub_id1, 3, None, None, utils.toTimeStamp(datetime.strptime(datetime1, "%m-%d-%Y %H:%M")),
|
| 613 |
offerData['datetime'], int(float(offerData['payout'])), offer['Offer']['name'], offerData['ip'], int(float(offerData['conversion_sale_amount'])))
|
667 |
0 if "-" else int(float(commission_earned)), None, None, int(float(sales)))
|
| 614 |
offer1.saleTime = int(time.mktime(datetime.strptime(offer1.saleDate, "%Y-%m-%d %H:%M:%S").timetuple()))
|
668 |
offer1.orderId = order_code if order_code else None
|
| - |
|
669 |
offer1.productCode = product
|
| - |
|
670 |
offer1.unitPrice = int(float(price))
|
| - |
|
671 |
offer1.quantity = int(quantity)
|
| 615 |
return offer1
|
672 |
return offer1
|
| 616 |
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
|
673 |
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
|
| 617 |
endDate=date.today() + timedelta(days=1)
|
674 |
endDate=date.today() + timedelta(days=1)
|
| 618 |
startDate=endDate - timedelta(days=31)
|
675 |
startDate=endDate - timedelta(days=31)
|
| 619 |
|
676 |
|
| Line 644... |
Line 701... |
| 644 |
|
701 |
|
| 645 |
def main():
|
702 |
def main():
|
| 646 |
#print todict([1,2,"3"])
|
703 |
#print todict([1,2,"3"])
|
| 647 |
store = getStore(3)
|
704 |
store = getStore(3)
|
| 648 |
#store.parseOrderRawHtml(332221, "3232311", 2, "32311243", "https://m.snapdeal.com/orderSummary?code=3fbc8a02a1c4d3c4e906f46886de0464&order=5808451506")
|
705 |
#store.parseOrderRawHtml(332221, "3232311", 2, "32311243", "https://m.snapdeal.com/orderSummary?code=3fbc8a02a1c4d3c4e906f46886de0464&order=5808451506")
|
| 649 |
store.scrapeStoreOrders()
|
706 |
#store.scrapeStoreOrders()
|
| 650 |
#store._isSubOrderActive(8, "5970688907")
|
707 |
#store._isSubOrderActive(8, "5970688907")
|
| 651 |
#store.scrapeAffiliate()
|
708 |
store.scrapeAffiliate(datetime(2015,4,1))
|
| 652 |
#store.scrapeStoreOrders()
|
709 |
#store.scrapeStoreOrders()
|
| 653 |
|
710 |
|
| 654 |
|
711 |
|
| 655 |
|
712 |
|
| 656 |
def todict(obj, classkey=None):
|
713 |
def todict(obj, classkey=None):
|