From 065ff76abad2fb94842905940fe7cda1149a9e73 Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Tue, 15 Mar 2022 23:18:31 +0100 Subject: [PATCH] Make more use of function optional_set to simplify code. --- bots/sledrun_wikitext_to_json.py | 50 ++++++++++---------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/bots/sledrun_wikitext_to_json.py b/bots/sledrun_wikitext_to_json.py index eec3329..72f28c1 100644 --- a/bots/sledrun_wikitext_to_json.py +++ b/bots/sledrun_wikitext_to_json.py @@ -121,9 +121,7 @@ class SledrunWikiTextToJsonBot( warning(f"{image_page.title()} does not exist.") sledrun_json['image'] = v - v = rbb['Länge'] - if v is not None: - sledrun_json['length'] = v + optional_set(sledrun_json, 'length', rbb['Länge']) v = rbb['Schwierigkeit'] if v is not None: @@ -134,24 +132,17 @@ class SledrunWikiTextToJsonBot( sledrun_json['avalanches'] = avalanches_german_to_str(v) v, w = rbb['Betreiber'] - if v is not None: - sledrun_json['has_operator'] = v - if w is not None: - sledrun_json['operator'] = w + optional_set(sledrun_json, 'has_operator', v) + optional_set(sledrun_json, 'operator', w) - v = rbb['Aufstieg möglich'] - if v is not None: - sledrun_json['walkup_possible'] = v + optional_set(sledrun_json, 'walkup_possible', rbb['Aufstieg möglich']) v, w = rbb['Aufstieg getrennt'] if v is not None: sledrun_json['walkup_separate'] = tristate_german_to_str(v) - if w is not None: - sledrun_json['walkup_comment'] = w # TODO + optional_set(sledrun_json, 'walkup_comment', w) - v = rbb['Gehzeit'] - if v is not None: - sledrun_json['walkup_time'] = v + optional_set(sledrun_json, 'walkup_time', rbb['Gehzeit']) def _walkup_support(): walkup_support_rbb = rbb['Aufstiegshilfe'] @@ -159,8 +150,7 @@ class SledrunWikiTextToJsonBot( walkup_supports = [] for walkup_support_type, comment in walkup_support_rbb: walkup_support = {'type': walkup_support_type} - if comment is not None: - walkup_support['comment'] = comment + optional_set(walkup_support, 'comment', comment) walkup_supports.append(walkup_support) sledrun_json['walkup_supports'] = walkup_supports _walkup_support() @@ -168,14 +158,11 @@ class SledrunWikiTextToJsonBot( v, w = rbb['Beleuchtungsanlage'] if v is not None: sledrun_json['nightlight_possible'] = tristate_german_to_str(v) - if w is not None: - sledrun_json['nightlight_possible_comment'] = w + optional_set(sledrun_json, 'nightlight_possible_comment', w) v, w = rbb['Beleuchtungstage'] - if v is not None: - sledrun_json['nightlight_weekdays_count'] = v - if w is not None: - sledrun_json['nightlight_weekdays_comment'] = w + optional_set(sledrun_json, 'nightlight_weekdays_count', v) + optional_set(sledrun_json, 'nightlight_weekdays_comment', w) def _sled_rental(): v = rbb['Rodelverleih'] @@ -190,8 +177,7 @@ class SledrunWikiTextToJsonBot( x['wr_page'] = wikilink_to_json(wiki_link) else: x['name'] = name - if comment is not None: - x['comment'] = comment + optional_set(x, 'comment', comment) w.append(x) sledrun_json['sled_rental'] = w _sled_rental() @@ -202,13 +188,8 @@ class SledrunWikiTextToJsonBot( sledrun_json['cachet'] = len(v) > 0 _cachet() - v = rbb['In Übersichtskarte'] - if v is not None: - sledrun_json['show_in_overview'] = v - - v = rbb['Forumid'] - if v is not None: - sledrun_json['forum_id'] = v + optional_set(sledrun_json, 'show_in_overview', rbb['In Übersichtskarte']) + optional_set(sledrun_json, 'forum_id', rbb['Forumid']) v = rbb['Position'] if v is not None: @@ -354,9 +335,7 @@ class SledrunWikiTextToJsonBot( if len(line) > 0: return line return None - w = _nightlight(str(v)) - if w is not None: - sledrun_json['nightlight_description'] = w + optional_set(sledrun_json, 'nightlight_description', _nightlight(str(v))) def _gastronomy(value: str): gastronomy = [] @@ -389,6 +368,7 @@ class SledrunWikiTextToJsonBot( else: break return gastronomy + w = _gastronomy(str(v)) if len(w) > 0: sledrun_json['gastronomy'] = w -- 2.39.5