diff --git a/urbantz/tracker.py b/urbantz/tracker.py new file mode 100755 index 0000000..8863d79 --- /dev/null +++ b/urbantz/tracker.py @@ -0,0 +1,38 @@ +#!/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', + ) + options = parser.parse_args() + 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(), + round(delivery.position.distance(delivery.destination), 1) + )) + sleep(10) + + +if __name__ == '__main__': + main()