From 7e5e2ede74f3e727d2d195866e5b4e15b30ec7c1 Mon Sep 17 00:00:00 2001 From: Jan Myszkier Date: Wed, 13 Mar 2024 09:16:16 +0100 Subject: [PATCH] Add --site parameter --- README.md | 34 ++++++++++++++++++++++++++-------- src/elicznik/__main__.py | 19 +++++++++++++++++-- src/elicznik/elicznik.py | 12 ++++++++++-- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7d321db..87e297c 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,39 @@ $ pip3 install git@github.com:mlesniew/elicznik.git With the package installed readings can be retrieved by simply running the `elicznik` command: ``` -usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] username password [start_date] [end_date] +usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] [--site SITE] username password [start_date] [end_date] positional arguments: username tauron-dystrybucja.pl user name password tauron-dystrybucja.pl password - start_date Start date of date range to be retrieved, in ISO8601 format. If the end date is omitted, it's the only date for which - measurements are retrieved. - end_date End date of date range to be retrieved, inclusive, in ISO8601 format. Can be omitted to only retrieve a single day's - measurements. + start_date Start date of date range to be retrieved, in ISO8601 format. If the end date is omitted, it's the only date for which measurements are retrieved. + end_date End date of date range to be retrieved, inclusive, in ISO8601 format. Can be omitted to only retrieve a single day's measurements. options: -h, --help show this help message and exit --format {table,csv} Specify the output format - --api {chart,csv} Specify which Tauron API to use to get the measurements. + --api {chart,csv} Specify which Tauron API to use to get the measurements + --site SITE site identifier, must match '[0-9]+_[0-9]+_[0-9]+' +(venv) ~/src/elicznik (site) $ elicznik --help | cat +usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] [--site SITE] + username password [start_date] [end_date] + +positional arguments: + username tauron-dystrybucja.pl user name + password tauron-dystrybucja.pl password + start_date Start date of date range to be retrieved, in ISO8601 + format. If the end date is omitted, it's the only date + for which measurements are retrieved. + end_date End date of date range to be retrieved, inclusive, in + ISO8601 format. Can be omitted to only retrieve a + single day's measurements. + +options: + -h, --help show this help message and exit + --format {table,csv} Specify the output format + --api {chart,csv} Specify which Tauron API to use to get the + measurements + --site SITE site identifier, must match '[0-9]+_[0-9]+_[0-9]+' ``` @@ -79,7 +98,7 @@ timestamp consumed produced net consumption net production import datetime import elicznik -with elicznik.ELicznik("freddy@example.com", "secretpassword") as m: +with elicznik.ELicznik("freddy@example.com", "secretpassword", "optional_site_identifier") as m: # date range print("July 2021") @@ -127,7 +146,6 @@ with elicznik.ELicznikChart("freddy@example.com", "secretpassword") as m: ## TODO & bugs -* Add support for accounts with multiple meters * Convert the dates to UTC and handle switches from and to DST properly * Make the dependency on tabulate optional diff --git a/src/elicznik/__main__.py b/src/elicznik/__main__.py index 5528d18..ca704e7 100644 --- a/src/elicznik/__main__.py +++ b/src/elicznik/__main__.py @@ -2,11 +2,20 @@ import argparse import csv import datetime import sys +import re import tabulate from .elicznik import ELicznikChart, ELicznikCSV +SITE_ID_PATTERN = "[0-9]+_[0-9]+_[0-9]+" + +def parse_site(site): + match = re.match(SITE_ID_PATTERN, site) + if not match: + raise ValueError + return match.string + def main(): parser = argparse.ArgumentParser() @@ -20,7 +29,13 @@ def main(): "--api", choices=["chart", "csv"], default="csv", - help="Specify which Tauron API to use to get the measurements. ", + help="Specify which Tauron API to use to get the measurements", + ) + parser.add_argument( + "--site", + type=parse_site, + default=None, + help=f"site identifier, must match '{SITE_ID_PATTERN}'", ) parser.add_argument("username", help="tauron-dystrybucja.pl user name") parser.add_argument("password", help="tauron-dystrybucja.pl password") @@ -47,7 +62,7 @@ def main(): elicznik_class = ELicznikCSV if args.api == "csv" else ELicznikChart - with elicznik_class(args.username, args.password) as elicznik: + with elicznik_class(args.username, args.password, args.site) as elicznik: result = elicznik.get_readings(args.start_date, args.end_date) if args.format == "table": diff --git a/src/elicznik/elicznik.py b/src/elicznik/elicznik.py index 5781230..696ee45 100755 --- a/src/elicznik/elicznik.py +++ b/src/elicznik/elicznik.py @@ -9,9 +9,10 @@ from .session import Session class ELicznikBase: LOGIN_URL = "https://logowanie.tauron-dystrybucja.pl/login" - def __init__(self, username, password): + def __init__(self, username, password, site=None): self.username = username self.password = password + self.site = site def login(self): self.session = Session() @@ -24,6 +25,13 @@ class ELicznikBase: "service": "https://elicznik.tauron-dystrybucja.pl", }, ) + if self.site is not None: + self.session.post( + "https://elicznik.tauron-dystrybucja.pl/ustaw_punkt", + data={ + "site[client]": self.site + }, + ) def __enter__(self): self.login() @@ -153,4 +161,4 @@ class ELicznikCSV(ELicznikBase): ) -ELicznik = ELicznikCSV +ELicznik = ELicznikCSV \ No newline at end of file