This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
pyurbantz/urbantz/tracker.py

46 lines
1.2 KiB
Python
Raw Normal View History

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))
2019-02-03 21:12:17 +00:00
distance = delivery.position.distance(delivery.destination.coordinates)
2018-10-20 16:57:23 +00:00
print("{} {} meters".format(
delivery.last_updated.isoformat(),
2019-02-03 21:12:17 +00:00
round(distance, 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()