| 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
|
|
|
13 |
import MySQLdb
|
|
|
14 |
import smtplib
|
|
|
15 |
import time
|
|
|
16 |
import xlwt
|
|
|
17 |
|
|
|
18 |
client = MongoClient('mongodb://localhost:27017/')
|
|
|
19 |
|
|
|
20 |
SENDER = "cnc.center@shop2020.in"
|
|
|
21 |
PASSWORD = "5h0p2o2o"
|
|
|
22 |
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
|
|
|
23 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
24 |
SMTP_PORT = 587
|
|
|
25 |
|
|
|
26 |
def generateFlipkartReco():
|
|
|
27 |
pass
|
|
|
28 |
def generateSnapDealReco():
|
|
|
29 |
curs = getOrdersAfterDate(datetime.now() - timedelta(days=10), 3)
|
|
|
30 |
db = client.Dtr
|
|
|
31 |
|
|
|
32 |
workbook = xlwt.Workbook()
|
|
|
33 |
worksheet = workbook.add_sheet("Snapdeal Reco")
|
|
|
34 |
boldStyle = xlwt.XFStyle()
|
|
|
35 |
f = xlwt.Font()
|
|
|
36 |
f.bold = True
|
|
|
37 |
boldStyle.font = f
|
|
|
38 |
column = 0
|
|
|
39 |
row = 0
|
|
|
40 |
|
|
|
41 |
worksheet.write(row, 0, 'Order Id', boldStyle)
|
| 14453 |
amit.gupta |
42 |
worksheet.write(row, 1, 'User Id', boldStyle)
|
|
|
43 |
worksheet.write(row, 2, 'Merchant Order Id', boldStyle)
|
|
|
44 |
worksheet.write(row, 3, 'Product Title', boldStyle)
|
|
|
45 |
worksheet.write(row, 4, 'Price', boldStyle)
|
|
|
46 |
worksheet.write(row, 5, 'Quantity', boldStyle)
|
|
|
47 |
worksheet.write(row, 6, 'Status', boldStyle)
|
|
|
48 |
worksheet.write(row, 7, 'Status', boldStyle)
|
|
|
49 |
worksheet.write(row, 8, 'Cashback Status', boldStyle)
|
|
|
50 |
worksheet.write(row, 9, 'Our CashBack', boldStyle)
|
|
|
51 |
worksheet.write(row, 10, 'Sale Date', boldStyle)
|
|
|
52 |
worksheet.write(row, 11, 'SubtagId', boldStyle)
|
|
|
53 |
worksheet.write(row, 12, 'Ad Id', boldStyle)
|
|
|
54 |
worksheet.write(row, 13, 'Affiliate PayOut', boldStyle)
|
|
|
55 |
worksheet.write(row, 14, 'Affiliate Sale Amount', boldStyle)
|
|
|
56 |
worksheet.write(row, 15, 'Affiliate Sale Date', boldStyle)
|
| 14450 |
amit.gupta |
57 |
|
|
|
58 |
for row1 in curs:
|
|
|
59 |
orderId = row1[0]
|
|
|
60 |
order = db.merchantOrder.find_one({"orderId":orderId})
|
|
|
61 |
if order is None:
|
|
|
62 |
continue
|
|
|
63 |
print order["subTagId"]
|
|
|
64 |
print order
|
|
|
65 |
saleTime = int(time.mktime(datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p").timetuple()))
|
|
|
66 |
saleTime1 = int(time.mktime((datetime.strptime(order['placedOn'], "%b %d, %Y, %I:%M %p") + timedelta(seconds=-60)).timetuple()))
|
|
|
67 |
aff = list(db.snapdealOrderAffiliateInfo.find({"subTagId":order["subTagId"]}))
|
|
|
68 |
|
|
|
69 |
for subOrder in order['subOrders']:
|
|
|
70 |
row += 1
|
|
|
71 |
worksheet.write(row, 0, orderId)
|
| 14453 |
amit.gupta |
72 |
worksheet.write(row, 1, row1[1])
|
|
|
73 |
worksheet.write(row, 2, order['merchantOrderId'])
|
|
|
74 |
worksheet.write(row, 3, subOrder['productTitle'])
|
|
|
75 |
worksheet.write(row, 4, subOrder['amountPaid'])
|
|
|
76 |
worksheet.write(row, 5, subOrder['quantity'])
|
|
|
77 |
worksheet.write(row, 6, subOrder['status'])
|
|
|
78 |
worksheet.write(row, 7, subOrder['detailedStatus'])
|
|
|
79 |
worksheet.write(row, 8, subOrder['cashBackStatus'])
|
|
|
80 |
worksheet.write(row, 9, subOrder['cashBackAmount'])
|
|
|
81 |
worksheet.write(row, 10, subOrder['placedOn'])
|
|
|
82 |
worksheet.write(row, 11, order['subTagId'])
|
| 14450 |
amit.gupta |
83 |
if len(aff)>0:
|
| 14453 |
amit.gupta |
84 |
worksheet.write(row, 12, aff[0]['adId'])
|
|
|
85 |
worksheet.write(row, 13, aff[0]['payOut'])
|
|
|
86 |
worksheet.write(row, 14, aff[0]['saleAmount'])
|
|
|
87 |
worksheet.write(row, 15, aff[0]['saleDate'])
|
| 14450 |
amit.gupta |
88 |
workbook.save("snapdealreco.xls")
|
|
|
89 |
|
|
|
90 |
def sendmail(email, message, title):
|
|
|
91 |
if email == "":
|
|
|
92 |
return
|
|
|
93 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
94 |
mailServer.ehlo()
|
|
|
95 |
mailServer.starttls()
|
|
|
96 |
mailServer.ehlo()
|
|
|
97 |
|
|
|
98 |
# Create the container (outer) email message.
|
|
|
99 |
msg = MIMEMultipart()
|
|
|
100 |
msg['Subject'] = title
|
|
|
101 |
msg.preamble = title
|
|
|
102 |
html_msg = MIMEText(message, 'html')
|
|
|
103 |
msg.attach(html_msg)
|
|
|
104 |
|
|
|
105 |
#snapdeal more to be added here
|
|
|
106 |
snapdeal = MIMEBase('application', 'vnd.ms-excel')
|
|
|
107 |
snapdeal.set_payload(file("snapdealreco.xls").read())
|
|
|
108 |
encoders.encode_base64(snapdeal)
|
|
|
109 |
snapdeal.add_header('Content-Disposition', 'attachment;filename=snapdealreco.xls')
|
|
|
110 |
msg.attach(snapdeal)
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
email.append('amit.gupta@shop2020.in')
|
|
|
114 |
MAILTO = email
|
|
|
115 |
mailServer.login(SENDER, PASSWORD)
|
|
|
116 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
def main():
|
|
|
120 |
generateSnapDealReco()
|
| 14452 |
amit.gupta |
121 |
sendmail(["amit.gupta@shop2020.in"], "", "DTR Affiliate Reco")
|
| 14450 |
amit.gupta |
122 |
|
|
|
123 |
if __name__ == '__main__':
|
|
|
124 |
main()
|