Draft helper script for intermap tables.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 17 Dec 2019 20:19:05 +0000 (20:19 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Tue, 17 Dec 2019 20:19:05 +0000 (20:19 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2852 7aebc617-e5e2-0310-91dc-80fb5f6d2477

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

diff --git a/wrpylib/wrintermaps.py b/wrpylib/wrintermaps.py
new file mode 100644 (file)
index 0000000..1de2bef
--- /dev/null
@@ -0,0 +1,92 @@
+import itertools
+import json
+from sqlalchemy import schema, orm
+from wrpylib import mwdb, wrmwdb
+
+
+def main():
+    filename = '/home/philipp/daten/Winterrodeln/Intermaps/intermaps_winterrodeln_2019-12-06_090501.json'
+    with open(filename) as fp:
+        content = json.load(fp)
+    regions = content['items']
+    sledruns = list(itertools.chain.from_iterable(region['sledruns']['items'] for region in regions))
+    sledrun_ids_list = [sledrun['id'] for sledrun in sledruns]
+    sledrun_ids_set = set(sledrun_ids_list)
+    duplicates = {sledrun_id for sledrun_id in sledrun_ids_set if sum(sr_id == sledrun_id for sr_id in sledrun_ids_list) >= 2}
+    print(duplicates)
+    print(max(map(len, sledrun_ids_set)))  # 36
+    with open(filename, 'w') as fp:
+        json.dump(content, fp, indent='\t')
+
+
+class UpdateError(RuntimeError):
+    pass
+
+
+def find_intermaps_sledrun(intermaps_sledrun_id: str, json_content: dict):
+    """Returns iterator of tuples (region, sledrun) for given sledrun_id"""
+    regions = json_content['items']
+    for region in regions:
+        for sledrun in region['sledruns']['items']:
+            if sledrun['id'] == intermaps_sledrun_id:
+                yield region, sledrun
+
+
+def update_wrintermapssledrun(engine, json_content: dict):
+    """Updates the wrintermapssledrun table from the wiki. If convert errors occur, an UpdateError exception
+    is raised. No other exception type should be raised under normal circumstances.
+
+    >>> import json
+    >>> from sqlalchemy.engine import create_engine
+    >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
+    >>> with open('2019-12-06_090500.json') as fp:
+    >>>     content = json.load(fp)
+    >>> update_wrintermapssledrun(engine, content)
+    """
+    class Page:
+        pass
+
+    class IntermapsSledrun:
+        pass
+
+    metadata = schema.MetaData()
+    wrintermapssledrun_table = wrmwdb.wrintermapssledrun_table(metadata)
+    page_table = mwdb.page_table(metadata)
+
+    Session = orm.sessionmaker(bind=engine)
+    session = Session()
+    orm.mapper(IntermapsSledrun, wrintermapssledrun_table)
+    orm.mapper(Page, page_table)
+
+    q = session.query(IntermapsSledrun)
+    for intermaps_sledrun in q:
+        if intermaps_sledrun.intermaps_sledrun_name is None or intermaps_sledrun.intermaps_region_id is None or \
+                intermaps_sledrun.intermaps_region_name is None or intermaps_sledrun.intermaps_country is None:
+            region_json, sledrun_json = \
+                next(find_intermaps_sledrun(intermaps_sledrun.intermaps_sledrun_id, json_content), (None, None))
+            if region_json is None:
+                raise UpdateError(
+                    f'Sledrun with intermaps id {intermaps_sledrun.intermaps_sledrun_id} not found in JSON')
+        if intermaps_sledrun.intermaps_sledrun_name is None:
+            intermaps_sledrun.intermaps_sledrun_name = sledrun_json['name']
+        if intermaps_sledrun.intermaps_region_id is None:
+            intermaps_sledrun.intermaps_region_id = region_json['id']
+        if intermaps_sledrun.intermaps_region_name is None:
+            intermaps_sledrun.intermaps_region_name = region_json['names']['de']
+        if intermaps_sledrun.intermaps_country is None:
+            intermaps_sledrun.intermaps_country = region_json['countryCode']
+
+        if intermaps_sledrun.wr_page_title is None:
+            page = session.query(Page).get(intermaps_sledrun.wr_page_id)
+            intermaps_sledrun.wr_page_title = page.page_title
+
+    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')
+    filename = '/home/philipp/daten/Winterrodeln/Intermaps/intermaps_winterrodeln_2019-12-06_090501.json'
+    with open(filename) as fp:
+        content = json.load(fp)
+    update_wrintermapssledrun(engine, content)