From 7c71763390a1ada6e58cbf6621dedb2cfb341dbd Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Sun, 9 Jan 2022 22:08:03 +0100 Subject: [PATCH] More detailed parsing of sled rental. --- bots/sledrun_wikitext_to_json.py | 56 +++++++++++++++++++++++++------- wrpylib/wrmwmarkup.py | 19 ++++++++--- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/bots/sledrun_wikitext_to_json.py b/bots/sledrun_wikitext_to_json.py index ee354c9..4381d5b 100644 --- a/bots/sledrun_wikitext_to_json.py +++ b/bots/sledrun_wikitext_to_json.py @@ -55,6 +55,14 @@ def template_to_json(value: Template) -> dict: } +def wikilink_to_json(value: Wikilink) -> dict: + wl = {'title': str(value.title)} + text = str_or_none(value.text) + if text is not None: + wl['text'] = text + return wl + + class SledrunWikiTextToJsonBot( SingleSiteBot, ConfigParserBot, @@ -154,10 +162,24 @@ class SledrunWikiTextToJsonBot( if w is not None: sledrun_json['nightlight_description'] = w - v = rbb['Rodelverleih'] - if v is not None: - sledrun_json['sled_rental_direct'] = v != [] - sledrun_json['sled_rental_description'] = opt_str_opt_comment_enum_to_str(v) + def _sled_rental(): + v = rbb['Rodelverleih'] + if v is not None: + sledrun_json['sled_rental_direct'] = v != [] + w = [] + for name, comment in v: + x = {} + name_code = mwparserfromhell.parse(name) + wiki_link = next(name_code.ifilter_wikilinks(), None) + if isinstance(wiki_link, Wikilink): + x['wr_page'] = wikilink_to_json(wiki_link) + else: + x['name'] = name + if comment is not None: + x['comment'] = comment + w.append(x) + sledrun_json['sled_rental'] = w + _sled_rental() v = rbb['In Übersichtskarte'] if v is not None: @@ -284,13 +306,7 @@ class SledrunWikiTextToJsonBot( wiki = mwparserfromhell.parse(line) wiki_link = next(wiki.ifilter_wikilinks(), None) if isinstance(wiki_link, Wikilink): - wl = { - 'title': str(wiki_link.title), - } - text = str_or_none(wiki_link.text) - if text is not None: - wl['text'] = text - g['wr_page'] = wl + g['wr_page'] = wikilink_to_json(wiki_link) ext_link = next(wiki.ifilter_external_links(), None) if isinstance(ext_link, ExternalLink): el = { @@ -299,7 +315,7 @@ class SledrunWikiTextToJsonBot( } g['weblink'] = el remaining = str(Wikicode(n for n in wiki.nodes - if isinstance(n, (Text, Tag)) and str(n).strip() is not '*')).\ + if isinstance(n, (Text, Tag)) and str(n).strip() != '*')).\ strip() match = re.match(r'\((.+)\)', remaining) if match: @@ -314,6 +330,22 @@ class SledrunWikiTextToJsonBot( if len(w) > 0: sledrun_json['gastronomy'] = w + def _sled_rental_description(): + line_iter = io.StringIO(str(v)) + line = next(line_iter, None) + match = None + while line is not None and (match := re.match(r"\* '''Rodelverleih''':(.*)", line)) is None: + line = next(line_iter, None) + if match is None: + return + result = [match.group(1)] + line = next(line_iter, None) + while line is not None and re.match(r"\* ", line) is None: + result.append(line) + line = next(line_iter, None) + sledrun_json['sled_rental_description'] = ''.join(result).strip() + _sled_rental_description() + i = iter(v.nodes) w = next(i, None) while w is not None: diff --git a/wrpylib/wrmwmarkup.py b/wrpylib/wrmwmarkup.py index 4bbda38..6394e2b 100644 --- a/wrpylib/wrmwmarkup.py +++ b/wrpylib/wrmwmarkup.py @@ -7,7 +7,7 @@ import collections from typing import Tuple, Optional, List, OrderedDict, Union, Dict import jinja2 -from mwparserfromhell.nodes import Template +from mwparserfromhell.nodes import Template, Wikilink import wrpylib.wrvalidators import wrpylib.mwmarkup @@ -371,6 +371,9 @@ class Jinja2Tools: def json_pos_ele_elevation(self, value: dict) -> str: return value.get('elevation', '') + def json_wr_page(self, value: dict) -> str: + return str(Wikilink(value['title'], value.get('text'))) + def list_template(self, name: str, value: List[str]) -> str: return str(wrpylib.mwmarkup.create_template(name, value)) @@ -421,10 +424,18 @@ def create_sledrun_wiki(sledrun_json: Dict, map_json: Optional[Dict]) -> str: return [(w['type'], w.get('comment')) for w in ws] def rodelverleih() -> Optional[List[Tuple[str, Optional[str]]]]: - sr = sledrun_json.get('sled_rental_direct') - if sr is None: + v = sledrun_json.get('sled_rental') + if v is None: return None - return [('Ja', None)] if sr else [] + w = [] + for x in v: + n = x.get('name') + c = x.get('comment') + p = x.get('wr_page') + if p is not None: + n = Jinja2Tools().json_wr_page(p) + w.append((n, c)) + return w def webauskunft() -> Tuple[Optional[bool], Optional[str]]: info_web = sledrun_json.get('info_web') -- 2.39.5