]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Fix indentation and create function for parsing car-related stuff.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 9 Jan 2022 22:08:07 +0000 (23:08 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 9 Jan 2022 22:08:07 +0000 (23:08 +0100)
bots/sledrun_wikitext_to_json.py

index eff3f25b05c74ac9d57b4af588cb725ea5abcdea..1e3cbfe56686ce150bfc53205a688eea83561715 100644 (file)
@@ -270,115 +270,116 @@ class SledrunWikiTextToJsonBot(
                 sledrun_json['public_transport_lines'] = public_transport_lines
             break
 
-        for v in wikicode.get_sections(levels=[2], matches='Anreise mit dem Auto'):
-            for w in v.ifilter_text(recursive=False):
-                x = w.strip()
-                if x:
-                    sledrun_json["car_description"] = str(x)
-                    break
-            x = []
-            for w in v.ifilter_templates(matches='Parkplatz'):
-                za = str_or_none(w.get(1, None))
-                zb = str_or_none(w.get(2, None))
-                z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb))
-                if len(z) > 0:
-                    x.append({'position': z})
-            if len(x) > 0:
-                sledrun_json['car_parking'] = x
-
-            x = []
-            for w in io.StringIO(str(v)):
-                match = re.match(r"\*\* von \'\'\'(.+)\'\'\'(.*): ([\d.,]+) km", w.rstrip())
-                if match:
-                    ya, yb, yc = match.groups()
-                    yc = float(yc.replace(',', '.'))
-                    x.append({
-                        'km': yc,
-                        'route': (ya.strip() + ' ' + yb.strip()).strip(),
-                    })
-            if len(x) > 0:
-                sledrun_json['car_distances'] = x
-
-            x = []
-            for v in wikicode.get_sections(levels=[2], matches='Allgemeines'):
-                def _gastronomy(value: str):
-                    gastronomy = []
-                    line_iter = io.StringIO(value)
+        def _car():
+            for v in wikicode.get_sections(levels=[2], matches='Anreise mit dem Auto'):
+                for w in v.ifilter_text(recursive=False):
+                    x = w.strip()
+                    if x:
+                        sledrun_json["car_description"] = str(x)
+                        break
+                x = []
+                for w in v.ifilter_templates(matches='Parkplatz'):
+                    za = str_or_none(w.get(1, None))
+                    zb = str_or_none(w.get(2, None))
+                    z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb))
+                    if len(z) > 0:
+                        x.append({'position': z})
+                if len(x) > 0:
+                    sledrun_json['car_parking'] = x
+
+                x = []
+                for w in io.StringIO(str(v)):
+                    match = re.match(r"\*\* von \'\'\'(.+)\'\'\'(.*): ([\d.,]+) km", w.rstrip())
+                    if match:
+                        ya, yb, yc = match.groups()
+                        yc = float(yc.replace(',', '.'))
+                        x.append({
+                            'km': yc,
+                            'route': (ya.strip() + ' ' + yb.strip()).strip(),
+                        })
+                if len(x) > 0:
+                    sledrun_json['car_distances'] = x
+        _car()
+
+        x = []
+        for v in wikicode.get_sections(levels=[2], matches='Allgemeines'):
+            def _gastronomy(value: str):
+                gastronomy = []
+                line_iter = io.StringIO(value)
+                line = next(line_iter, None)
+                while line is not None and line.rstrip() != "* '''Hütten''':":
                     line = next(line_iter, None)
-                    while line is not None and line.rstrip() != "* '''Hütten''':":
-                        line = next(line_iter, None)
-                    if line is None:
-                        return gastronomy
-                    while line is not None:
-                        line = next(line_iter, None)
-                        if line is not None:
-                            if line.startswith('** '):
-                                g = {}
-                                wiki = mwparserfromhell.parse(line)
-                                wiki_link = next(wiki.ifilter_wikilinks(), None)
-                                if isinstance(wiki_link, Wikilink):
-                                    g['wr_page'] = wikilink_to_json(wiki_link)
-                                ext_link = next(wiki.ifilter_external_links(), None)
-                                if isinstance(ext_link, ExternalLink):
-                                    el = {
-                                        'url': str(ext_link.url),
-                                        'text': str(ext_link.title)
-                                    }
-                                    g['weblink'] = el
-                                remaining = str(Wikicode(n for n in wiki.nodes
-                                                         if isinstance(n, (Text, Tag)) and str(n).strip() != '*')).\
-                                                strip()
-                                match = re.match(r'\((.+)\)', remaining)
-                                if match:
-                                    remaining = match.group(1)
-                                if len(remaining) > 0:
-                                    g['note'] = remaining
-                                gastronomy.append(g)
-                            else:
-                                break
+                if line is None:
                     return gastronomy
-                w = _gastronomy(str(v))
-                if len(w) > 0:
-                    sledrun_json['gastronomy'] = w
-
-                def _sled_rental_description():
-                    line_iter = io.StringIO(str(v))
+                while line is not None:
+                    line = next(line_iter, None)
+                    if line is not None:
+                        if line.startswith('** '):
+                            g = {}
+                            wiki = mwparserfromhell.parse(line)
+                            wiki_link = next(wiki.ifilter_wikilinks(), None)
+                            if isinstance(wiki_link, Wikilink):
+                                g['wr_page'] = wikilink_to_json(wiki_link)
+                            ext_link = next(wiki.ifilter_external_links(), None)
+                            if isinstance(ext_link, ExternalLink):
+                                el = {
+                                    'url': str(ext_link.url),
+                                    'text': str(ext_link.title)
+                                }
+                                g['weblink'] = el
+                            remaining = str(Wikicode(n for n in wiki.nodes
+                                                     if isinstance(n, (Text, Tag)) and str(n).strip() != '*')).strip()
+                            match = re.match(r'\((.+)\)', remaining)
+                            if match:
+                                remaining = match.group(1)
+                            if len(remaining) > 0:
+                                g['note'] = remaining
+                            gastronomy.append(g)
+                        else:
+                            break
+                return gastronomy
+            w = _gastronomy(str(v))
+            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)
-                    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)]
+                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)
-                    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()
+                sledrun_json['sled_rental_description'] = ''.join(result).strip()
+            _sled_rental_description()
 
-                i = iter(v.nodes)
-                w = next(i, None)
-                while w is not None:
-                    if isinstance(w, Tag) and str(w) == "'''Siehe auch'''":
-                        w = next(i, None)
-                        break
-                    w = next(i, None)
-                while w is not None:
-                    if isinstance(w, ExternalLink):
-                        link = {'url': w.url}
-                        if w.title is not None:
-                            link['text'] = w.title
-                        x.append(link)
-                    elif isinstance(w, (Text, Tag)) and str(w).strip() in ['', '*', ':']:
-                        pass
-                    else:
-                        break
+            i = iter(v.nodes)
+            w = next(i, None)
+            while w is not None:
+                if isinstance(w, Tag) and str(w) == "'''Siehe auch'''":
                     w = next(i, None)
-            if len(x) > 0:
-                sledrun_json['see_also'] = x
+                    break
+                w = next(i, None)
+            while w is not None:
+                if isinstance(w, ExternalLink):
+                    link = {'url': w.url}
+                    if w.title is not None:
+                        link['text'] = w.title
+                    x.append(link)
+                elif isinstance(w, (Text, Tag)) and str(w).strip() in ['', '*', ':']:
+                    pass
+                else:
+                    break
+                w = next(i, None)
+        if len(x) > 0:
+            sledrun_json['see_also'] = x
 
-            sledrun_json['allow_reports'] = True
+        sledrun_json['allow_reports'] = True
 
         impressions = None
         sledrun_impressions_page = Page(self.site, self.current_page.title() + '/Impressionen')