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
import elasticsearch
import smtplib
from email.message import EmailMessage
import datetime
import sys
es = elasticsearch.Elasticsearch(hosts='192.168.17.13', port=9200)
hosts = '192.168.17.11'
port = 80
user = 'zhouyujiang'
passwd = '8tM9JDN2LVxM'
http_auth = (user, passwd)
es = elasticsearch.Elasticsearch(hosts=hosts, port=port, http_auth=http_auth)
def releaser_fans_alert(st, et):
platform_list = ("toutiao", "new_tudou", "腾讯视频", "haokan", "网易新闻", "腾讯新闻", "kwai", "抖音", "weibo")
fans_text = ""
for platform in platform_list:
search_body = {
"query": {
"bool": {
"filter": [
{"term": {"platform.keyword": platform}},
{"range": {"fetch_time": {"gte": st, "lt": et}}}
]
}
}
}
search_resp = es.search(index="releaser_fans",
doc_type="doc",
body=search_body,
request_timeout=100)
video_num = search_resp['hits']['total']
fans_text += "平台 "+ platform + " 昨日抓取粉丝量条目数为 %s\n"% video_num
return fans_text
def videonumber_alert_for_ccr(days_from_running, f_log=None):
video_num_criteria = {
'short-video': {
'observed': {
'toutiao': 15000000,
'new_tudou': 1500000,
'腾讯视频': 4000000,
# 'iqiyi':300,
# 'youku':2000,
'haokan': 1200000,
"网易新闻": 7000000,
'腾讯新闻': 300000,
"kwai": 150000,
"抖音":150000,
},
'new_released': {
'toutiao': 30000,
'new_tudou': 30000,
'腾讯视频': 30000,
# 'iqiyi': 100,
# 'youku': 100,
'haokan': 17000,
'腾讯新闻': 6000,
"网易新闻": 5000,
"kwai": 5000,
"抖音": 1500,
}
}
}
email_group = {
'short-video': [
'zhouyujiang@csm.com.cn',
'hanye@csm.com.cn',
"litao@csm.com.cn",
"gengdi@csm.com.cn"
]
}
email_msg_suffix = ('\n\n\n'
+ '-' * 80 + '\n'
+ '这是自动发送的邮件,可以不用回复。\n'
+ 'This is an automatically sent message. You do NOT need to reply.\n')
index_short_video = 'crawler-data-raw'
doc_type_short_video = 'doc'
idx_dict = {
'short-video': {
'index': index_short_video,
'doc_type': doc_type_short_video,
}
}
p = 86400000
# crr = 10800000
today = datetime.datetime.now() - datetime.timedelta(days=days_from_running)
t = datetime.datetime(today.year, today.month, today.day)
# print(t)
stamp = int(t.timestamp() * 1000)
# print(stamp)
if f_log == None:
path = '/home/hanye/project_data/Python/Projects/proj-short-videos/maintenance/log/'
log_fn = 'email_alert_for_craw_%s_log' % datetime.datetime.strftime(today, '%b-%Y')
f_log = open(path + log_fn, 'a')
else:
f_log = sys.stdout
print('*' * 80, file=f_log)
print('log timestamp ', datetime.datetime.now(), file=f_log)
print('Checking video number for fetch_date', today.isoformat()[:10], file=f_log)
alert_msg = {}
for idx in video_num_criteria:
alert_msg[idx] = {}
for stats_type in ['observed']:
alert_msg[idx][stats_type] = []
for platform in video_num_criteria[idx][stats_type]:
if stats_type == 'observed':
search_body = {
"query": {
"bool": {
"filter": [
{"term": {"platform.keyword": platform}},
{"range": {"fetch_time": {"gte": stamp, "lt": stamp + p}}}
]
}
}
}
else:
search_body = {
"query": {
"bool": {
"filter": [
{"term": {"platform.keyword": platform}},
{"range": {"fetch_time": {"gte": stamp, "lt": stamp + p}}},
{"range": {"release_time": {"gte": stamp - p, "lt": stamp}}}
]
}
}
}
search_resp = es.search(index=idx_dict[idx]['index'],
doc_type=idx_dict[idx]['doc_type'],
body=search_body,
request_timeout=100)
video_num = search_resp['hits']['total']
fetch_date_str = today.isoformat()[:10]
chk_source = idx_dict[idx]['index']
if video_num < video_num_criteria[idx][stats_type][platform]:
msg = {'fetch_date': fetch_date_str,
'platform': platform,
'video_num': video_num,
'alert_criteria': video_num_criteria[idx][stats_type][platform],
'short_perct': (video_num_criteria[idx][stats_type][platform] - video_num) /
video_num_criteria[idx][stats_type][platform] * 100,
'index': idx,
'chk_source': chk_source,
}
alert_msg[idx][stats_type].append(msg)
else:
print('%s %s OK' % (idx, platform), file=f_log)
fans_text = releaser_fans_alert(stamp-p,stamp)
# send the alert email
csm_mail_service = 'mail.csm.com.cn'
sender = 'zhouyujiang@csm.com.cn'
stats_type_dict = {
'observed': '总视频条目数',
'new_released': '新增视频条目数'
}
for idx in alert_msg:
email_subj = '[爬虫数据日常预警] 数据条目预警 %s %s' % (idx, today.isoformat()[:10])
email_msg_body = ''
for stats_type in alert_msg[idx]:
if len(alert_msg[idx][stats_type]) > 0:
email_msg_body += ('%s %s 预警:\n' % (idx, stats_type_dict[stats_type]))
for ml in alert_msg[idx][stats_type]:
msgline = '平台 %s 抓取 %s 视频条目数:%d,低于预警值(%d) %.2f%% \n' % (
ml['platform'], ml['fetch_date'], ml['video_num'], ml['alert_criteria'], ml['short_perct'])
email_msg_body += msgline
email_msg_body += "\n\n%s\n" % fans_text
email_msg_body += '\nchecking data source index name: %s\n\n\n' % alert_msg[idx][stats_type][0][
'chk_source']
if email_msg_body != '':
email_msg_body += email_msg_suffix
print('email_msg_body:\n', email_msg_body, file=f_log)
email_msg = EmailMessage()
email_msg.set_content(email_msg_body)
email_msg['Subject'] = email_subj
email_msg['From'] = sender
email_msg['to'] = email_group[idx]
try:
server = smtplib.SMTP(host=csm_mail_service)
server.send_message(email_msg)
server.quit()
print('Successfully sent email to %s for %s' % (email_group[idx], idx),
datetime.datetime.now(), file=f_log)
print('email_msg:\n', email_msg, file=f_log)
except:
print('Failed to connect email server.', datetime.datetime.now(), file=f_log)
else:
print('All platforms is ok for %s on fetch_date %s, %s' % (
idx, today.isoformat()[:10], datetime.datetime.now()), file=f_log)
print('Alert criteria:\n', video_num_criteria[idx], file=f_log)
print('\n\n', file=f_log)
f_log.close()
if __name__ == '__main__':
videonumber_alert_for_ccr(0)