1
+ import bs4
1
2
import requests
2
3
from typing import Tuple
3
4
from fake_useragent import UserAgent
@@ -64,18 +65,18 @@ def expected_delivery_status(self) -> Tuple[str, None]:
64
65
65
66
66
67
@property
67
- def history (self ) -> Tuple [list , None ]:
68
+ def history (self ) -> Tuple [dict , None ]:
68
69
"Gets the package tracking history."
69
70
70
71
if isinstance (self ._history , bs4types .Tag ):
71
72
history = self ._history .contents .copy ()
72
- string = []; self ._history = []
73
+ string = []; self ._history = { 'events' : []}
73
74
for x in history :
74
75
if isinstance (x , bs4types .Tag ):
75
76
if x .name == 'hr' :
76
77
event = re .sub (r'[\t\n\r]*' , '' , ' ' .join (string ))
77
78
event = event .strip ().split (' ' )
78
- self ._history .append ({
79
+ self ._history [ 'events' ] .append ({
79
80
'date' : event [0 ],
80
81
'status' : event [1 ],
81
82
'location' : event [2 ] if len (event ) >= 3 else None
@@ -86,6 +87,17 @@ def history(self) -> Tuple[list, None]:
86
87
return self ._history
87
88
88
89
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
+
89
101
def refresh (self ):
90
102
"Gets most recent package tracking data from USPS."
91
103
@@ -107,7 +119,8 @@ def refresh(self):
107
119
self ._expected_delivery_status = expected_delivery .find ('p' )
108
120
109
121
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
+
111
124
112
125
def as_dict (self ) -> dict :
113
126
"Returns all package tracking information as a dictionary."
@@ -119,11 +132,6 @@ def as_dict(self) -> dict:
119
132
'date' : self .expected_delivery_date ,
120
133
'status' : self .expected_delivery_status
121
134
},
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
129
137
}
0 commit comments