mirror of
https://github.com/kemayo/leech
synced 2026-02-14 10:42:41 +01:00
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
This commit is contained in:
parent
f89f5163b5
commit
a856f9d0f8
1 changed files with 34 additions and 22 deletions
|
|
@ -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(('<div>', segment['b'].replace('<br>', '<br/>'), '</div>'))
|
||||
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('<hr/><ul>')
|
||||
for votecount, choice in choices:
|
||||
html.append(f'<li>{choice}: {votecount}</li>')
|
||||
html.append('</ul><hr/>')
|
||||
try:
|
||||
if segment['nt'] == 'chapter':
|
||||
html.extend(('<div>', segment['b'].replace('<br>', '<br/>'), '</div>'))
|
||||
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('<hr/><ul>')
|
||||
for votecount, choice in choices:
|
||||
html.append(f'<li>{choice}: {votecount}</li>')
|
||||
html.append('</ul><hr/>')
|
||||
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'],
|
||||
|
|
|
|||
Loading…
Reference in a new issue