diff options
author | JJ <nicetry@noemail.com> | 2025-03-20 14:11:14 +0000 |
---|---|---|
committer | JJ <nicetry@noemail.com> | 2025-03-20 14:11:14 +0000 |
commit | 670635562388a4a00fa9f45f28b37301c76d976f (patch) | |
tree | 91ac8942bc9a180f458208abf7e2a3a653b25339 | |
parent | 1540bc6da31beeed4f4a2c1c97e100c9d8fd9984 (diff) |
readme
-rw-r--r-- | README.md | 67 | ||||
-rw-r--r-- | package-lock.json | 6 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | pyproject.toml | 7 | ||||
-rw-r--r-- | tube_stat/__init__.py (renamed from src/tube_stat/__init__.py) | 0 | ||||
-rw-r--r-- | tube_stat/cli.py (renamed from src/tube_stat/cli.py) | 40 |
6 files changed, 108 insertions, 13 deletions
@@ -1,3 +1,68 @@ # Tubestat -Python CLI tool to see London tube statuses from the command line +Python CLI tool to see London tube statuses from the command line. + +## Installation + +```console +pipx install tube-stat +``` + +Tubestat relies on a `config.json` file with the following format: + +```json +{ + "APP": "<APPID>", + "KEY": "<KEY>" +} +``` + +You will need to sign up for a developer API key and app name at [TFL](https://api.tfl.gov.uk/). + +On Linux and Mac, the app looks for the following path: + +`/Users/$HOME/.config/tubestat/config.json` + +On Windows: + +`/Users/$HOME/AppData/Roaming/tubestat/config.json` + +## Usage + +Return the status on all lines: + +```console +tubestat +``` + +Return the status only on the Bakerloo line, for example: + +```console +tubestat --line bakerloo +``` + +Return the status for both the Bakerloo and Central line, for example: + +```console +tubestat --line bakerloo,central +``` + +All possible lines: + +```console +"bakerloo" +"victoria" +"central" +"circle" +"district" +"hammersmith-city" +"jubilee" +"metropolitan" +"northern" +"piccadilly" +"waterloo-city" +"dlr" +"overground" +"tram" +"elizabeth" +``` diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..99e9e5f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "tube-stat", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/package.json @@ -0,0 +1 @@ +{} diff --git a/pyproject.toml b/pyproject.toml index 6ecf7d1..c1d9ecb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "tube-stat" -version = "0.1.0" +version = "0.1.2" description = "" authors = [ {name = "JJ",email = "nicetry@noemail.com"} @@ -12,11 +12,8 @@ dependencies = [ "requests (>=2.32.3,<3.0.0)" ] -[tool.poetry] -packages = [{include = "tube_stat", from = "src"}] - [tool.poetry.scripts] -tubestat = "src.tube_stat.cli:cli" +tubestat = "tube_stat.cli:cli" [build-system] requires = ["poetry-core>=2.0.0,<3.0.0"] diff --git a/src/tube_stat/__init__.py b/tube_stat/__init__.py index e69de29..e69de29 100644 --- a/src/tube_stat/__init__.py +++ b/tube_stat/__init__.py diff --git a/src/tube_stat/cli.py b/tube_stat/cli.py index c408804..577eaab 100644 --- a/src/tube_stat/cli.py +++ b/tube_stat/cli.py @@ -1,17 +1,13 @@ import click +import os import json import requests -with open("./config.json", "r") as e: - config = json.load(e) - TUBE_LINES = ( "bakerloo", "victoria", "central", "circle", "district", "hammersmith-city", "jubilee", "metropolitan", "northern", "piccadilly", "waterloo-city", "dlr", - "overground", "tram" + "overground", "tram", "elizabeth" ) -APP = config["APP"] -KEY = config["KEY"] def get_line_color(line: str) -> tuple[int, int, int]: match line: @@ -25,6 +21,8 @@ def get_line_color(line: str) -> tuple[int, int, int]: return (255, 211, 0) case "district": return (0, 120, 42) + case "elizabeth": + return (89, 52, 146) case "hammersmith-city": return (243, 169, 187) case "jubilee": @@ -46,20 +44,48 @@ def get_line_color(line: str) -> tuple[int, int, int]: case _: return (128, 128, 128) +# Get user config +def get_config() -> dict: + if os.name == "nt": + config_path = os.path.join(os.getenv("APPDATA"), "tubestat", "config.json") + else: + config_path = os.path.join(os.path.expanduser("~"), ".config", "tubestat", "config.json") + print(config_path) + try: + with open(config_path, "r") as r: + json_data = json.load(r) + return {"APP": json_data["APP"], "KEY": json_data["KEY"]} + except Exception: + raise click.ClickException(f'ensure that config.json exists and it is properly configured. Instructions are in the Readme') + @click.command() @click.option("--line", default=None, help="comma separated tube lines, e.g bakerloo,central") def cli(line): + app_data = get_config() + APP = app_data.get("APP") + KEY = app_data.get("KEY") + if line: requested_lines = line.split(",") invalid_lines = [l for l in requested_lines if l not in TUBE_LINES] if invalid_lines: raise click.ClickException(f'Invalid tube lines: {', '.join(invalid_lines)}') + else: + requested_lines = ','.join(requested_lines) else: requested_lines = ','.join(TUBE_LINES) + req_url = f'https://api.tfl.gov.uk/Line/{requested_lines}/Status?app_id={APP}&app_key={KEY}' - res = requests.get(req_url) + + try: + res = requests.get(req_url) + res.raise_for_status() + except Exception as e: + raise click.ClickException(f'Invalid api call {e}') + res_json = json.loads(res.text) print_str = "" + for i, r in enumerate(res_json): if len(res_json) - 1 != i: line_break = "\n" |