Skip to content

Commit 3b47bb2

Browse files
committed
added product_info
1 parent 2092631 commit 3b47bb2

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

usps_webtools/tracking.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import bs4
12
import requests
23
from typing import Tuple
34
from fake_useragent import UserAgent
@@ -64,18 +65,18 @@ def expected_delivery_status(self) -> Tuple[str, None]:
6465

6566

6667
@property
67-
def history(self) -> Tuple[list, None]:
68+
def history(self) -> Tuple[dict, None]:
6869
"Gets the package tracking history."
6970

7071
if isinstance(self._history, bs4types.Tag):
7172
history = self._history.contents.copy()
72-
string = []; self._history = []
73+
string = []; self._history = {'events' : []}
7374
for x in history:
7475
if isinstance(x, bs4types.Tag):
7576
if x.name == 'hr':
7677
event = re.sub(r'[\t\n\r]*', '', ' '.join(string))
7778
event = event.strip().split(' ')
78-
self._history.append({
79+
self._history['events'].append({
7980
'date': event[0],
8081
'status': event[1],
8182
'location': event[2] if len(event) >= 3 else None
@@ -86,6 +87,17 @@ def history(self) -> Tuple[list, None]:
8687
return self._history
8788

8889

90+
@property
91+
def product_info(self):
92+
if isinstance(self._product_info, bs4types.Tag):
93+
html = self._product_info.find('li').get_text()
94+
self._product_info = {
95+
'postal_product': re.sub(r'[\t\n\r]*', '', html).split(':')[1]
96+
}
97+
98+
return self._product_info
99+
100+
89101
def refresh(self):
90102
"Gets most recent package tracking data from USPS."
91103

@@ -107,7 +119,8 @@ def refresh(self):
107119
self._expected_delivery_status = expected_delivery.find('p')
108120

109121
self._history = html.find('div', attrs={'class': 'panel-actions-content thPanalAction'})
110-
122+
self._product_info = html.find('div', attrs={'class': 'panel-actions-content product-information-content'})
123+
111124

112125
def as_dict(self) -> dict:
113126
"Returns all package tracking information as a dictionary."
@@ -119,11 +132,6 @@ def as_dict(self) -> dict:
119132
'date': self.expected_delivery_date,
120133
'status': self.expected_delivery_status
121134
},
122-
'history': {
123-
'events': self.history
124-
},
125-
'product_info': {
126-
'postal_product': '',
127-
'features': ''
128-
}
135+
'history': self.history,
136+
'product_info': self.product_info
129137
}

0 commit comments

Comments
 (0)