From 06e31af6ee98e7b517a903827a7d91e726f1e4c7 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Mon, 6 Jan 2020 04:05:27 +0100 Subject: [PATCH] Fully working version with command line arguments --- hyperdia.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/hyperdia.py b/hyperdia.py index d4f2f4e..42ff991 100755 --- a/hyperdia.py +++ b/hyperdia.py @@ -5,7 +5,7 @@ from dataclasses import dataclass from datetime import datetime from itertools import zip_longest import re -from typing import NamedTuple, Optional, List +from typing import NamedTuple, Optional, List, Union from urllib.parse import urlparse, urlencode, urlunparse from bs4 import BeautifulSoup @@ -57,8 +57,7 @@ def required_length(nmin, nmax): class RequiredLength(argparse.Action): def __call__(self, parser, args, values, option_string=None): if not nmin <= len(values) <= nmax: - msg='argument "{f}" requires between {nmin} and {nmax} arguments'.format( - f=self.dest,nmin=nmin,nmax=nmax) + msg = f'argument "{self.dest}" requires between {nmin} and {nmax} arguments' raise argparse.ArgumentTypeError(msg) setattr(args, self.dest, values) return RequiredLength @@ -308,7 +307,7 @@ def trip_summary(trip: HyperdiaTrip) -> str: def hyperdia_search(start_station: str, end_station: str, hour: int, minute: int, day: int = "15", month: str = "08", year: int = 2020, max_route: int = 5, - via: List[str] = None) -> List[str]: + via: List[str] = None, output_type: str = "md"): # TODO: Error checking raw_result = get_hyperdia_data(start_station, end_station, @@ -318,20 +317,27 @@ def hyperdia_search(start_station: str, end_station: str, hour: int, results = parse_hyperdia_html(soup, year=year, month=month, day=day) for trip in results: - print(trip_summary(trip)) + + if output_type == "md": + print(trip_summary(trip)) + elif output_type == "json": + table = convert_trip_to_table(trip) + print(table.to_json(orient="records")) def main(): - parser = argparse.argumentParser() + parser = argparse.ArgumentParser() parser.add_argument("-t", "--time", help="Hour of travel", type=lambda d: datetime.strptime(d, '%H.%M').time()) parser.add_argument("-d", "--date", help="Date of travel", type=lambda d: datetime.strptime(d, "%Y-%m-%d").date()) - parser.add_arguments("--max-routes", help="Maximum number of routes", - type=int) + parser.add_argument("--max-routes", help="Maximum number of routes", + type=int) parser.add_argument("--via", nargs='+', action=required_length(1, 3), help="Stations to force route through (min 1, max 3)") + parser.add_argument("--output-type", choices=("md", "json"), default="md", + help="Output type (markdown or JSON)") parser.add_argument("start_station", help="Start station") parser.add_argument("end_station", help="End station") @@ -347,7 +353,8 @@ def main(): month = str(month) hyperdia_search(options.start_station, options.end_station, hour, minute, - day, month, year, options.max_routes, via=options.via) + day, month, year, options.max_routes, via=options.via, + output_type=options.output_type) if __name__ == "__main__":