Add usage

This commit is contained in:
nemunaire 2016-07-10 18:55:53 +02:00
parent 5b7b3f656b
commit bc05a6f39d

View File

@ -5,32 +5,46 @@ import sys
import captaintrain as ct
def usage():
print("""cheapest_ticket.py departure-station arrival-station departure-date
departure-date: any string parsable by date -d
""")
if __name__ == "__main__":
if len(sys.argv) > 3:
departure = ct.station(sys.argv[1])
arrival = ct.station(sys.argv[2])
departure_time = 0
if len(sys.argv) <= 3:
usage()
sys.exit(1)
with subprocess.Popen(["date", "-d", sys.argv[3], "-u", "-Iseconds"], stdout=subprocess.PIPE) as f:
departure_time = f.stdout.read().strip().decode()
departure = ct.station(sys.argv[1])
if departure is None:
print("Unknown station '%s'" % sys.argv[1])
sys.exit(1)
if departure_time == 0 or departure_time == "":
sys.exit(1)
else:
departure_time = ct.parse_datetime(departure_time)
arrival = ct.station(sys.argv[2])
if arrival is None:
print("Unknown station '%s'" % sys.argv[2])
sys.exit(1)
print("From:", departure.name, departure.id)
print("To:", arrival.name, arrival.id)
print("Departure:", departure_time)
departure_time = 0
#with open("res.json") as f:
# import json
# res = ct.Search(**json.load(f))
with subprocess.Popen(["date", "-d", sys.argv[3], "-u", "-Iseconds"], stdout=subprocess.PIPE) as f:
departure_time = f.stdout.read().strip().decode()
res = ct.search(departure, arrival, departure_time)
min_trips = ct.cheapest_trips(res.trips)
for trip in min_trips:
print(trip)
if departure_time == 0 or departure_time == "":
sys.exit(1)
else:
print("usage")
departure_time = ct.parse_datetime(departure_time)
print("From:", departure.name, departure.id)
print("To:", arrival.name, arrival.id)
print("Departure:", departure_time)
#with open("res.json") as f:
# import json
# res = ct.Search(**json.load(f))
res = ct.search(departure, arrival, departure_time)
min_trips = ct.cheapest_trips(res.trips)
for trip in min_trips:
print(trip)