From a856f9d0f82ae9426a5c5ff39f62d3777e826412 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Thu, 7 Nov 2019 09:34:39 -0600 Subject: [PATCH] Fiction.live: account for a weird rare bug/possibility in votes Also, add a bunch of error handling / logging to the section-parsing to avoid this in the future. Fixes #30 --- sites/fictionlive.py | 56 +++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/sites/fictionlive.py b/sites/fictionlive.py index 280204e..9d23f9d 100644 --- a/sites/fictionlive.py +++ b/sites/fictionlive.py @@ -52,28 +52,40 @@ class FictionLive(Site): updated = max(updated, segment['ct']) # TODO: work out if this is actually enough types handled # There's at least also a reader post type, which mostly seems to be used for die rolls. - if segment['nt'] == 'chapter': - html.extend(('
', segment['b'].replace('
', '
'), '
')) - elif segment['nt'] == 'choice': - if 'votes' not in segment: - # Somehow, sometime, we end up with a choice without votes (or choices) - continue - votes = {} - for vote in segment['votes']: - votechoices = segment['votes'][vote] - if type(votechoices) == int: - votechoices = (votechoices,) - for choice in votechoices: - if int(choice) < len(segment['choices']): - # sometimes someone has voted for a presumably-deleted choice - choice = segment['choices'][int(choice)] - votes[choice] = votes.get(choice, 0) + 1 - choices = [(votes[v], v) for v in votes] - choices.sort(reverse=True) - html.append('

') + try: + if segment['nt'] == 'chapter': + html.extend(('
', segment['b'].replace('
', '
'), '
')) + elif segment['nt'] == 'choice': + if 'votes' not in segment: + # Somehow, sometime, we end up with a choice without votes (or choices) + continue + votes = {} + for vote in segment['votes']: + votechoices = segment['votes'][vote] + if type(votechoices) == str: + # This caused issue #30, where for some reason one + # choice on a story was a string rather than an + # index into the choices array. + continue + if type(votechoices) == int: + votechoices = (votechoices,) + for choice in votechoices: + if int(choice) < len(segment['choices']): + # sometimes someone has voted for a presumably-deleted choice + choice = segment['choices'][int(choice)] + votes[choice] = votes.get(choice, 0) + 1 + choices = [(votes[v], v) for v in votes] + choices.sort(reverse=True) + html.append('

') + elif segment['nt'] == 'readerPost': + pass + else: + logger.info("Skipped chapter-segment of unhandled type: %s", segment['nt']) + except Exception as e: + logger.error("Skipped chapter-segment due to parsing error", exc_info=e) story.add(Chapter( title=currc['title'],