Skip to content

Instantly share code, notes, and snippets.

@rodrigosetti
Created April 25, 2016 20:49
Show Gist options
  • Save rodrigosetti/5299448c17e3e378f0aed547c6cb4408 to your computer and use it in GitHub Desktop.
Save rodrigosetti/5299448c17e3e378f0aed547c6cb4408 to your computer and use it in GitHub Desktop.
Parse Trips Helper Script
#! /usr/bin/env python
# coding: utf-8
import json, sys, time
def to_iso_time(ts):
return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(ts))
def list_localities(city):
s = ["woeid=%d" % city["location"]["woeid"]]
for k, i in city["location"].items():
if type(i) == dict and k != "timezone" and "woeid" in i:
s.append("%s=%d" % (i["type"], i["woeid"]))
return ", ".join(s)
def print_report(data):
trips = data["command"][0]["trips"]
print("%d trips" % len(trips["trip"]))
for n, trip in enumerate(trips["trip"]):
print(60 * "-")
print('Trip %d: "%s"' % (n+1, trip["title"]))
for np, pass_ in enumerate(trip["pass"]):
if "flight" in pass_:
reservation = pass_["flight"]["reservationFor"]
print('%d. flight: %s (%s) -> %s (%s)' % (np+1, reservation["departureAirport"]["iataCode"],
to_iso_time(reservation["departureTime"]["timestamp"]),
reservation["arrivalAirport"]["iataCode"],
to_iso_time(reservation["arrivalTime"]["timestamp"])))
elif "hotel" in pass_:
print('%d. hotel' % (np+1))
elif "car" in pass_:
print('%d. car' % (np+1))
print(10 * "-")
print("Destinations:")
for dn, dest in enumerate(trip["destination"]):
for city in dest["city_ref"]:
print("%d %s %s RTI-SUPPORTED=%s %s" % (dn+1, city["rationale"],
city["city"]["full_name"],
city["city"]["supported"]["rti"],
list_localities(city["city"])))
if __name__ == "__main__":
data = json.load(sys.stdin)
print_report(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment