-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_saver.py
72 lines (62 loc) · 2.12 KB
/
data_saver.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
import datetime
import json
import re
from retrying import retry
import requests
from config import API_SERVER, UPLOADER, API_KEY
def check_update_time(conn, link):
cursor = conn.cursor()
select_sql = 'SELECT wjw_notice.update_time FROM wjw_notice WHERE wjw_notice.link = "%s" LIMIT 1' % link
cursor.execute(select_sql)
exist_item = cursor.fetchall()
if len(exist_item) == 0:
return 'insert'
else:
publish_time = exist_item[0][0]
delta = datetime.datetime.now() - publish_time
if delta <= datetime.timedelta(hours=6):
return 'pass'
return 'update'
@retry(stop_max_attempt_number=6, wait_random_min=1, wait_random_max=3)
def update_data(session, province, city, title, date_time:str, content, image, link):
date_pattern = r'([0-9]{4}-([0-9]{2})-([0-9]{2}))'
time_pattern = r'([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$'
try:
date = re.findall(date_pattern, date_time)[0][0]
except:
date = date_time
try:
time = re.findall(time_pattern, date_time)[0][0]
except:
time = '00:00:00'
url = '%s/api/add' % API_SERVER
if content == '':
if image == '':
print('Warning: %s content and image are empty' % title)
else:
content = 'image'
data = {
'province': province, # str
'city': city, # str
'publish_time': time, # str
'publish_date': date, # str
'title': title, # str
'content': content, # str
'link': link, # str
'links_to_pic': image, # str
'announce_type': 0, # int
'uploader': UPLOADER, # 写自己的ID #str
'key': API_KEY # 密钥作为验证权限 #str
}
r = session.post(url, data=json.dumps(data))
if r.status_code != requests.codes.ok:
print('server error: ', link)
print(r.status_code)
print(json.loads(r.text))
else:
result = json.loads(r.content)
if result['code'] == 0:
print(title, 'saved')
else:
print('save error: ', link)
print('error msg: ', result['message'])