big rework of emoji calculations, much simpler and more efficient now

This commit is contained in:
2021-08-13 19:31:22 -05:00
parent 8ef4de693a
commit 108a51b9fe
3 changed files with 92 additions and 91 deletions

35
msg.py
View File

@@ -5,7 +5,6 @@ from typing import Dict, Iterable
import discord
import pandas as pd
import pandas.errors
from dotenv import load_dotenv
LOGGER = logging.getLogger(__name__)
@@ -49,7 +48,7 @@ def message_dict(m: discord.Message) -> Dict:
'user id': m.author.id,
'message': m.content,
'channel': m.channel.name,
'channel link': f'<#{m.channel.id}>',
'channel link': m.channel.mention,
'link': m.jump_url,
}
@@ -78,38 +77,6 @@ async def reaction_dict(r: discord.Reaction) -> Dict:
}
def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.DataFrame:
cached_emojis = react_df.index.get_level_values(1).drop_duplicates().values
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, ' + \
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.sort_values('count', ascending=False)
return reacted_msgs
else:
LOGGER.error(f'Emoji not found in reactions DataFrame: {emoji_name}')
def emoji_totals(edf: pd.DataFrame) -> pd.DataFrame:
totals = edf.groupby('display_name').sum()['count'].sort_values(ascending=False).apply(int)
max_channels = (