X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/d8ba29c8d3efb308ca8631970b3228bb1dd91858..14b0005a675787bfb0f90fe318c2090a1edeb045:/owm.py?ds=inline diff --git a/owm.py b/owm.py index 307e910..bd6ba6e 100755 --- a/owm.py +++ b/owm.py @@ -18,7 +18,7 @@ import os from pprint import pprint import sqlalchemy -from sqlalchemy import create_engine, Table +from sqlalchemy import create_engine, Table, URL from seeparklib.openweathermap import openweathermap_json @@ -81,19 +81,19 @@ def write_db(config, url, weather_json, weather_data): host = config.get('database','hostname') db = config.get('database','database') - engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False) - conn = engine.connect() - row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json)) - row.update(weather_data) - for key, value in row.items(): - if isinstance(value, float) and math.isnan(value): - row[key] = None - metadata = sqlalchemy.MetaData() - openweathermap_table = Table('openweathermap', metadata, autoload_with=engine) - ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row) - conn.execute(ins) - conn.commit() - conn.close() + db_url = URL.create(drivername='mysql+mysqldb', username=user, password=pwd, host=host, database=db) + engine = create_engine(db_url, echo=False) + with engine.connect() as conn: + row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json)) + row.update(weather_data) + for key, value in row.items(): + if isinstance(value, float) and math.isnan(value): + row[key] = None + metadata = sqlalchemy.MetaData() + openweathermap_table = Table('openweathermap', metadata, autoload_with=engine) + ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row) + conn.execute(ins) + conn.commit() def main(configfile, debug):