Skip to content

Instantly share code, notes, and snippets.

@winstonma
Last active November 23, 2020 06:38
Show Gist options
  • Save winstonma/0af29a86ff1c1911fb0be340c0005554 to your computer and use it in GitHub Desktop.
Save winstonma/0af29a86ff1c1911fb0be340c0005554 to your computer and use it in GitHub Desktop.
# import requests module
import requests
import dateutil.parser as dp
from dateutil.relativedelta import relativedelta
from dateutil.tz import tz, tzlocal
from datetime import *
# Making a get request
response = requests.get('http://localhost:5000/getETAsByStopID/CH15-T-1100-0')
# Set Timezone
to_zone = tz.gettz('Asia/Hong_Kong')
NOW = datetime.now(tzlocal())
# print json content
jsonResponse = response.json()
for route in jsonResponse:
# Print route info
print("Route: " + str(route[0]['stopping']['variant']['route']['number']))
print("Destination: " + str(route[0]['stopping']['variant']['destination']))
print("ETA:")
for stop in route:
# Print ETA
time = dp.isoparse(stop['time'])
print(" " + time.astimezone(to_zone).strftime('%H:%M') + " (" + str(relativedelta(time, NOW).minutes) + " minutes)")
print("\n")

KMB API Client in Python

To run this

pip install -r requirements.txt
python api-client.py
requests
python-dateutil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment