-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetDeliveryData.py
159 lines (140 loc) · 4.85 KB
/
getDeliveryData.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
#!/usr/bin/python3
import requests as rq
from bs4 import BeautifulSoup as bs
import json
import datetime
#東風
DONGPOO_CUSTOMER_DATA_LINK = 'http://220.135.157.10:8088/goods_status'
DONGPOO_DELIVERY_DETAILS_LINK = 'http://220.135.157.10:8088/goods_queryGoodsStatus'
DONGPOO_ID_LENGTH = (10, 12)
class DongPoo():
def __init__(self, deliveryId):
self.deliveryId = str(deliveryId)
self.handleDate = None
self.consigneeName = None
self.bookingDate = None
self.realCcPrice = None
self.details = list()
def deliveryIdLengthIsCorrect(self):
if (len(self.deliveryId) in DONGPOO_ID_LENGTH) and (self.deliveryId.isdigit()):
return True
def getCustomerData(self):
response = rq.post(DONGPOO_CUSTOMER_DATA_LINK, data={'trackNo': self.deliveryId})
if response.json() != []:
responseData = response.json()[0]
self.handleDate = responseData['handleDate'] # 送達日期
self.consigneeName = responseData['consigneeName'] # 收件人
self.bookingDate = datetime.datetime.strptime(
responseData['bookingDate'], '%Y%m%d').strftime('%Y/%m/%d') # 資料日期
self.realCcPrice = responseData['realCcPrice'] # 代收款
def getDetailsData(self):
response = rq.post(DONGPOO_DELIVERY_DETAILS_LINK,
data={'trackNo': self.deliveryId})
responseDatas = response.json()
for data in responseDatas:
self.details.append({
'rowNum': data['rowNum'],
'handleDate': data['handleDate'], # 日期
'handleTime': data['handleTime'], # 時間
'remark': data['remark'] # 貨況
})
def getData(self):
try:
if self.deliveryIdLengthIsCorrect():
self.getCustomerData()
self.getDetailsData()
return self.__dict__ if (self.handleDate != None and
self.consigneeName != None and
self.bookingDate != None and
self.realCcPrice != None and
self.details != list()) else None
except:
return None
# 黑貓
BLACKCAT_LINK_TEMPLATE = 'https://www.t-cat.com.tw/Inquire/TraceDetail.aspx?BillID={}&ReturnUrl=Trace.aspx'
BLACKCAT_ID_LENGTH = 12
class BlackCat():
def __init__(self, deliveryId):
self.deliveryId = str(deliveryId)
self.responseData = list()
def deliveryIdLengthIsCorrect(self):
if (len(self.deliveryId) == BLACKCAT_ID_LENGTH) and (self.deliveryId.isdigit()):
return True
def getResponseData(self):
response = rq.get(BLACKCAT_LINK_TEMPLATE.format(self.deliveryId))
soup = bs(response.text, 'lxml')
try:
rows = soup.select('table.tablelist')[0].select('tr')
for rowIndex in range(1, len(rows)):
elementIndex = 0
if rowIndex == 1:
elementIndex += 1
self.responseData.append({
'rowNum': rowIndex,
'status': rows[rowIndex].select('td')[elementIndex].text.strip(),
'time': rows[rowIndex].select('td')[elementIndex + 1].text.strip(),
'location': rows[rowIndex].select('td')[elementIndex + 2].text.strip()
})
except IndexError:
pass
def getData(self):
try:
if self.deliveryIdLengthIsCorrect():
self.getResponseData()
return self.responseData if self.responseData != list() else None
except:
return None
# 速力達
SKYLEADEREXPRESS_LINK_TEMPLATE = 'http://www.sldex.com/index_DetailSearchResult.php?ship_no=(%27{}%27)'
SKYLEADEREXPRESS_ID_LENGTH = 12
class SkyLeaderExpress():
def __init__(self, deliveryId):
self.deliveryId = str(deliveryId)
self.responseData = list()
def deliveryIdLengthIsCorrect(self):
if (len(self.deliveryId) == SKYLEADEREXPRESS_ID_LENGTH) and (self.deliveryId.isdigit()):
return True
def getResponse(self):
response = rq.get(SKYLEADEREXPRESS_LINK_TEMPLATE.format(self.deliveryId))
response.encoding = 'UTF-8'
soup = bs(response.text, 'lxml')
responseTable = soup.select('div.alert.alert-danger')[0].select('b')
responseDeliveryId = responseTable[0].text
responseDatas = [data.text.split('·') for data in responseTable[1:]]
if responseDeliveryId == self.deliveryId:
for data in responseDatas:
self.responseData.append({
'time': data[0],
'status': data[1]
})
def getData(self):
try:
if self.deliveryIdLengthIsCorrect():
self.getResponse()
except:
pass
finally:
return self.responseData if (self.responseData != list()) else None
class GetDeliveryData():
def __init__(self, deliveryId):
self.deliveryId = deliveryId
self.skyLeaderExpress = SkyLeaderExpress(deliveryId)
self.dongPoo = DongPoo(deliveryId)
self.blackCat = BlackCat(deliveryId)
def getDict(self):
return {
'deliveryId': self.deliveryId,
'data': {
'skyLeader': self.skyLeaderExpress.getData(),
'dongPoo': self.dongPoo.getData(),
'blackCat': self.blackCat.getData()
}
}
if __name__ == '__main__':
# deliveryId = 900143300616
deliveryId = 900032521962
# print(DongPoo(deliveryId).getData())
# print(BlackCat(deliveryId).getData())
# deliveryId = 300003938454
# print(SkyLeaderExpress(deliveryId).getData())
print(GetDeliveryData(deliveryId).getDict())