Further cleanup of the output
This commit is contained in:
parent
090a2de314
commit
5f01bf54a2
1 changed files with 24 additions and 9 deletions
33
hyperdia.py
33
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, Union
|
||||
from typing import Optional, List
|
||||
from urllib.parse import urlparse, urlencode, urlunparse
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
@ -57,7 +57,8 @@ 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 = f'argument "{self.dest}" requires between {nmin} and {nmax} arguments'
|
||||
msg = (f'argument "{self.dest}" requires '
|
||||
f'between {nmin} and {nmax} arguments')
|
||||
raise argparse.ArgumentTypeError(msg)
|
||||
setattr(args, self.dest, values)
|
||||
return RequiredLength
|
||||
|
@ -88,7 +89,8 @@ class HyperdiaTrip:
|
|||
|
||||
|
||||
def get_hyperdia_data(start_station, end_station, hour, minute, day="15",
|
||||
month="08", year="2020", max_route=5, via=None):
|
||||
month="08", year="2020", max_route=5, via=None,
|
||||
use_shinkansen=True):
|
||||
|
||||
session = requests.Session()
|
||||
post_params = HYPERDIA_PARAMS.copy()
|
||||
|
@ -103,6 +105,10 @@ def get_hyperdia_data(start_station, end_station, hour, minute, day="15",
|
|||
post_params["minute"] = minute
|
||||
post_params["max_route"] = max_route
|
||||
|
||||
if not use_shinkansen:
|
||||
post_params["sprexprs"] = "off"
|
||||
post_params["sprnozomi"] = "off"
|
||||
|
||||
if via is None:
|
||||
for element in ("via_node01", "via_node02", "via_node03"):
|
||||
post_params[element] = ""
|
||||
|
@ -111,10 +117,10 @@ def get_hyperdia_data(start_station, end_station, hour, minute, day="15",
|
|||
if len(via) > 3:
|
||||
raise ValueError("Only up to three through stations are allowed")
|
||||
|
||||
for node, station in zip_longest(
|
||||
for station, node in zip_longest(
|
||||
via,
|
||||
("via_node01", "via_node02", "via_node03"),
|
||||
fill_value=""):
|
||||
fillvalue=""):
|
||||
|
||||
post_params[node] = station
|
||||
|
||||
|
@ -172,7 +178,12 @@ def parse_train_name(element):
|
|||
# Trains are in a list with only one element, inside a span
|
||||
selected_item = element.select("td > ul > li > span")[0]
|
||||
|
||||
return list(selected_item.stripped_strings)[0]
|
||||
# Long train lines (for XXX) have newlines in it, remove, along with tabs
|
||||
|
||||
result = list(selected_item.stripped_strings)[0]
|
||||
result = result.replace("\n", "").replace("\t", "")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def parse_track_number(element):
|
||||
|
@ -306,12 +317,13 @@ 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, output_type: str = "md"):
|
||||
via: List[str] = None, output_type: str = "md",
|
||||
use_shinkansen: bool = True):
|
||||
|
||||
# TODO: Error checking
|
||||
raw_result = get_hyperdia_data(start_station, end_station,
|
||||
hour, minute, day, month, year, max_route,
|
||||
via)
|
||||
via, use_shinkansen)
|
||||
soup = BeautifulSoup(raw_result.text, "html.parser")
|
||||
results = parse_hyperdia_html(soup, year=year, month=month, day=day)
|
||||
|
||||
|
@ -334,6 +346,8 @@ def main():
|
|||
type=lambda d: datetime.strptime(d, "%Y-%m-%d").date())
|
||||
parser.add_argument("--max-routes", help="Maximum number of routes",
|
||||
type=int)
|
||||
parser.add_argument("--no-shinkansen", action="store_false",
|
||||
help="Do not use shinkansen routes")
|
||||
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",
|
||||
|
@ -354,7 +368,8 @@ def main():
|
|||
|
||||
hyperdia_search(options.start_station, options.end_station, hour, minute,
|
||||
day, month, year, options.max_routes, via=options.via,
|
||||
output_type=options.output_type)
|
||||
output_type=options.output_type,
|
||||
use_shinkansen=options.no_shinkansen)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Reference in a new issue