Move command line interface to intermaps to separate file.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 17 Dec 2019 20:19:19 +0000 (20:19 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 17 Dec 2019 20:19:19 +0000 (20:19 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2867 7aebc617-e5e2-0310-91dc-80fb5f6d2477

scripts/update_intermaps.py [new file with mode: 0644]
wrpylib/wrintermaps.py

diff --git a/scripts/update_intermaps.py b/scripts/update_intermaps.py
new file mode 100644 (file)
index 0000000..843721e
--- /dev/null
@@ -0,0 +1,56 @@
+import argparse
+import configparser
+import datetime
+import json
+import os
+from typing import Optional, Tuple
+import urllib.request
+from sqlalchemy.engine import create_engine
+from sqlalchemy import orm
+from wrpylib.wrintermaps import update_wrintermapssledrun, update_wrintermapsreport
+
+
+INTERMAPS_URL = 'https://iski.intermaps.com/api/v1/winterrodeln_org.aspx'
+
+
+def download(src: str) -> bytes:
+    with urllib.request.urlopen(src) as fp:
+        return fp.read()
+
+
+def get_json_str(in_json: Optional[str]) -> Tuple[str, datetime.datetime]:
+    if in_json is None:
+        json_bytes = download(INTERMAPS_URL)
+        return json_bytes.decode(), datetime.datetime.now()
+    with open(in_json) as fp:
+        return fp.read(), datetime.datetime.fromtimestamp(os.path.getmtime(in_json))
+
+
+def save_json_str(json_str: str, out_json: Optional[str]):
+    if out_json is not None:
+        with open(out_json, 'w') as fp:
+            fp.write(json_str)
+
+
+def main(in_json: Optional[str], out_json: Optional[str], inifile: str):
+    json_str, in_date = get_json_str(in_json)
+    save_json_str(json_str, out_json)
+    content = json.loads(json_str)
+    config = configparser.ConfigParser()
+    config.read(inifile)
+    connection_string = config.get('mysql', 'sqlalchemy_user')
+    engine = create_engine(connection_string)
+    SessionMaker = orm.sessionmaker(bind=engine)
+    session = SessionMaker()
+    update_wrintermapssledrun(session, content)
+    update_wrintermapsreport(session, content, in_date)
+    session.commit()
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Downloads intermaps sledrun data (JSON) and updates the tables.')
+    parser.add_argument('--in-json', help='Don\'t download but use this file instead')
+    parser.add_argument('--out-json', help='Additionally save downloaded JSON data to this filename')
+    parser.add_argument('inifile', help='Configuration file, see https://www.winterrodeln.org/trac/wiki/ConfigIni')
+    args = parser.parse_args()
+    main(args.in_json, args.out_json, args.inifile)
index a1245257a24fe60d79822a3dbed17fcab7c6a9df..b7d54999151207216bb682bb7bda3849a9411ba7 100644 (file)
@@ -119,16 +119,3 @@ def update_wrintermapsreport(session: Session, json_content: dict, last_check: d
 
     # add JSON content to intermaps_report
     insert_json_to_intermapsreport(session, json_content, last_check)
-    session.commit()
-
-
-if __name__ == '__main__':
-    from sqlalchemy.engine import create_engine
-    engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
-    SessionMaker = orm.sessionmaker(bind=engine)
-    filename = '/home/philipp/daten/Winterrodeln/Intermaps/intermaps_winterrodeln_2019-12-06_090501.json'
-    with open(filename) as fp:
-        content = json.load(fp)
-    session = SessionMaker()
-    update_wrintermapssledrun(session, content)
-    update_wrintermapsreport(session, content, datetime.datetime.now())