-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfguoji_save.py
152 lines (109 loc) · 5.41 KB
/
sfguoji_save.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymysql
import redis
import datetime
import time
def save_yanwen():
redis_client = redis.Redis(host='....', port=6379)
while True:
yuntrack = redis_client.lpop('shuefeng_item')
if yuntrack:
j = (str(yuntrack, encoding="utf-8"))
data = eval(j)
logistics_name = 'sfexpress'
tracking_number = data['nu']
status = data['originCountryData']['stausDataNum']
if status == 4 or status == 5:
# 已签收 取第倒数一个为上网时间
for a in data['originCountryData']['trackinfo'][-2: -1]:
info_date = a['Date'].strip()
info_content = a['StatusDescription'].strip()
save_date(tracking_number, info_content, info_date, logistics_name, tracking_status='1')
for b in data['originCountryData']['trackinfo'][:1]: # 取第一条数据为妥投
info_date = b['Date'].strip()
info_content = b['StatusDescription'].strip()
save_date(tracking_number, info_content, info_date, logistics_name, tracking_status='2')
for c in data['originCountryData']['trackinfo'][1:-2]: # 中间的数据,交运数据已存可以去重复
info_date = c['Date'].strip()
info_content = c['StatusDescription'].strip()
save_date(tracking_number, info_content, info_date, logistics_name)
elif status != 4 or status != 5: # 未签收 取第一个为上网时间
for a in data['originCountryData']['trackinfo'][-2:-1]:
info_date = a['Date'].strip()
info_content = a['StatusDescription'].strip()
save_date(tracking_number, info_content, info_date, logistics_name, tracking_status='1')
for c in data['originCountryData']['trackinfo'][:-2]: # 中间的数据,交运数据已存可以去重复
info_date = c['Date'].strip()
info_content = c['StatusDescription'].strip()
save_date(tracking_number, info_content, info_date, logistics_name)
def save_date(tracking_number, info_content, info_date, logistics_name, tracking_status=''):
"""
写入数据
:param tracking_number:
:param description:
:param info_data:
:return:
"""
db = pymysql.connect(host='', user='', password='', port=3306, db='',
charset='utf8')
cursor = db.cursor()
db_1 = pymysql.connect(host='', user='', password='', port=3306, db='',
charset='utf8')
# 已存在的数据不再去查询存入
cursor.execute("select * from logistics_info WHERE tracking_number=\"{}\" "
"and info_content=\"{}\" and info_date=\"{}\"".format(tracking_number, info_content, info_date))
isExists = cursor.rowcount
if not isExists: # 将数据库中没有的数据保存进去
cursor_1 = db_1.cursor()
sql_1 = "SELECT customerorderno pmsorder, ebayorderid salechannelorderid, shippingforwardermethod, salechannel " \
"FROM customerorder where shippingorderno ='{}';".format(tracking_number)
cursor_1.execute(sql_1)
result = cursor_1.fetchone()
try:
pms_order_id = result[0]
sale_channel_order_id = result[1]
logistics_method = result[2]
sale_channel = result[3]
except TypeError as e:
print(e)
else:
creation_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if tracking_status:
data = {
'tracking_number': tracking_number,
'info_content': info_content,
'creation_date': creation_date,
'info_date': info_date,
'logistics_name': logistics_name,
'tracking_status': tracking_status,
'pms_order_id': pms_order_id,
'sale_channel_order_id': sale_channel_order_id,
'logistics_method': logistics_method,
'sale_channel': sale_channel,
}
else:
data = {
'tracking_number': tracking_number,
'info_content': info_content,
'creation_date': creation_date,
'info_date': info_date,
'logistics_name': logistics_name,
'pms_order_id': pms_order_id,
'sale_channel_order_id': sale_channel_order_id,
'logistics_method': logistics_method,
'sale_channel': sale_channel,
}
table = 'logistics_info'
keys = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
try:
cursor.execute(sql, tuple(data.values()))
print('%s:%s save successful' % (tracking_number, info_content))
db.commit()
except:
print('Failed')
db.rollback()
if __name__ == '__main__':
save_yanwen()