This commit is contained in:
Wolfgang Hottgenroth 2023-08-28 11:58:38 +02:00
commit 3d824b3c2b
3 changed files with 59 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv/

48
export_month.py Normal file
View File

@ -0,0 +1,48 @@
import psycopg2
import sqlalchemy
import pandas as pd
import argparse
import sys
import datetime
import dateutil.relativedelta
parser = argparse.ArgumentParser(description="export_month.py")
parser.add_argument('--lastmonth', '-l',
help="Export data of the last month",
required=False,
action='store_true',
default=False)
parser.add_argument('--month', '-m',
help="Export data of the given month, format: YYYY-MM-DD",
required=False,
default='0000-00-00')
args = parser.parse_args()
if not args.lastmonth and args.month == '0000-00-00':
raise Exception('Either --lastmonth or --month must be given')
if args.lastmonth and args.month != '0000-00-00':
raise Exception('Only one of --lastmonth and --month must be given')
if args.lastmonth:
startdate = datetime.date.today().replace(day=1) - dateutil.relativedelta.relativedelta(months=1)
else:
startdate = datetime.datetime.strptime(args.month, '%Y-%m-%d').replace(day=1)
enddate = startdate + dateutil.relativedelta.relativedelta(months=1)
try:
engine = sqlalchemy.create_engine('postgresql://wn@db.hottis.de/mainscnt')
with engine.connect() as conn:
dat = pd.read_sql_query(f"select time, location, freq from mainsfrequency where valid=1 and time >= '{startdate}' and time < '{enddate}'", con=conn)
finally:
engine.dispose()
dat = dat.pivot_table(index='time', columns='location', values='freq')
csv_filename = f"{startdate.strftime('%Y-%m')}.csv"
dat.to_csv(csv_filename)

10
requirements.txt Normal file
View File

@ -0,0 +1,10 @@
greenlet==2.0.2
numpy==1.25.2
pandas==2.0.3
psycopg2==2.9.7
python-dateutil==2.8.2
pytz==2023.3
six==1.16.0
SQLAlchemy==2.0.20
typing_extensions==4.7.1
tzdata==2023.3