| 14450 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Mar 13, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
|
|
6 |
from datetime import date, datetime, timedelta
|
|
|
7 |
from dtr.storage.Mysql import getOrdersAfterDate
|
|
|
8 |
from email import encoders
|
|
|
9 |
from email.mime.base import MIMEBase
|
|
|
10 |
from email.mime.multipart import MIMEMultipart
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
from pymongo.mongo_client import MongoClient
|
| 14460 |
amit.gupta |
13 |
from xlrd import open_workbook
|
|
|
14 |
from xlwt.Workbook import Workbook
|
| 14450 |
amit.gupta |
15 |
import MySQLdb
|
| 14460 |
amit.gupta |
16 |
from xlutils.copy import copy
|
| 14450 |
amit.gupta |
17 |
import smtplib
|
|
|
18 |
import time
|
|
|
19 |
import xlwt
|
| 14460 |
amit.gupta |
20 |
#from xlwt import
|
| 14450 |
amit.gupta |
21 |
|
|
|
22 |
client = MongoClient('mongodb://localhost:27017/')
|
|
|
23 |
|
|
|
24 |
SENDER = "cnc.center@shop2020.in"
|
|
|
25 |
PASSWORD = "5h0p2o2o"
|
|
|
26 |
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
|
|
|
27 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
28 |
SMTP_PORT = 587
|
|
|
29 |
|
| 14460 |
amit.gupta |
30 |
XLS_FILENAME = "dtrsourcesreco.xls"
|
|
|
31 |
boldStyle = xlwt.XFStyle()
|
|
|
32 |
f = xlwt.Font()
|
|
|
33 |
f.bold = True
|
|
|
34 |
boldStyle.font = f
|
|
|
35 |
i = -1
|
| 14450 |
amit.gupta |
36 |
def generateFlipkartReco():
|
| 14460 |
amit.gupta |
37 |
curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 2)
|
|
|
38 |
db = client.Dtr
|
|
|
39 |
rb = open_workbook(XLS_FILENAME)
|
|
|
40 |
wb = copy(rb)
|
|
|
41 |
#wb = xlwt.Workbook()
|
|
|
42 |
row = 0
|
|
|
43 |
worksheet = wb.add_sheet("Flipkart")
|
|
|
44 |
worksheet.write(row, inc(), 'Order Id', boldStyle)
|
|
|
45 |
worksheet.write(row, inc(), 'User Id', boldStyle)
|
| 14474 |
amit.gupta |
46 |
worksheet.write(row, inc(), 'Username', boldStyle)
|
| 14460 |
amit.gupta |
47 |
worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
|
|
|
48 |
worksheet.write(row, inc(), 'Product Title', boldStyle)
|
|
|
49 |
worksheet.write(row, inc(), 'Price', boldStyle)
|
|
|
50 |
worksheet.write(row, inc(), 'Quantity', boldStyle)
|
|
|
51 |
worksheet.write(row, inc(), 'Status', boldStyle)
|
|
|
52 |
worksheet.write(row, inc(), 'Detailed Status', boldStyle)
|
|
|
53 |
worksheet.write(row, inc(), 'Cashback Status', boldStyle)
|
|
|
54 |
worksheet.write(row, inc(), 'Our CashBack', boldStyle)
|
|
|
55 |
worksheet.write(row, inc(), 'Sale Date', boldStyle)
|
|
|
56 |
worksheet.write(row, inc(), 'SubtagId', boldStyle)
|
|
|
57 |
worksheet.write(row, inc(), 'Category', boldStyle)
|
|
|
58 |
worksheet.write(row, inc(), 'Aff PayOut', boldStyle)
|
|
|
59 |
worksheet.write(row, inc(), 'Aff Sale Amount', boldStyle)
|
|
|
60 |
worksheet.write(row, inc(), 'Aff Sale Date', boldStyle)
|
|
|
61 |
for row1 in curs:
|
|
|
62 |
orderId = row1[0]
|
|
|
63 |
order = db.merchantOrder.find_one({"orderId":orderId})
|
|
|
64 |
if order is None:
|
|
|
65 |
continue
|
|
|
66 |
print order["subTagId"]
|
|
|
67 |
print order
|
|
|
68 |
saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%d %B, %Y %I:%M %p").replace(hour=0, minute=0, second=0, microsecond=0).timetuple()))
|
|
|
69 |
#saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y %I:%M %p") + timedelta(seconds=-60)).timetuple()))
|
|
|
70 |
|
|
|
71 |
for subOrder in order['subOrders']:
|
| 14487 |
amit.gupta |
72 |
affs = list(db.flipkartOrderAffiliateInfo.find({"subOrders.productCode":subOrder.getProductCode("productCode"), "saleDateInt":saleTime}))
|
| 14460 |
amit.gupta |
73 |
row += 1
|
|
|
74 |
global i
|
|
|
75 |
i=-1
|
|
|
76 |
worksheet.write(row, inc(), orderId)
|
|
|
77 |
worksheet.write(row, inc(), row1[1])
|
| 14474 |
amit.gupta |
78 |
worksheet.write(row, inc(), row1[-1])
|
| 14460 |
amit.gupta |
79 |
worksheet.write(row, inc(), order['merchantOrderId'])
|
|
|
80 |
worksheet.write(row, inc(), subOrder['productTitle'])
|
|
|
81 |
worksheet.write(row, inc(), subOrder['amountPaid'])
|
|
|
82 |
worksheet.write(row, inc(), subOrder['quantity'])
|
|
|
83 |
worksheet.write(row, inc(), subOrder['status'])
|
|
|
84 |
worksheet.write(row, inc(), subOrder['detailedStatus'])
|
|
|
85 |
worksheet.write(row, inc(), subOrder['cashBackStatus'])
|
|
|
86 |
worksheet.write(row, inc(), subOrder['cashBackAmount'])
|
|
|
87 |
worksheet.write(row, inc(), subOrder['placedOn'])
|
|
|
88 |
worksheet.write(row, inc(), order['subTagId'])
|
|
|
89 |
for aff in affs:
|
| 14487 |
amit.gupta |
90 |
if aff['subTagId']== subOrder['subTagId']:
|
| 14460 |
amit.gupta |
91 |
worksheet.write(row, inc(), aff['category'])
|
|
|
92 |
worksheet.write(row, inc(), aff['payOut'])
|
|
|
93 |
worksheet.write(row, inc(), aff['saleAmount'])
|
|
|
94 |
worksheet.write(row, inc(), aff['saleDate'])
|
|
|
95 |
break
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
wb.save(XLS_FILENAME)
|
| 14450 |
amit.gupta |
99 |
def generateSnapDealReco():
|
| 14460 |
amit.gupta |
100 |
curs = getOrdersAfterDate(datetime.now() - timedelta(days=30), 3)
|
| 14450 |
amit.gupta |
101 |
db = client.Dtr
|
|
|
102 |
|
| 14487 |
amit.gupta |
103 |
matchedList = []
|
| 14450 |
amit.gupta |
104 |
workbook = xlwt.Workbook()
|
| 14460 |
amit.gupta |
105 |
worksheet = workbook.add_sheet("Snapdeal")
|
| 14450 |
amit.gupta |
106 |
column = 0
|
|
|
107 |
row = 0
|
| 14474 |
amit.gupta |
108 |
global i
|
|
|
109 |
i=-1
|
|
|
110 |
worksheet.write(row, inc(), 'Order Id', boldStyle)
|
|
|
111 |
worksheet.write(row, inc(), 'User Id', boldStyle)
|
|
|
112 |
worksheet.write(row, inc(), 'Username', boldStyle)
|
|
|
113 |
worksheet.write(row, inc(), 'Merchant Order Id', boldStyle)
|
|
|
114 |
worksheet.write(row, inc(), 'Product Title', boldStyle)
|
|
|
115 |
worksheet.write(row, inc(), 'Price', boldStyle)
|
|
|
116 |
worksheet.write(row, inc(), 'Quantity', boldStyle)
|
|
|
117 |
worksheet.write(row, inc(), 'Status', boldStyle)
|
|
|
118 |
worksheet.write(row, inc(), 'Detailed Status', boldStyle)
|
|
|
119 |
worksheet.write(row, inc(), 'Cashback Status', boldStyle)
|
|
|
120 |
worksheet.write(row, inc(), 'Our CashBack', boldStyle)
|
|
|
121 |
worksheet.write(row, inc(), 'Sale Date', boldStyle)
|
|
|
122 |
worksheet.write(row, inc(), 'SubtagId', boldStyle)
|
|
|
123 |
worksheet.write(row, inc(), 'Ad Id', boldStyle)
|
|
|
124 |
worksheet.write(row, inc(), 'Affiliate PayOut', boldStyle)
|
|
|
125 |
worksheet.write(row, inc(), 'Affiliate Sale Amount', boldStyle)
|
|
|
126 |
worksheet.write(row, inc(), 'Affiliate Sale Date', boldStyle)
|
| 14450 |
amit.gupta |
127 |
|
|
|
128 |
for row1 in curs:
|
|
|
129 |
orderId = row1[0]
|
|
|
130 |
order = db.merchantOrder.find_one({"orderId":orderId})
|
|
|
131 |
if order is None:
|
|
|
132 |
continue
|
|
|
133 |
print order["subTagId"]
|
|
|
134 |
saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
|
|
|
135 |
aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
|
| 14487 |
amit.gupta |
136 |
gotCashBack = False
|
| 14488 |
amit.gupta |
137 |
if len(aff) > 0 and order["subTagId"] not in matchedList:
|
| 14487 |
amit.gupta |
138 |
gotCashBack = True
|
|
|
139 |
|
| 14474 |
amit.gupta |
140 |
for subOrder in order['subOrders']:
|
|
|
141 |
i=-1
|
| 14450 |
amit.gupta |
142 |
row += 1
|
| 14474 |
amit.gupta |
143 |
worksheet.write(row, inc(), orderId)
|
|
|
144 |
worksheet.write(row, inc(), row1[1])
|
|
|
145 |
worksheet.write(row, inc(), row1[-1])
|
|
|
146 |
worksheet.write(row, inc(), order['merchantOrderId'])
|
|
|
147 |
worksheet.write(row, inc(), subOrder['productTitle'])
|
|
|
148 |
worksheet.write(row, inc(), subOrder['amountPaid'])
|
|
|
149 |
worksheet.write(row, inc(), subOrder['quantity'])
|
|
|
150 |
worksheet.write(row, inc(), subOrder['status'])
|
|
|
151 |
worksheet.write(row, inc(), subOrder['detailedStatus'])
|
|
|
152 |
worksheet.write(row, inc(), subOrder['cashBackStatus'])
|
|
|
153 |
worksheet.write(row, inc(), subOrder['cashBackAmount'])
|
|
|
154 |
worksheet.write(row, inc(), subOrder['placedOn'])
|
|
|
155 |
worksheet.write(row, inc(), order['subTagId'])
|
| 14487 |
amit.gupta |
156 |
if gotCashBack:
|
| 14477 |
amit.gupta |
157 |
print saleTime -aff[0]['saleTime']
|
| 14474 |
amit.gupta |
158 |
worksheet.write(row, inc(), aff[0]['adId'])
|
|
|
159 |
worksheet.write(row, inc(), aff[0]['payOut'])
|
|
|
160 |
worksheet.write(row, inc(), aff[0]['saleAmount'])
|
|
|
161 |
worksheet.write(row, inc(), aff[0]['saleDate'])
|
| 14460 |
amit.gupta |
162 |
workbook.save(XLS_FILENAME)
|
| 14450 |
amit.gupta |
163 |
|
|
|
164 |
def sendmail(email, message, title):
|
|
|
165 |
if email == "":
|
|
|
166 |
return
|
|
|
167 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
168 |
mailServer.ehlo()
|
|
|
169 |
mailServer.starttls()
|
|
|
170 |
mailServer.ehlo()
|
|
|
171 |
|
|
|
172 |
# Create the container (outer) email message.
|
|
|
173 |
msg = MIMEMultipart()
|
|
|
174 |
msg['Subject'] = title
|
|
|
175 |
msg.preamble = title
|
|
|
176 |
html_msg = MIMEText(message, 'html')
|
|
|
177 |
msg.attach(html_msg)
|
|
|
178 |
|
|
|
179 |
#snapdeal more to be added here
|
|
|
180 |
snapdeal = MIMEBase('application', 'vnd.ms-excel')
|
| 14460 |
amit.gupta |
181 |
snapdeal.set_payload(file(XLS_FILENAME).read())
|
| 14450 |
amit.gupta |
182 |
encoders.encode_base64(snapdeal)
|
| 14460 |
amit.gupta |
183 |
snapdeal.add_header('Content-Disposition', 'attachment;filename=' + XLS_FILENAME)
|
| 14450 |
amit.gupta |
184 |
msg.attach(snapdeal)
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
email.append('amit.gupta@shop2020.in')
|
|
|
188 |
MAILTO = email
|
|
|
189 |
mailServer.login(SENDER, PASSWORD)
|
|
|
190 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
191 |
|
|
|
192 |
|
| 14460 |
amit.gupta |
193 |
|
|
|
194 |
def inc():
|
|
|
195 |
global i
|
|
|
196 |
i+=1
|
|
|
197 |
return i
|
|
|
198 |
|
| 14450 |
amit.gupta |
199 |
def main():
|
|
|
200 |
generateSnapDealReco()
|
| 14460 |
amit.gupta |
201 |
generateFlipkartReco()
|
| 14452 |
amit.gupta |
202 |
sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
|
| 14450 |
amit.gupta |
203 |
|
|
|
204 |
if __name__ == '__main__':
|
| 14460 |
amit.gupta |
205 |
main()
|
|
|
206 |
|