diff --git a/README.md b/README.md index d1b3b31..6430a19 100644 --- a/README.md +++ b/README.md @@ -17,12 +17,15 @@ $ pip3 install elicznik With the package installed readings can be retrieved by simply running the `elicznik` command: ``` -usage: elicznik [-h] [--format {raw,table,csv}] username password [date] +usage: elicznik [-h] [--format {raw,table,csv}] username password [start date] [end date] positional arguments: username tauron-dystrybucja.pl user name password tauron-dystrybucja.pl password - date Date of data to be retrieved + 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. optional arguments: -h, --help show this help message and exit @@ -70,7 +73,19 @@ import datetime import elicznik with elicznik.ELicznik("freddy@example.com", "secretpassword") as m: - readings = m.get_readings(datetime.date(2021, 7, 10)) + # date range + print("July 2021") + + readings = m.get_readings(datetime.date(2021, 7, 1), datetime.date(2021, 7, 31)) + + for timestamp, consumed, produced in readings: + print(timestamp, consumed, produced) + + # single day + print("Yesterday") + + yesterday = datetime.date.today() - datetime.timedelta(days=1) + readings = m.get_readings(yesterday) for timestamp, consumed, produced in readings: print(timestamp, consumed, produced) @@ -81,7 +96,6 @@ with elicznik.ELicznik("freddy@example.com", "secretpassword") as m: * Add support for accounts with multiple meters * Convert the dates to UTC and handle switches from and to DST properly -* Allow reading a date range instead of just one day * Make the dependency on tabulate optional diff --git a/setup.py b/setup.py index 931db2f..4d05dc5 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = (here / 'README.md').read_text(encoding='utf-8') setup( name='elicznik', - version='1.0', + version='1.1', description='Tauron eLicznik scrapper', long_description=long_description, long_description_content_type='text/markdown', diff --git a/src/elicznik/__main__.py b/src/elicznik/__main__.py index 19e568f..fd10fa6 100644 --- a/src/elicznik/__main__.py +++ b/src/elicznik/__main__.py @@ -20,24 +20,46 @@ def main(): parser.add_argument("username", help="tauron-dystrybucja.pl user name") parser.add_argument("password", help="tauron-dystrybucja.pl password") parser.add_argument( - "date", + "start date", nargs="?", type=datetime.date.fromisoformat, default=datetime.date.today() - datetime.timedelta(days=1), - help="Date of data to be retrieved", + help="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.", + ) + parser.add_argument( + "end date", + nargs="?", + type=datetime.date.fromisoformat, + default=None, + help="End date of date range to be retrieved, inclusive, " + "in ISO8601 format. Can be omitted to only retrieve a single " + "day's measurements.", ) args = parser.parse_args() with ELicznik(args.username, args.password) as elicznik: if args.format == "raw": - print(json.dumps(elicznik.get_raw_readings(args.date), indent=4)) + print( + json.dumps( + elicznik.get_raw_readings( + args.start_date, args.end_date + ), + indent=4, + ) + ) return - result = elicznik.get_readings(args.date) + result = elicznik.get_readings(args.start_date, args.end_date) if args.format == "table": - print(tabulate.tabulate(result, headers=["timestamp", "consumed", "produced"])) + print( + tabulate.tabulate( + result, headers=["timestamp", "consumed", "produced"] + ) + ) else: writer = csv.writer(sys.stdout) for timestamp, consumed, produced in result: diff --git a/src/elicznik/elicznik.py b/src/elicznik/elicznik.py index 22a4be5..f38171a 100755 --- a/src/elicznik/elicznik.py +++ b/src/elicznik/elicznik.py @@ -32,7 +32,8 @@ class ELicznik: def __exit__(self, exc_type, exc_val, exc_tb): pass - def get_raw_readings(self, date): + def get_raw_readings(self, start_date, end_date=None): + end_date = end_date or start_date return self.session.post( self.CHART_URL, data={ @@ -40,8 +41,8 @@ class ELicznik: # "dane[chartDay]": date.strftime("%d.%m.%Y"), "dane[paramType]": "csv", "dane[trybCSV]": "godzin", - "dane[startDay]": date.strftime("%d.%m.%Y"), - "dane[endDay]": date.strftime("%d.%m.%Y"), + "dane[startDay]": start_date.strftime("%d.%m.%Y"), + "dane[endDay]": end_date.strftime("%d.%m.%Y"), "dane[checkOZE]": "on", }, ).json() @@ -59,8 +60,8 @@ class ELicznik: value = element.get("EC") yield timestamp, value - def get_readings(self, date): - data = self.get_raw_readings(date).get("dane", {}) + def get_readings(self, start_date, end_date=None): + data = self.get_raw_readings(start_date, end_date).get("dane", {}) consumed = dict(self._extract_values_with_timestamps(data.get("chart", []))) produced = dict(self._extract_values_with_timestamps(data.get("OZE", []))) return sorted(