2 """Uses the wrsledrunjsoncache table to generate a list of sledruns containing
5 * link to Winterrodeln.org article
7 Ensure you have called updatewrsledrunjsoncache.py previously so that wrsledrunjsoncache is up to date.
9 This script corresponds to the following SQL statement:
12 JSON_EXTRACT(sledrun_json, '$.name') as Name,
13 JSON_EXTRACT(sledrun_json, '$.position.longitude') as Longitude,
14 JSON_EXTRACT(sledrun_json, '$.position.latitude') as Latitude,
15 JSON_EXTRACT(sledrun_json, '$.entry_under_construction') as InArbeit,
16 CONCAT('https://www.winterrodeln.org/wiki/', page_title) as Url
17 FROM wrsledrunjsoncache JOIN page ON sledrun_page_id=page_id
18 ORDER BY JSON_EXTRACT(sledrun_json, '$.name');
22 $ python query_sledrun_list.py inifile1.ini ...
24 One or more .ini configuration files can be given,
25 their format is described in https://www.winterrodeln.org/trac/wiki/ConfigIni.
26 At the end, the following entries have to be present:
30 dbname=philipp_winterrodeln_wiki
35 server=https://www.winterrodeln.org/
43 from typing import List
46 from sqlalchemy.engine import create_engine, Connection
49 def create_sledrun_list(connection: Connection, api_url: str):
51 "JSON_EXTRACT(sledrun_json, '$.name') as name, " \
52 "JSON_EXTRACT(sledrun_json, '$.position.longitude') as latitude, " \
53 "JSON_EXTRACT(sledrun_json, '$.position.latitude') as longitude, " \
54 "JSON_EXTRACT(sledrun_json, '$.length') as length, " \
55 "JSON_EXTRACT(sledrun_json, '$.walkup_time') as walkup_time, " \
56 "JSON_EXTRACT(sledrun_json, '$.entry_under_construction') as under_construction, " \
57 "page_title as title " \
58 "FROM wrsledrunjsoncache JOIN page ON sledrun_page_id=page_id " \
59 "ORDER BY JSON_EXTRACT(sledrun_json, '$.name');"
60 rows = connection.execute(sql)
62 writer = csv.writer(sys.stdout, lineterminator='\n')
63 writer.writerow(['Name', 'Length', 'WalkupTime', 'Longitude', 'Latitude', 'InArbeit', 'Url'])
65 title = row['title'].decode('utf-8')
66 url = urllib.parse.urljoin(api_url, title)
67 writer.writerow([json.loads(row['name']), row['length'], row['walkup_time'], row['latitude'], row['longitude'], row['under_construction'], url])
70 def query_sledrun_list(inifiles: List[str]):
72 :param inifiles: filename of an .ini file or a list of .ini files.
74 config = configparser.ConfigParser()
77 wiki_url = config.get('mediawiki', 'server')
78 article_path = config.get('mediawiki', 'article_path')
79 wiki_path = urllib.parse.urljoin(wiki_url, article_path)
81 host = config.get('mysql', 'host')
82 dbname = config.get('mysql', 'dbname')
83 user = config.get('mysql', 'user_name')
84 passwd = config.get('mysql', 'user_pass')
86 engine = create_engine(f'mysql://{user}@{host}:3306/{dbname}?passwd={passwd}&charset=utf8mb4')
87 create_sledrun_list(engine.connect(), wiki_path)
90 if __name__ == '__main__':
91 parser = argparse.ArgumentParser(description='Generates a sledrun list in CSV format.')
92 parser.add_argument('inifile', nargs='+', help='inifile.ini, see: https://www.winterrodeln.org/trac/wiki/ConfigIni')
93 args = parser.parse_args()
94 query_sledrun_list(args.inifile)