improved error handling

This commit is contained in:
2021-08-13 12:10:15 -05:00
parent 52dbb8d17b
commit de6e5414eb
3 changed files with 18 additions and 11 deletions

16
msg.py
View File

@@ -84,24 +84,28 @@ def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.Data
if emoji_name in cached_emojis:
reactions = react_df.loc[pd.IndexSlice[:, emoji_name], :]
reacted_msgs = msg_df.loc[reactions.index.get_level_values(0).to_list()]
reacted_msgs = reacted_msgs[~reacted_msgs.index.duplicated()].sort_index()
if reacted_msgs.shape[0] == 0:
LOGGER.error(f'No messages found with {emoji_name} reactions')
else:
LOGGER.info(
f'Found {reacted_msgs.shape[0]} messages for the leaderboard, {reactions["count"].sum():.0f} reactions total')
f'Found {reacted_msgs.shape[0]} messages for the leaderboard, ' + \
f'{reactions["count"].sum():.0f} reactions total'
)
try:
reacted_msgs['count'] = reacted_msgs.index.to_series().apply(
lambda idx: reactions.loc[pd.IndexSlice[idx, emoji_name], 'count'])
except pandas.errors.InvalidIndexError as e:
LOGGER.error(f'{e}\n{reacted_msgs[reacted_msgs.index.duplicated()]}')
raise
else:
reacted_msgs = reacted_msgs[
reacted_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
reacted_msgs = reacted_msgs[
reacted_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
reacted_msgs = reacted_msgs.sort_values('count', ascending=False)
reacted_msgs = reacted_msgs.sort_values('count', ascending=False)
return reacted_msgs
return reacted_msgs
else:
LOGGER.error(f'Emoji not found in reactions DataFrame: {emoji_name}')