-
Notifications
You must be signed in to change notification settings - Fork 1
/
package_tracking.py
88 lines (69 loc) · 2.51 KB
/
package_tracking.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
import sys
import requests
from bs4 import BeautifulSoup
import argparse
class CPostPackage:
"""
Class for storing Ceska posta package information
"""
def __init__(self, tracking_number="DR1234567890E"):
"""
Constructor for CPostPackage class.
"""
# Create some member animals
self.tracking_number = tracking_number
self.tracking_url = "https://www.postaonline.cz/trackandtrace/-/zasilka/cislo?parcelNumbers="
def print_package_status(status):
"""
Prints package info in nice format base on last status
:param status: Last package status
:return:
"""
# Check if the input is list
if type(status) is not list:
raise TypeError
# Get date and status
package_no = str(status[0])
package_date = str(status[1])
package_status = str(status[2])
# Get the line length
current_line = '=' * (len(package_date) + len(package_status) + 7)
no_line = '=' * int((len(current_line) - len(package_no)) / 2)
# If the no. of chars in package no. is odd, align the printed table
if len(no_line) % 2 is 0:
print("{0}{1}{2}".format(no_line, package_no, no_line))
else:
print("{0}{1}{2}=".format(no_line, package_no, no_line))
print(current_line)
print("= {0} | {1} =".format(package_date, package_status))
print(current_line)
def find_package_status(cpost_pkg):
"""
Gets Ceska posta track and trace page and scrape actual package info based on package no.
:param cpost_pkg: A CPostPackage class with url and track no.
:return:
"""
try:
r = requests.get(cpost_pkg.tracking_url + cpost_pkg.tracking_number)
except requests.exceptions.RequestException as e:
print("{0}".format(e))
html_doc = r.content
soup = BeautifulSoup(html_doc, 'html.parser')
status = []
for elm in soup.select('[class~=datatable2] tr strong'):
# print(elm.get_text(strip=True))
if elm.get_text(strip=True) is not '':
status.append(elm.get_text(strip=True))
try:
print_package_status(status)
except IndexError as e:
print("Neexistujici cislo zasilky!".format(e))
sys.exit(0)
if __name__ == '__main__':
# Construct the argument parse and parse the arguments
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--package', required=True, help="Package number")
args = parser.parse_args()
# Create a new delivery class
delivery = CPostPackage(args.package)
find_package_status(delivery)