2018-10-20 16:57:23 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
from time import sleep
|
|
|
|
from urbantz import Delivery
|
|
|
|
from urbantz.exceptions import APIError
|
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Simple UrbanTZ delivery tracker",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'delivery',
|
|
|
|
type=Delivery,
|
|
|
|
metavar='ID',
|
|
|
|
help='A unique UrbanTZ delivery ID',
|
|
|
|
)
|
2018-10-20 17:05:45 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-f', '--frequency',
|
|
|
|
type=int,
|
|
|
|
default=10,
|
|
|
|
help='Update frequency in seconds',
|
|
|
|
)
|
2019-01-30 06:40:07 +00:00
|
|
|
options = vars(parser.parse_args())
|
2018-10-20 16:57:23 +00:00
|
|
|
delivery = options['delivery']
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
delivery.update()
|
|
|
|
except (APIError, HTTPError) as e:
|
|
|
|
if isinstance(e, APIError) and e.code == 1:
|
|
|
|
raise SystemExit('Invalid delivery ID')
|
|
|
|
print('Error while fetching data:', str(e))
|
|
|
|
|
|
|
|
print("{} {} meters".format(
|
|
|
|
delivery.last_updated.isoformat(),
|
2018-10-20 17:05:45 +00:00
|
|
|
round(delivery.position.distance(delivery.destination), 1),
|
2018-10-20 16:57:23 +00:00
|
|
|
))
|
2018-10-20 17:05:45 +00:00
|
|
|
sleep(options['frequency'])
|
2018-10-20 16:57:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|