| 16356 |
manas |
1 |
import MySQLdb
|
|
|
2 |
import xlwt
|
|
|
3 |
from email import encoders
|
|
|
4 |
from email.mime.base import MIMEBase
|
|
|
5 |
from email.mime.multipart import MIMEMultipart
|
|
|
6 |
from xlrd import open_workbook
|
|
|
7 |
from xlutils.copy import copy
|
|
|
8 |
import smtplib
|
|
|
9 |
from dtr.storage.DataService import brand_preferences
|
|
|
10 |
|
|
|
11 |
DB_HOST = "localhost"
|
|
|
12 |
DB_USER = "root"
|
|
|
13 |
DB_PASSWORD = "shop2020"
|
|
|
14 |
DB_NAME = "dtr"
|
|
|
15 |
#TMP_FILE = "/home/kshitij/Downloads/User_Segmentation_Links.xls"
|
|
|
16 |
TMP_FILE = "User_Segmentation_Links.xls"
|
|
|
17 |
|
|
|
18 |
SENDER = "cnc.center@shop2020.in"
|
|
|
19 |
PASSWORD = "5h0p2o2o"
|
|
|
20 |
SUBJECT = "User Segmentation Report"
|
|
|
21 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
22 |
SMTP_PORT = 587
|
|
|
23 |
|
|
|
24 |
date_format = xlwt.XFStyle()
|
|
|
25 |
date_format.num_format_str = 'yyyy/mm/dd'
|
|
|
26 |
|
|
|
27 |
datetime_format = xlwt.XFStyle()
|
|
|
28 |
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
|
|
|
29 |
|
|
|
30 |
default_format = xlwt.XFStyle()
|
|
|
31 |
|
|
|
32 |
USER_SEGMENTATION_LINK_QUERY="""
|
| 16359 |
manas |
33 |
select x.user_id,x.brand,x.category_id,x.count,x.source,x.weight
|
| 16356 |
manas |
34 |
from
|
|
|
35 |
(
|
| 16359 |
manas |
36 |
select c.user_id,c.brand,c.category_id,count(c.id) as count,"clicks" as source,(uw.weightage * count(c.id)) as weight
|
| 16356 |
manas |
37 |
from clicks c join user_segmentation_weigthage uw on "clicks"=uw.weightage_type
|
|
|
38 |
where c.category_id is not null and c.category_id !=0 and c.brand is not null and
|
| 16359 |
manas |
39 |
c.brand not like '' and c.user_id!=0 group by c.category_id,c.brand,c.user_id
|
| 16356 |
manas |
40 |
|
|
|
41 |
UNION
|
| 16359 |
manas |
42 |
|
|
|
43 |
select m.userId as user_id,m.brand,m.categoryId as category_id,count(1) as count,"orders" as source,(uw.weightage * count(1)) as weight
|
|
|
44 |
from merchantsuborders m join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
|
|
45 |
where m.categoryId is not null and m.brand is not null and m.userId!=0 group by m.categoryId,m.brand,m.userId
|
| 16356 |
manas |
46 |
|
|
|
47 |
UNION
|
| 16359 |
manas |
48 |
|
|
|
49 |
select fo.user_id,fo.brand,fo.category as category_id,count(fo.id) as count,"orders" as source, (uw.weightage * count(fo.id)) as weight
|
| 16356 |
manas |
50 |
from flipkartorders fo join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
| 16359 |
manas |
51 |
where fo.category is not null and fo.brand is not null and fo.brand not like '' and fo.user_id!=0 and fo.user_id is not null group by fo.category,fo.brand,fo.user_id
|
|
|
52 |
|
| 16356 |
manas |
53 |
UNION
|
| 16359 |
manas |
54 |
|
| 16360 |
manas |
55 |
select bp.user_id,bp.brand,bp.category_id,count(1) as count,"preferences" as source,(uw.weightage* count(1)) as weight
|
| 16359 |
manas |
56 |
from brand_preferences bp join user_segmentation_weigthage uw on "preferences"=uw.weightage_type
|
| 16361 |
manas |
57 |
where user_id!=0 and status='show' group by bp.category_id,bp.brand,bp.user_id
|
| 16359 |
manas |
58 |
) as x
|
|
|
59 |
|
| 16356 |
manas |
60 |
"""
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
def getDbConnection():
|
|
|
64 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
65 |
|
|
|
66 |
def generateUserSegmentationLinksReport():
|
|
|
67 |
datesql= USER_SEGMENTATION_LINK_QUERY
|
|
|
68 |
conn = getDbConnection()
|
|
|
69 |
|
|
|
70 |
global workbook
|
|
|
71 |
workbook = xlwt.Workbook()
|
|
|
72 |
|
|
|
73 |
worksheet = workbook.add_sheet("User Segmentation")
|
|
|
74 |
boldStyle = xlwt.XFStyle()
|
|
|
75 |
|
|
|
76 |
style = xlwt.XFStyle()
|
|
|
77 |
pattern = xlwt.Pattern()
|
|
|
78 |
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
|
|
|
79 |
pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
|
|
|
80 |
style.pattern = pattern
|
|
|
81 |
f = xlwt.Font()
|
|
|
82 |
f.bold = True
|
|
|
83 |
boldStyle.font = f
|
|
|
84 |
|
|
|
85 |
column = 0
|
|
|
86 |
row = 0
|
|
|
87 |
worksheet.write(row,column,'User Id',style)
|
|
|
88 |
worksheet.write(row,column+1,'Product Name',style)
|
|
|
89 |
worksheet.write(row,column+2,'Brand',style)
|
|
|
90 |
worksheet.write(row,column+3,'Category Id',style)
|
|
|
91 |
worksheet.write(row,column+4,'Count',style)
|
|
|
92 |
worksheet.write(row,column+5,'Source',style)
|
|
|
93 |
worksheet.write(row,column+6,'Weight',style)
|
|
|
94 |
|
|
|
95 |
cursor = conn.cursor()
|
|
|
96 |
cursor.execute(datesql)
|
|
|
97 |
result = cursor.fetchall()
|
|
|
98 |
|
|
|
99 |
for r in result:
|
|
|
100 |
row=row+1
|
|
|
101 |
worksheet.write(row,column,r[0])
|
|
|
102 |
worksheet.write(row,column+1,r[1])
|
|
|
103 |
worksheet.write(row,column+2,r[2])
|
|
|
104 |
worksheet.write(row,column+3,r[3])
|
|
|
105 |
worksheet.write(row,column+4,r[4])
|
|
|
106 |
worksheet.write(row,column+5,r[5])
|
|
|
107 |
worksheet.write(row,column+6,r[6])
|
|
|
108 |
conn.close()
|
|
|
109 |
workbook.save(TMP_FILE)
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
def addUsersForLinks():
|
|
|
113 |
user_id=''
|
|
|
114 |
brand=''
|
|
|
115 |
category_id=''
|
|
|
116 |
source=''
|
|
|
117 |
count=''
|
|
|
118 |
weight=''
|
|
|
119 |
|
|
|
120 |
datesql= USER_SEGMENTATION_LINK_QUERY
|
|
|
121 |
conn = getDbConnection()
|
|
|
122 |
cursor = conn.cursor()
|
|
|
123 |
cursor.execute(datesql)
|
|
|
124 |
result = cursor.fetchall()
|
|
|
125 |
|
|
|
126 |
for r in result:
|
|
|
127 |
user_id=r[0]
|
| 16359 |
manas |
128 |
brand=r[1]
|
|
|
129 |
if r[2] == 'Mobiles':
|
| 16356 |
manas |
130 |
category_id=3
|
| 16359 |
manas |
131 |
elif r[2] == 'Tablets':
|
| 16356 |
manas |
132 |
category_id=5
|
|
|
133 |
else:
|
| 16359 |
manas |
134 |
category_id=r[2]
|
| 16362 |
manas |
135 |
count=r[3]
|
|
|
136 |
source=r[4]
|
| 16359 |
manas |
137 |
weight=r[5]
|
| 16356 |
manas |
138 |
|
| 16359 |
manas |
139 |
sql = "insert into userlinkssegmentation (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
|
|
140 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
| 16356 |
manas |
141 |
conn.commit()
|
|
|
142 |
|
|
|
143 |
conn.close()
|
|
|
144 |
|
|
|
145 |
def deleteUserForLinks():
|
|
|
146 |
conn = getDbConnection()
|
|
|
147 |
cursor = conn.cursor()
|
|
|
148 |
deleteAllUserGroups = "delete from userlinkssegmentation"
|
|
|
149 |
try:
|
|
|
150 |
cursor.execute(deleteAllUserGroups)
|
|
|
151 |
conn.commit()
|
|
|
152 |
except:
|
|
|
153 |
conn.rollback()
|
|
|
154 |
finally:
|
|
|
155 |
conn.close()
|
|
|
156 |
|
|
|
157 |
def main():
|
|
|
158 |
#generateUserSegmentationLinksReport()
|
|
|
159 |
deleteUserForLinks()
|
|
|
160 |
addUsersForLinks()
|
|
|
161 |
|
|
|
162 |
def sendmail(email, message, fileName, title):
|
|
|
163 |
if email == "":
|
|
|
164 |
return
|
|
|
165 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
166 |
mailServer.ehlo()
|
|
|
167 |
mailServer.starttls()
|
|
|
168 |
mailServer.ehlo()
|
|
|
169 |
msg = MIMEMultipart()
|
|
|
170 |
msg['Subject'] = title
|
|
|
171 |
msg.preamble = title
|
|
|
172 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
|
|
173 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
174 |
encoders.encode_base64(fileMsg)
|
|
|
175 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
|
|
176 |
msg.attach(fileMsg)
|
|
|
177 |
MAILTO = ['manas.kapoor@saholic.com']
|
|
|
178 |
mailServer.login(SENDER, PASSWORD)
|
|
|
179 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
if __name__=='__main__':
|
| 16359 |
manas |
183 |
main()
|