| 5087 |
anupam.sin |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
import json
|
|
|
4 |
import urllib2, cookielib
|
|
|
5 |
import MySQLdb
|
|
|
6 |
import datetime
|
|
|
7 |
import sys
|
|
|
8 |
import smtplib
|
|
|
9 |
|
|
|
10 |
from email import encoders
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
from email.mime.base import MIMEBase
|
|
|
13 |
from email.mime.multipart import MIMEMultipart
|
|
|
14 |
from pyExcelerator import Workbook, Font, XFStyle
|
|
|
15 |
from datetime import date
|
|
|
16 |
|
|
|
17 |
from datetime import datetime, timedelta
|
|
|
18 |
|
|
|
19 |
# Initialize db connection settings.
|
|
|
20 |
DB_HOST = "localhost"
|
|
|
21 |
DB_USER = "root"
|
|
|
22 |
DB_PASSWORD = "shop2020"
|
|
|
23 |
DB_NAME = "sales"
|
|
|
24 |
|
|
|
25 |
# KEY NAMES
|
| 5301 |
anupam.sin |
26 |
MAILTO = ['abhishek.mathur@shop2020.in', 'sitakanta@mysmartprice.com', 'anupam.singh@shop2020.in']
|
| 5087 |
anupam.sin |
27 |
SENDER = "cnc.center@shop2020.in"
|
|
|
28 |
PASSWORD = "5h0p2o2o"
|
|
|
29 |
SUBJECT = "MySmartPrice Report"
|
|
|
30 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
31 |
SMTP_PORT = 587
|
|
|
32 |
|
|
|
33 |
TMP_FILE="/tmp/mysmartprice_report.xls"
|
|
|
34 |
|
|
|
35 |
def getDbConnection():
|
|
|
36 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
37 |
|
|
|
38 |
def closeConnection(conn):
|
|
|
39 |
conn.close()
|
|
|
40 |
|
|
|
41 |
def getCount(dateYesterday):
|
|
|
42 |
selectCount = "select count(*) from productactivity where date_id = (select date_id from datedim where fulldate = '" + dateYesterday + "')"
|
|
|
43 |
|
|
|
44 |
conn = getDbConnection()
|
|
|
45 |
try:
|
|
|
46 |
# prepare a cursor object using cursor() method
|
|
|
47 |
cursor = conn.cursor()
|
|
|
48 |
# Execute the SQL command
|
|
|
49 |
# Fetch source id.
|
|
|
50 |
cursor.execute(selectCount)
|
|
|
51 |
result = cursor.fetchall()
|
|
|
52 |
for r in result:
|
|
|
53 |
count = r[0]
|
|
|
54 |
|
|
|
55 |
except Exception as e:
|
|
|
56 |
print "Error: unable to fetch data"
|
|
|
57 |
print e
|
|
|
58 |
|
|
|
59 |
return count
|
|
|
60 |
|
|
|
61 |
def getProductSaleData():
|
|
|
62 |
selectSql = '''select concat_ws(' ', catalog_item.brand, catalog_item.model_name, catalog_item.model_number) as Product,
|
|
|
63 |
sum(p.unique_view) as Views, sum(p.unique_add_to_cart) as AddToCarts
|
|
|
64 |
from productactivity p
|
|
|
65 |
join catalog_item catalog_item on (catalog_item.catalog_item_id = p.catalog_item_id)
|
|
|
66 |
where date_id = (select MAX(date_id) from productactivity) and session_source_id in (980) group by p.catalog_item_id
|
|
|
67 |
'''
|
|
|
68 |
conn = getDbConnection()
|
|
|
69 |
productTuples = []
|
|
|
70 |
|
|
|
71 |
try:
|
|
|
72 |
# prepare a cursor object using cursor() method
|
|
|
73 |
cursor = conn.cursor()
|
|
|
74 |
# Execute the SQL command
|
|
|
75 |
# Fetch source id.
|
|
|
76 |
cursor.execute(selectSql)
|
|
|
77 |
result = cursor.fetchall()
|
|
|
78 |
for r in result:
|
|
|
79 |
productTuple = r[0:3]
|
|
|
80 |
productTuples.append(productTuple)
|
|
|
81 |
|
|
|
82 |
except Exception as e:
|
|
|
83 |
print "Error: unable to fetch data"
|
|
|
84 |
print e
|
|
|
85 |
|
|
|
86 |
return productTuples
|
|
|
87 |
|
| 5091 |
anupam.sin |
88 |
def createXlsReport(productTuples, dateYesterday):
|
| 5087 |
anupam.sin |
89 |
workbook = Workbook()
|
|
|
90 |
worksheet = workbook.add_sheet("Sheet 1")
|
|
|
91 |
boldStyle = XFStyle()
|
|
|
92 |
f = Font()
|
|
|
93 |
f.bold = True
|
|
|
94 |
boldStyle.font = f
|
|
|
95 |
|
|
|
96 |
timeNow = datetime.now()
|
|
|
97 |
worksheet.write(0, 0, dateYesterday, boldStyle)
|
|
|
98 |
row = 3
|
|
|
99 |
|
|
|
100 |
worksheet.write(row, 0, 'Product', boldStyle)
|
|
|
101 |
worksheet.write(row, 1, 'Views', boldStyle)
|
|
|
102 |
worksheet.write(row, 2, 'Add to Carts', boldStyle)
|
|
|
103 |
|
|
|
104 |
for productTuple in productTuples:
|
|
|
105 |
row += 1
|
|
|
106 |
worksheet.write(row, 0, productTuple[0])
|
|
|
107 |
worksheet.write(row, 1, str(productTuple[1]))
|
|
|
108 |
worksheet.write(row, 2, str(productTuple[2]))
|
|
|
109 |
|
|
|
110 |
workbook.save(TMP_FILE)
|
|
|
111 |
print "Spreadsheet Saved"
|
|
|
112 |
|
| 5091 |
anupam.sin |
113 |
def sendmail(dateYesterday):
|
| 5087 |
anupam.sin |
114 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
115 |
mailServer.ehlo()
|
|
|
116 |
mailServer.starttls()
|
|
|
117 |
mailServer.ehlo()
|
|
|
118 |
|
|
|
119 |
# Create the container (outer) email message.
|
|
|
120 |
msg = MIMEMultipart()
|
|
|
121 |
msg['Subject'] = SUBJECT + ' - ' + dateYesterday
|
|
|
122 |
msg['From'] = "bi@saholic.com"
|
|
|
123 |
msg['To'] = 'sku-recipients@saholic.com'
|
|
|
124 |
msg.preamble = SUBJECT + ' - ' + date.today().isoformat()
|
|
|
125 |
|
|
|
126 |
fileMsg = MIMEBase('application','vnd.ms-excel')
|
|
|
127 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
128 |
encoders.encode_base64(fileMsg)
|
|
|
129 |
fileMsg.add_header('Content-Disposition','attachment;filename=MySmartPrice-Report' + ' - ' + dateYesterday + '.xls')
|
|
|
130 |
msg.attach(fileMsg)
|
|
|
131 |
|
|
|
132 |
mailServer.login(SENDER, PASSWORD)
|
|
|
133 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
134 |
print "Mail Sent"
|
|
|
135 |
|
|
|
136 |
def main():
|
|
|
137 |
dateYesterday = str((datetime.now() - timedelta(days=1)).date())
|
|
|
138 |
count = getCount(dateYesterday)
|
|
|
139 |
if count == 0:
|
|
|
140 |
print "No data found for date : " + dateYesterday
|
|
|
141 |
exit(1)
|
|
|
142 |
|
|
|
143 |
productTuples = getProductSaleData()
|
|
|
144 |
print "Number of tuples on " + dateYesterday + " : " + str(len(productTuples))
|
|
|
145 |
createXlsReport(productTuples, dateYesterday)
|
| 5091 |
anupam.sin |
146 |
sendmail(dateYesterday)
|
| 5087 |
anupam.sin |
147 |
|
|
|
148 |
if __name__ == '__main__':
|
|
|
149 |
main()
|