-
Notifications
You must be signed in to change notification settings - Fork 6
/
dailyReport.py
561 lines (484 loc) · 18.3 KB
/
dailyReport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText
from Config import MAILPASS, NO_REPLY_TATATIMES_COM, SMTP_EXMAIL_QQ_COM, receivers, MANAGER_TATATIMES_COM, \
NOREPLAYEMAIL
from networkHelper import getERAConn, getEADConn
yesteday = time.strftime('%Y%m%d', time.localtime(time.time() - 24 * 3600))
# yesteday = time.strftime('%Y%m%d', time.localtime(time.time()))
def bossconsume():
csorEAD, connEAD = getEADConn()
csorERA, connERA = getERAConn()
#所有广告创意信息
adObjs = dict()
#所有广告主信息
advertisers = dict()
#所有广告位信息
adunits = dict()
# csorERA.excute('select ')
# 查广告、及所属广告主、及所投广告位名称及媒体名称
csorERA.execute('select contentId, title, adunitid,advertiserId,advertiser.name,adslot.mapName, adsponsor.name '
'from adsdkcontent,advertiser,adslot,adsponsor where adsdkcontent.advertiserId = advertiser.sponsorId '
'and adsdkcontent.adunitid = adslot.slotUdid and adsponsor.sponsorId = adslot.sponsorId;')
connERA.commit()
contents = csorERA.fetchall()
for content in contents:
adObj = dict()
adObj['adid'] = content[0]
adObj['title'] = content[1]
adObj['adunitid'] = content[2]
adObj['advertiserId'] = content[3]
adObj['advertiserName'] = content[4]
adObj['slotName'] = content[5]
adObj['adsponsorName'] = content[6]
adObjs[content[0]] = adObj
if advertisers.has_key(adObj['advertiserId']):
adids = advertisers.get(adObj['advertiserId'])
advertisers[adObj['advertiserId']] = adids + ',' + str(adObj['adid'])
else:
advertisers[adObj['advertiserId']] = str(adObj['adid'])
#各广告商各广告位消费
consums = list()
# 在各媒体自投广告汇总
consumOnMedias = dict()
#所有统计到的广告投放情况信息
csorEAD.execute('select ad.adId as adId, sum(ad.clickcnt) as clickcnt, sum(ad.showcnt) as showcnt, ad.cdate as cdate'
', sum(acc.amount) as accAmount from ad_statistic_ad_record ad '
'left join ad_accounts_record acc on ad.id = acc.sourceId '
'where ad.sourceId=1 and ad.cdate=%s GROUP BY adId, cdate ORDER BY cdate DESC;', (yesteday, ))
connEAD.commit()
stastics = csorEAD.fetchall()
for static in stastics:
adid = static[0]
clickcnt = static[1]
showcnt = static[2]
cdate = static[3]
if not adObjs.has_key(adid):
continue
adObj = adObjs[adid]
adObj['clickcnt'] = clickcnt
adObj['showcnt'] = showcnt
adObj['cdate'] = cdate
consums.append(adObj)
if not consumOnMedias.has_key(adObj['adsponsorName']):
consumOnMd = dict()
consumOnMd['name'] = adObj['adsponsorName']
consumOnMd['clickcnt'] = clickcnt
consumOnMd['showcnt'] = showcnt
consumOnMedias[adObj['adsponsorName']] = consumOnMd
else:
consumOnMd = consumOnMedias[adObj['adsponsorName']]
consumOnMd['clickcnt'] = consumOnMd['clickcnt'] + clickcnt
consumOnMd['showcnt'] = consumOnMd['showcnt'] + showcnt
return consumOnMedias, consums
def mediaIncome():
csorERA, connERA = getERAConn()
#媒体总收入
mediaObjs = list()
csorERA.execute('select c.sponsorId, s.name, sum(c.showNum) as showNum, sum(c.clickNum) as clickNum'
', sum(c.cost) as cost, c.day as day, sum(c.reqNum) as reqNum '
' from adslot_dailycost c left join adsponsor s on c.sponsorId = s.sponsorId'
' left join adslot a on c.slotId = a.slotId where c.day = %s and showNum > 0 '
'Group BY day, a.sponsorId ORDER BY day, a.sponsorId DESC;', (yesteday, ))
connERA.commit()
contents = csorERA.fetchall()
for content in contents:
adObj = dict()
adObj['sponsorId'] = content[0]
adObj['name'] = content[1]
adObj['showcnt'] = content[2]
adObj['clickcnt'] = content[3]
adObj['cost'] = content[4]
adObj['day'] = content[5]
adObj['reqNum'] = content[6]
# adObj['sloName'] = content[5]
mediaObjs.append(adObj)
# 媒体广告位收入详情
slotIncoms = list()
#广告位详情
csorERA.execute('select c.sponsorId, sum(c.showNum) as showNum, sum(c.clickNum) as clickNum, sum(c.cost) as cost'
', c.day as day, sum(c.reqNum) as reqNum, a.mapName, c.slotId as slotId from adslot_dailycost c'
' left join adslot a on c.slotId = a.slotId where c.day = %s '
'Group BY day, a.slotId ORDER BY day,c.sponsorId DESC;', (yesteday, ))
connERA.commit()
stastics = csorERA.fetchall()
for static in stastics:
adObj = dict()
adObj['sponsorId'] = static[0]
adObj['showcnt'] = static[1]
adObj['clickcnt'] = static[2]
adObj['cost'] = static[3]
adObj['day'] = static[4]
adObj['reqNum'] = static[5]
adObj['slotName'] = static[6]
adObj['slotId'] = static[7]
slotIncoms.append(adObj)
return mediaObjs,slotIncoms
def send(msg):
mail_host= SMTP_EXMAIL_QQ_COM #设置服务器
mail_user= NO_REPLY_TATATIMES_COM #用户名
mail_pass= MAILPASS #口令
sender = NOREPLAYEMAIL
receivers = [MANAGER_TATATIMES_COM, receivers] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText(msg, 'html', 'utf-8')
message['From'] = Header("广告平台", 'utf-8')
message['To'] = Header('老大们', 'utf-8')
subject = "塔塔日报-" + yesteday
message['Subject'] = Header(subject, 'utf-8')
try:
smtp = smtplib.SMTP()
smtp.connect(mail_host)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(mail_user, mail_pass)
smtp.sendmail(sender, receivers, message.as_string())
smtp.quit()
# smtpObj = smtplib.SMTP()
# smtpObj.connect(mail_host, 465) # 25 为 SMTP 端口号
# smtpObj.login(mail_user, mail_pass)
# smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
def dailyReport():
msgHead = u'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<style>
table {
background-color: transparent;
border-collapse: collapse;
border-spacing: 0;
box-sizing: border-box;
display: table;
border-collapse: separate;
border-spacing: 2px;
border-color: grey;
}
thead {
display: table-header-group;
vertical-align: middle;
border-color: inherit;
}
.table>caption+thead>tr:first-child>th, .table>colgroup+thead>tr:first-child>th, .table>thead:first-child>tr:first-child>th, .table>caption+thead>tr:first-child>td, .table>colgroup+thead>tr:first-child>td, .table>thead:first-child>tr:first-child>td {
border-top: 0;
}
.table thead>tr>th {
border-bottom: 1px solid #ebebeb;
}
.table-striped>thead th {
background: #f5f5f5;
border-right: 1px solid #f1f1f1;
}
.table td, .table th {
padding: 6px 15px;
border-top: 1px solid #f1f1f1;
}
.table>thead>tr>th {
vertical-align: bottom;
border-bottom: 2px solid #dddddd;
}
.table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td {
padding: 8px;
line-height: 1.428571429;
vertical-align: top;
border-top: 1px solid #dddddd;
}
th {
text-align: left;
}
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
th {
font-weight: bold;
text-align: -internal-center;
}
td, th {
display: table-cell;
vertical-align: inherit;
}
.text-sm {
font-size: 12px;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
table {
display: table;
border-collapse: separate;
border-spacing: 2px;
border-color: grey;
}
@media (min-width: 768px)
.vbox {
display: table;
border-spacing: 0;
position: relative;
height: 100%;
width: 100%;
}
@media (min-width: 768px)
.hbox {
display: table;
table-layout: fixed;
border-spacing: 0;
width: 100%;
}
@media (min-width: 768px)
.vbox {
display: table;
border-spacing: 0;
position: relative;
height: 100%;
width: 100%;
}
body {
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
color: #717171;
background-color: transparent;
-webkit-font-smoothing: antialiased;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.428571429;
color: #333333;
background-color: #ffffff;
}
html {
font-size: 62.5%;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
}
}
}
</style>
<br>
<br>
<h1 style="font-size: 20px;">自投广告消费按媒体汇总</h1>
<br>
<table class="table table-striped m-b-none text-sm" style="background-color: transparent;border-collapse: collapse;border-spacing: 0;box-sizing: border-box;display: table;border-collapse: separate;border-spacing: 2px;border-color: grey;">
<thead>
<tr role="row">
<th>媒体名称</th>
<th>总点击</th>
<th>总展示</th>
<th>点击率</th>
<th>日期</th>
</tr>
</thead>
<tbody>
'''
bossConsumeOnMDTemp = u'''
<tr class="odd">
<td>媒体名称</td>
<td>总点击</td>
<td>总展示</td>
<td>点击率</td>
<td>日期</td>
</tr>
'''
mediaWholeBase = u'''
</tbody>
</table>
<br>
<br>
<h1 style="font-size: 20px;">各媒体总收入</h1>
<br>
<table class="table table-striped m-b-none text-sm">
<thead>
<tr role="row">
<th>媒体sponsorId</th>
<th>媒体名称</th>
<th>总点击</th>
<th>总展示</th>
<th>点击率</th>
<th>总请求</th>
<th>填充率</th>
<th>总收入</th>
<th>日期</th>
</tr>
</thead>
<tbody>
'''
MediaWholeIncomeTemp = u'''
<tr class="odd">
<td>媒体sponsorId</td>
<td>媒体名称</td>
<td>总点击</td>
<td>总展示</td>
<td>点击率</td>
<td>总请求</td>
<td>填充率</td>
<td>总收入</td>
<td>日期</td>
</tr>
'''
slotConsumeDeatilBase = u'''
</tbody>
</table>
<br>
<br>
<h1 style="font-size: 20px;">自投广告按广告位统计</h1>
<br>
<table class="table table-striped m-b-none text-sm">
<thead>
<tr role="row">
<th>广告位adId</th>
<th>广告名称</th>
<th>广告位</th>
<th>点击次数</th>
<th>展示次数</th>
<th>点击率</th>
<th>日期</th>
</tr>
</thead>
<tbody>
'''
slotConsumeDeatilTemp = u'''
<tr class="even">
<td>广告位adId</td>
<td>广告名称</td>
<td>广告位</td>
<td>点击次数</td>
<td>展示次数</td>
<td>点击率</td>
<td>日期</td>
</tr>
'''
slotIncomeDeatilBase = u'''
</tbody>
</table>
<br>
<br>
<h1 style="font-size: 20px;">媒体广告位收入详情</h1>
<br>
<table class="table table-striped m-b-none text-sm">
<thead>
<tr role="row">
<th>广告位adId</th>
<th>广告位名称</th>
<th>点击次数</th>
<th>展示次数</th>
<th>点击率</th>
<th>请求次数</th>
<th>填充率</th>
<th>日期</th>
</tr>
</thead>
<tbody>
'''
slotIncomeDeatilTemp = u'''
<tr class="even">
<td>广告位adId</td>
<td>广告位名称</td>
<td>点击次数</td>
<td>展示次数</td>
<td>点击率</td>
<td>请求次数</td>
<td>填充率</td>
<td>日期</td>
</tr>
'''
tail = u'''
</tbody>
</table>
</body>
</html>
'''
consumeOnMedias, consums = bossconsume()
mediaObjs, slotIncoms = mediaIncome()
mg = msgHead
# 在各媒体自投总计
for consumOnMdKey in consumeOnMedias:
consumOnMd = consumeOnMedias[consumOnMdKey]
record = bossConsumeOnMDTemp.replace(u'媒体名称', consumOnMd['name']).replace(u'总点击', str(consumOnMd['clickcnt']))\
.replace(u'总展示', str(consumOnMd['showcnt']))\
.replace(u'日期', str(yesteday))
if not consumOnMd.has_key('showcnt') or consumOnMd['showcnt'] < 1:
record = record.replace(u'点击率', str(0))
else:
record = record.replace(u'点击率', str('%.2f' % ((float(consumOnMd['clickcnt']) / float(consumOnMd['showcnt'])) * 100)) + '%')
mg = mg + record
mg = mg + mediaWholeBase
# 各媒体总收入
for consumOnMd in mediaObjs:
# consumOnMd = consumeOnMedias[consumOnMdKey]
record = MediaWholeIncomeTemp.replace(u'媒体sponsorId', str(consumOnMd['sponsorId']))\
.replace(u'媒体名称', consumOnMd['name'])\
.replace(u'总点击', str(consumOnMd['clickcnt']))\
.replace(u'总展示', str(consumOnMd['showcnt']))\
.replace(u'总请求', str(consumOnMd['reqNum']))\
.replace(u'总收入', str('%.2f'%(float(consumOnMd['cost']) / 100.0)) + u'元')\
.replace(u'日期', str(yesteday))
if not consumOnMd.has_key('showcnt') or consumOnMd['showcnt'] < 1:
record = record.replace(u'点击率', str(0))
else:
record = record.replace(u'点击率', str('%.2f' % ((float(consumOnMd['clickcnt']) / float(consumOnMd['showcnt'])) * 100)) + '%')
if not consumOnMd.has_key('reqNum') or consumOnMd['reqNum'] < 1:
record = record.replace(u'填充率', str(0))
else:
record = record.replace(u'填充率', str('%.2f' % ((float(consumOnMd['showcnt']) / float(consumOnMd['reqNum'])) * 100)) + '%')
mg = mg + record
mg = mg + slotConsumeDeatilBase
# 自投广告在广告位上投放详情
for consumOnMd in consums:
# consumOnMd = consumeOnMedias[consumOnMdKey]
record = slotConsumeDeatilTemp.replace(u'广告位adId', str(consumOnMd['adid']))\
.replace(u'广告名称', consumOnMd['title'])\
.replace(u'广告位', consumOnMd['slotName'])\
.replace(u'点击次数', str(consumOnMd['clickcnt']))\
.replace(u'展示次数', str(consumOnMd['showcnt']))\
.replace(u'日期', str(yesteday))
if not consumOnMd.has_key('showcnt') or consumOnMd['showcnt'] < 1:
record = record.replace(u'点击率', str(0))
else:
record = record.replace(u'点击率', str('%.2f' % ((float(consumOnMd['clickcnt']) / float(consumOnMd['showcnt'])) * 100)) + '%')
mg = mg + record
mg = mg + slotIncomeDeatilBase
# 广告位收入详情
for consumOnMd in slotIncoms:
# consumOnMd = consumeOnMedias[consumOnMdKey]
if consumOnMd['showcnt'] < 1:
continue
record = slotIncomeDeatilTemp.replace(u'广告位adId', str(consumOnMd['slotId'])) \
.replace(u'广告位名称', consumOnMd['slotName']) \
.replace(u'点击次数', str(consumOnMd['clickcnt'])) \
.replace(u'展示次数', str(consumOnMd['showcnt'])) \
.replace(u'请求次数', str(consumOnMd['reqNum'])) \
.replace(u'日期', str(yesteday))
if not consumOnMd.has_key('showcnt') or consumOnMd['showcnt'] < 1:
record = record.replace(u'点击率', str(0))
else:
record = record.replace(u'点击率', str('%.2f' % ((float(consumOnMd['clickcnt']) / float(consumOnMd['showcnt'])) * 100)) + '%')
if not consumOnMd.has_key('reqNum') or consumOnMd['reqNum'] < 1:
record = record.replace(u'填充率', str(0))
else:
record = record.replace(u'填充率', str('%.2f' % ((float(consumOnMd['showcnt']) / float(consumOnMd['reqNum'])) * 100)) + '%')
mg = mg + record
mg = mg + tail
send(mg)
if __name__ == '__main__':
dailyReport()
# produceBookSuggest()