From 90a26bcc9ce47f50493e2585d1f821f8ea5f2d73 Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 9 Apr 2021 18:04:36 +0200 Subject: [PATCH] flattened results in data_type --- src/data_types/composition.py | 84 +++++++++++------------ src/data_types/frequency.py | 11 +-- src/data_types/presence.py | 124 ++++++++++++++++------------------ src/logs/channel_logs.py | 1 + src/utils/utils.py | 14 +++- 5 files changed, 115 insertions(+), 119 deletions(-) diff --git a/src/data_types/composition.py b/src/data_types/composition.py index e2c0a1c..69364a1 100644 --- a/src/data_types/composition.py +++ b/src/data_types/composition.py @@ -23,49 +23,45 @@ class Composition: self.spoilers = 0 def to_string(self, msg_count: int) -> List[str]: - ret = [] - ret += [ - f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}" - ] - if self.plain_text > 0: - ret += [ - f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})" - ] - if self.edited > 0: - ret += [ - f"- **edited messages**: {self.edited:,} ({percent(self.edited/msg_count)})" - ] - if self.everyone > 0: - ret += [ - f"- **@\u200beveryone**: {self.everyone:,} ({percent(self.everyone/msg_count)})" - ] - if self.mentions > 0: - ret += [ - f"- **mentions**: {self.mentions:,} (in {percent(self.mention_msg/msg_count)} of msg, avg. {precise(self.mentions/msg_count)}/msg)", - ] - if self.answers > 0: - ret += [ - f"- **answers**: {self.answers:,} ({percent(self.answers/msg_count)})" - ] total_emotes = val_sum(self.emotes) - if total_emotes > 0: - top_emote = top_key(self.emotes) - ret += [ - f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)", - f"- **most used emoji**: {top_emote} ({plural(self.emotes[top_emote], 'time')}, {percent(self.emotes[top_emote]/total_emotes)})", - ] - if self.emote_only > 0: - ret += [ - f"- **emoji-only messages**: {self.emote_only:,} ({percent(self.emote_only/msg_count)})" - ] - if self.images > 0: - ret += [f"- **images**: {self.images:,} ({percent(self.images/msg_count)})"] - if self.links > 0: - ret += [f"- **links**: {self.links:,} ({percent(self.link_msg/msg_count)})"] - if self.spoilers > 0: - ret += [ - f"- **spoilers**: {self.spoilers:,} ({percent(self.spoilers/msg_count)})" - ] - if self.tts > 0: - ret += [f"- **tts messages**: {self.tts:,} ({percent(self.tts/msg_count)})"] + top_emote = top_key(self.emotes) + ret = [ + f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}", + f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})" + if self.plain_text > 0 + else "", + f"- **edited messages**: {self.edited:,} ({percent(self.edited/msg_count)})" + if self.edited > 0 + else "", + f"- **@\u200beveryone**: {self.everyone:,} ({percent(self.everyone/msg_count)})" + if self.everyone > 0 + else "", + f"- **mentions**: {self.mentions:,} (in {percent(self.mention_msg/msg_count)} of msg, avg. {precise(self.mentions/msg_count)}/msg)" + if self.mentions > 0 + else "", + f"- **answers**: {self.answers:,} ({percent(self.answers/msg_count)})" + if self.answers > 0 + else "", + f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)" + if total_emotes > 0 + else "", + f"- **most used emoji**: {top_emote} ({plural(self.emotes[top_emote], 'time')}, {percent(self.emotes[top_emote]/total_emotes)})" + if total_emotes > 0 + else "", + f"- **emoji-only messages**: {self.emote_only:,} ({percent(self.emote_only/msg_count)})" + if self.emote_only > 0 + else "", + f"- **images**: {self.images:,} ({percent(self.images/msg_count)})" + if self.images > 0 + else "", + f"- **links**: {self.links:,} ({percent(self.link_msg/msg_count)})" + if self.links > 0 + else "", + f"- **spoilers**: {self.spoilers:,} ({percent(self.spoilers/msg_count)})" + if self.spoilers > 0 + else "", + f"- **tts messages**: {self.tts:,} ({percent(self.tts/msg_count)})" + if self.tts > 0 + else "", + ] return ret diff --git a/src/data_types/frequency.py b/src/data_types/frequency.py index 075084c..b0f00ec 100644 --- a/src/data_types/frequency.py +++ b/src/data_types/frequency.py @@ -67,13 +67,8 @@ class Frequency: f"- **busiest hour ever**: {str_datetime(self.busiest_hour)} ({from_now(self.busiest_hour)}, {self.busiest_hour_count} msg)", f"- **longest break**: {plural(round(self.longest_break.total_seconds()/3600), 'hour')} ({plural(self.longest_break.days,'day')}) from {str_datetime(self.longest_break_start)} ({from_now(self.longest_break_start)})", f"- **avg. streak**: {precise(sum(self.streaks)/len(self.streaks), precision=3)} msg", + f"- **longest streak**: {self.longest_streak:,} msg from {str_datetime(self.longest_streak_start)} ({from_now(self.longest_streak_start)})" + if member_specific + else f"- **longest streak**: {mention(self.longest_streak_author)} ({self.longest_streak:,} msg from {str_datetime(self.longest_streak_start)}, {from_now(self.longest_streak_start)})", ] - if member_specific: - ret += [ - f"- **longest streak**: {self.longest_streak:,} msg from {str_datetime(self.longest_streak_start)} ({from_now(self.longest_streak_start)})" - ] - else: - ret += [ - f"- **longest streak**: {mention(self.longest_streak_author)} ({self.longest_streak:,} msg from {str_datetime(self.longest_streak_start)}, {from_now(self.longest_streak_start)})" - ] return ret diff --git a/src/data_types/presence.py b/src/data_types/presence.py index 778881a..682774b 100644 --- a/src/data_types/presence.py +++ b/src/data_types/presence.py @@ -25,74 +25,70 @@ class Presence: show_top_channel: bool, member_specific: bool, ) -> List[str]: - ret = [] if chan_count is None: type = "server's" elif chan_count == 1: type = "channel's" else: type = "channels'" - if member_specific: - ret += [ - f"- **messages**: {msg_count:,} ({percent(msg_count/total_msg)} of {type})" - ] - else: - top_member = top_key(self.messages) - ret += [ - f"- **top messages**: {mention(top_member)} ({self.messages[top_member]:,} msg, {percent(self.messages[top_member]/val_sum(self.messages))})" - ] - if show_top_channel: - top_channel = top_key(self.channel_usage) - channel_sum = val_sum(self.channel_usage) - found_in = sorted( - self.channel_usage, - key=lambda k: self.channel_usage[k] / self.channel_total[k], - )[-1] - ret += [ - f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {percent(self.channel_usage[top_channel]/channel_sum)})", - ] - if member_specific: - ret += [ - f"- **most contributed channel**: {channel_mention(found_in)} ({self.channel_usage[found_in]:,} msg, {percent(self.channel_usage[found_in]/self.channel_total[found_in])} of {type})" - ] - if member_specific: - if len(self.mentions) > 0: - top_mention = top_key(self.mentions) - mention_sum = val_sum(self.mentions) - ret += [ - f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})", - f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {percent(self.mentions[top_mention]/mention_sum)})", - ] - if len(self.mention_others) > 0: - top_mention = top_key(self.mention_others) - mention_sum = val_sum(self.mention_others) - if member_specific: - ret += [ - f"- **mentioned others**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})", - f"- **mostly mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {percent(self.mention_others[top_mention]/mention_sum)})", - ] - else: - top_member = top_key(self.mention_count) - ret += [ - f"- **mentioned**: {plural(mention_sum, 'time')} ({mention(top_member)}, {percent(self.mention_count[top_member]/val_sum(self.mention_count))})", - f"- **top mentions**: {mention(top_member)} ({plural(self.mention_count[top_member], 'time')}, {percent(self.mention_count[top_member]/val_sum(self.mention_count))})", - f"- **most mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {percent(self.mention_others[top_mention]/mention_sum)})", - ] - if len(self.reactions) > 0: - total_used = val_sum(self.reactions) - top_reaction = top_key(self.reactions) - ret += [ - f"- **reactions**: {plural(total_used, 'time')}", - f"- **most used reaction**: {top_reaction} ({plural(self.reactions[top_reaction], 'time')}, {percent(self.reactions[top_reaction]/total_used)})", - ] - if member_specific: - ret[ - -2 - ] += f" ({percent(total_used/val_sum(self.used_reaction))} of {type})" - else: - top_member = top_key(self.used_reaction) - ret.insert( - -1, - f"- **top reactions**: {mention(top_member)} ({plural(self.used_reaction[top_member], 'time')}, {percent(self.used_reaction[top_member]/val_sum(self.used_reaction))})", - ) + top_member = top_key(self.messages) + top_channel = top_key(self.channel_usage) + channel_sum = val_sum(self.channel_usage) + found_in = top_key( + self.channel_usage, + key=lambda k: self.channel_usage[k] / self.channel_total[k], + ) + top_mention = top_key(self.mentions) + mention_sum = val_sum(self.mentions) + top_mention_others = top_key(self.mention_others) + mention_others_sum = val_sum(self.mention_others) + top_member_mentioned = top_key(self.mention_count) + total_reaction_used = val_sum(self.reactions) + top_reaction = top_key(self.reactions) + top_reaction_member = top_key(self.used_reaction) + + ret = [ + f"- **messages**: {msg_count:,} ({percent(msg_count/total_msg)} of {type})" + if member_specific + else f"- **top messages**: {mention(top_member)} ({self.messages[top_member]:,} msg, {percent(self.messages[top_member]/val_sum(self.messages))})", + f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {percent(self.channel_usage[top_channel]/channel_sum)})" + if show_top_channel + else "", + f"- **most contributed channel**: {channel_mention(found_in)} ({self.channel_usage[found_in]:,} msg, {percent(self.channel_usage[found_in]/self.channel_total[found_in])} of {type})" + if show_top_channel and member_specific + else "", + f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})" + if member_specific and len(self.mentions) > 0 + else "", + f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {percent(self.mentions[top_mention]/mention_sum)})" + if member_specific and len(self.mentions) > 0 + else "", + f"- **mentioned others**: {plural(mention_others_sum, 'time')} ({percent(mention_others_sum/val_sum(self.mention_count))} of {type})" + if len(self.mention_others) > 0 and member_specific + else "", + f"- **mostly mentioned**: {mention(top_mention_others)} ({plural(self.mention_others[top_mention_others], 'time')}, {percent(self.mention_others[top_mention_others]/mention_others_sum)})" + if len(self.mention_others) > 0 and member_specific + else "", + f"- **mentioned**: {plural(mention_others_sum, 'time')} ({mention(top_member_mentioned)}, {percent(self.mention_count[top_member_mentioned]/val_sum(self.mention_count))})" + if len(self.mention_others) > 0 and not member_specific + else "", + f"- **top mentions**: {mention(top_member_mentioned)} ({plural(self.mention_count[top_member_mentioned], 'time')}, {percent(self.mention_count[top_member_mentioned]/val_sum(self.mention_count))})" + if len(self.mention_others) > 0 and not member_specific + else "", + f"- **most mentioned**: {mention(top_mention_others)} ({plural(self.mention_others[top_mention_others], 'time')}, {percent(self.mention_others[top_mention_others]/mention_others_sum)})" + if len(self.mention_others) > 0 and not member_specific + else "", + f"- **reactions**: {plural(total_reaction_used, 'time')}" + if len(self.reactions) > 0 and not member_specific + else "", + f"- **reactions**: {plural(total_reaction_used, 'time')} ({percent(total_reaction_used/val_sum(self.used_reaction))} of {type})" + if len(self.reactions) > 0 and member_specific + else "", + f"- **top reactions**: {mention(top_reaction_member)} ({plural(self.used_reaction[top_reaction_member], 'time')}, {percent(self.used_reaction[top_reaction_member]/val_sum(self.used_reaction))})" + if len(self.reactions) > 0 and not member_specific + else "", + f"- **most used reaction**: {top_reaction} ({plural(self.reactions[top_reaction], 'time')}, {percent(self.reactions[top_reaction]/total_reaction_used)})" + if len(self.reactions) > 0 + else "", + ] return ret diff --git a/src/logs/channel_logs.py b/src/logs/channel_logs.py index 83681e9..99fbfdb 100644 --- a/src/logs/channel_logs.py +++ b/src/logs/channel_logs.py @@ -94,5 +94,6 @@ class ChannelLogs: channel = dict(self.__dict__) channel.pop("channel", None) channel.pop("guild", None) + channel.pop("start_date", None) channel["messages"] = [message.dict() for message in self.messages] return channel diff --git a/src/utils/utils.py b/src/utils/utils.py index a8f2ad5..a823399 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -1,5 +1,5 @@ from calendar import month -from typing import List, Dict, Union, Optional, Any +from typing import Callable, List, Dict, Union, Optional, Any import os import logging import discord @@ -117,11 +117,19 @@ def no_duplicate(seq: list) -> list: # DICTS -def top_key(d: Dict[Union[str, int], int]) -> Union[str, int]: - return sorted(d, key=lambda k: d[k])[-1] +def top_key( + d: Dict[Union[str, int], int], key: Optional[Callable] = None +) -> Union[str, int]: + if len(d) == 0: + return None + if key is None: + key = lambda k: d[k] + return sorted(d, key=key)[-1] def val_sum(d: Dict[Any, int]) -> int: + if len(d) == 0: + return 0 return sum(d.values())