We don't need the investigate() function anymore.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrintermaps.py
1 import datetime
2 import itertools
3 import json
4 from sqlalchemy import orm
5 from wrpylib.wrorm import Page, WrIntermapsSledrun, WrIntermapsReport, WrIntermapsReportHistory
6
7
8 class UpdateError(RuntimeError):
9     pass
10
11
12 def find_intermaps_sledrun(intermaps_sledrun_id: str, json_content: dict):
13     """Returns iterator of tuples (region, sledrun) for given sledrun_id"""
14     regions = json_content['items']
15     for region in regions:
16         for sledrun in region['sledruns']['items']:
17             if sledrun['id'] == intermaps_sledrun_id:
18                 yield region, sledrun
19
20
21 def region_to_region_with_sledrun_dict(region: dict):
22     region = region.copy()
23     region.update({'sledruns': {sledrun['id']: sledrun for sledrun in region['sledruns']['items']}})
24     return region
25
26
27 def json_content_to_region_dict(json_content: dict) -> dict:
28     return {region['id']: region_to_region_with_sledrun_dict(region) for region in json_content['items']}
29
30
31 def update_wrintermapssledrun(session, json_content: dict):
32     """Updates the wrintermapssledrun table from the wiki. By filling in the missing fields of rows as long as
33     intermaps_sledrun_id and wr_page_id are set.
34     If convert errors occur, an UpdateError exception
35     is raised. No other exception type should be raised under normal circumstances.
36
37     >>> import json
38     >>> from sqlalchemy.engine import create_engine
39     >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
40     >>> Session = orm.sessionmaker(bind=engine)
41     >>> with open('2019-12-06_090500.json') as fp:
42     >>>     content = json.load(fp)
43     >>> update_wrintermapssledrun(Session(), content)
44     """
45     q = session.query(WrIntermapsSledrun)
46     for intermaps_sledrun in q:
47         if intermaps_sledrun.intermaps_sledrun_name is None or intermaps_sledrun.intermaps_region_id is None or \
48                 intermaps_sledrun.intermaps_region_name is None or intermaps_sledrun.intermaps_country is None:
49             region_json, sledrun_json = \
50                 next(find_intermaps_sledrun(intermaps_sledrun.intermaps_sledrun_id, json_content), (None, None))
51             if region_json is None:
52                 raise UpdateError(
53                     f'Sledrun with intermaps id {intermaps_sledrun.intermaps_sledrun_id} not found in JSON')
54         if intermaps_sledrun.intermaps_sledrun_name is None:
55             intermaps_sledrun.intermaps_sledrun_name = sledrun_json['name']
56         if intermaps_sledrun.intermaps_region_id is None:
57             intermaps_sledrun.intermaps_region_id = region_json['id']
58         if intermaps_sledrun.intermaps_region_name is None:
59             intermaps_sledrun.intermaps_region_name = region_json['names']['de']
60         if intermaps_sledrun.intermaps_country is None:
61             intermaps_sledrun.intermaps_country = region_json['countryCode']
62
63         if intermaps_sledrun.wr_page_title is None:
64             page = session.query(Page).get(intermaps_sledrun.wr_page_id)
65             intermaps_sledrun.wr_page_title = page.page_title
66
67     session.commit()
68
69
70 def update_wrintermapsreport(session, json_content: dict, last_check: datetime.datetime):
71     # add current content of intermaps_report to intermaps_history
72     pass
73
74     # delete content of intermaps_report
75     session.query(WrIntermapsReport).delete()
76
77     # add JSON content to intermaps_report
78     regions = json_content_to_region_dict(json_content)
79     for sledrun in session.query(WrIntermapsSledrun):
80         region = regions.get(sledrun.intermaps_region_id)
81         if region is None:
82             continue
83         sledrun_json = region['sledruns'].get(sledrun.intermaps_sledrun_id)
84         if sledrun_json is None:
85             continue
86         report = WrIntermapsReport()
87         report.intermaps_sledrun_id = sledrun.intermaps_sledrun_id
88         report.status = sledrun_json['status']
89         report.last_update = region['lastUpdate']
90         report.last_check = last_check
91         report.utc_offset = 1
92         session.add(report)
93     session.commit()
94
95
96 if __name__ == '__main__':
97     from sqlalchemy.engine import create_engine
98     engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
99     Session = orm.sessionmaker(bind=engine)
100     filename = '/home/philipp/daten/Winterrodeln/Intermaps/intermaps_winterrodeln_2019-12-06_090501.json'
101     with open(filename) as fp:
102         content = json.load(fp)
103     session = Session()
104     update_wrintermapssledrun(session, content)
105     update_wrintermapsreport(session, content, datetime.datetime.now())