flattened results in data_type

This commit is contained in:
Klemek
2021-04-09 18:04:36 +02:00
parent 2062f08721
commit 90a26bcc9c
5 changed files with 115 additions and 119 deletions
+40 -44
View File
@@ -23,49 +23,45 @@ class Composition:
self.spoilers = 0 self.spoilers = 0
def to_string(self, msg_count: int) -> List[str]: 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) total_emotes = val_sum(self.emotes)
if total_emotes > 0: top_emote = top_key(self.emotes)
top_emote = top_key(self.emotes) ret = [
ret += [ f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}",
f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)", f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})"
f"- **most used emoji**: {top_emote} ({plural(self.emotes[top_emote], 'time')}, {percent(self.emotes[top_emote]/total_emotes)})", if self.plain_text > 0
] else "",
if self.emote_only > 0: f"- **edited messages**: {self.edited:,} ({percent(self.edited/msg_count)})"
ret += [ if self.edited > 0
f"- **emoji-only messages**: {self.emote_only:,} ({percent(self.emote_only/msg_count)})" else "",
] f"- **@\u200beveryone**: {self.everyone:,} ({percent(self.everyone/msg_count)})"
if self.images > 0: if self.everyone > 0
ret += [f"- **images**: {self.images:,} ({percent(self.images/msg_count)})"] else "",
if self.links > 0: f"- **mentions**: {self.mentions:,} (in {percent(self.mention_msg/msg_count)} of msg, avg. {precise(self.mentions/msg_count)}/msg)"
ret += [f"- **links**: {self.links:,} ({percent(self.link_msg/msg_count)})"] if self.mentions > 0
if self.spoilers > 0: else "",
ret += [ f"- **answers**: {self.answers:,} ({percent(self.answers/msg_count)})"
f"- **spoilers**: {self.spoilers:,} ({percent(self.spoilers/msg_count)})" if self.answers > 0
] else "",
if self.tts > 0: f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)"
ret += [f"- **tts messages**: {self.tts:,} ({percent(self.tts/msg_count)})"] 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 return ret
+3 -8
View File
@@ -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"- **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"- **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"- **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 return ret
+60 -64
View File
@@ -25,74 +25,70 @@ class Presence:
show_top_channel: bool, show_top_channel: bool,
member_specific: bool, member_specific: bool,
) -> List[str]: ) -> List[str]:
ret = []
if chan_count is None: if chan_count is None:
type = "server's" type = "server's"
elif chan_count == 1: elif chan_count == 1:
type = "channel's" type = "channel's"
else: else:
type = "channels'" type = "channels'"
if member_specific: top_member = top_key(self.messages)
ret += [ top_channel = top_key(self.channel_usage)
f"- **messages**: {msg_count:,} ({percent(msg_count/total_msg)} of {type})" channel_sum = val_sum(self.channel_usage)
] found_in = top_key(
else: self.channel_usage,
top_member = top_key(self.messages) key=lambda k: self.channel_usage[k] / self.channel_total[k],
ret += [ )
f"- **top messages**: {mention(top_member)} ({self.messages[top_member]:,} msg, {percent(self.messages[top_member]/val_sum(self.messages))})" top_mention = top_key(self.mentions)
] mention_sum = val_sum(self.mentions)
if show_top_channel: top_mention_others = top_key(self.mention_others)
top_channel = top_key(self.channel_usage) mention_others_sum = val_sum(self.mention_others)
channel_sum = val_sum(self.channel_usage) top_member_mentioned = top_key(self.mention_count)
found_in = sorted( total_reaction_used = val_sum(self.reactions)
self.channel_usage, top_reaction = top_key(self.reactions)
key=lambda k: self.channel_usage[k] / self.channel_total[k], top_reaction_member = top_key(self.used_reaction)
)[-1]
ret += [ ret = [
f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {percent(self.channel_usage[top_channel]/channel_sum)})", f"- **messages**: {msg_count:,} ({percent(msg_count/total_msg)} of {type})"
] if member_specific
if member_specific: else f"- **top messages**: {mention(top_member)} ({self.messages[top_member]:,} msg, {percent(self.messages[top_member]/val_sum(self.messages))})",
ret += [ f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {percent(self.channel_usage[top_channel]/channel_sum)})"
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
] else "",
if member_specific: 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 len(self.mentions) > 0: if show_top_channel and member_specific
top_mention = top_key(self.mentions) else "",
mention_sum = val_sum(self.mentions) f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})"
ret += [ if member_specific and len(self.mentions) > 0
f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})", else "",
f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {percent(self.mentions[top_mention]/mention_sum)})", 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
if len(self.mention_others) > 0: else "",
top_mention = top_key(self.mention_others) f"- **mentioned others**: {plural(mention_others_sum, 'time')} ({percent(mention_others_sum/val_sum(self.mention_count))} of {type})"
mention_sum = val_sum(self.mention_others) if len(self.mention_others) > 0 and member_specific
if member_specific: else "",
ret += [ 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)})"
f"- **mentioned others**: {plural(mention_sum, 'time')} ({percent(mention_sum/val_sum(self.mention_count))} of {type})", if len(self.mention_others) > 0 and member_specific
f"- **mostly mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {percent(self.mention_others[top_mention]/mention_sum)})", else "",
] f"- **mentioned**: {plural(mention_others_sum, 'time')} ({mention(top_member_mentioned)}, {percent(self.mention_count[top_member_mentioned]/val_sum(self.mention_count))})"
else: if len(self.mention_others) > 0 and not member_specific
top_member = top_key(self.mention_count) else "",
ret += [ 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))})"
f"- **mentioned**: {plural(mention_sum, 'time')} ({mention(top_member)}, {percent(self.mention_count[top_member]/val_sum(self.mention_count))})", if len(self.mention_others) > 0 and not member_specific
f"- **top mentions**: {mention(top_member)} ({plural(self.mention_count[top_member], 'time')}, {percent(self.mention_count[top_member]/val_sum(self.mention_count))})", else "",
f"- **most mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {percent(self.mention_others[top_mention]/mention_sum)})", 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
if len(self.reactions) > 0: else "",
total_used = val_sum(self.reactions) f"- **reactions**: {plural(total_reaction_used, 'time')}"
top_reaction = top_key(self.reactions) if len(self.reactions) > 0 and not member_specific
ret += [ else "",
f"- **reactions**: {plural(total_used, 'time')}", f"- **reactions**: {plural(total_reaction_used, 'time')} ({percent(total_reaction_used/val_sum(self.used_reaction))} of {type})"
f"- **most used reaction**: {top_reaction} ({plural(self.reactions[top_reaction], 'time')}, {percent(self.reactions[top_reaction]/total_used)})", if len(self.reactions) > 0 and member_specific
] else "",
if member_specific: 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))})"
ret[ if len(self.reactions) > 0 and not member_specific
-2 else "",
] += f" ({percent(total_used/val_sum(self.used_reaction))} of {type})" f"- **most used reaction**: {top_reaction} ({plural(self.reactions[top_reaction], 'time')}, {percent(self.reactions[top_reaction]/total_reaction_used)})"
else: if len(self.reactions) > 0
top_member = top_key(self.used_reaction) else "",
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))})",
)
return ret return ret
+1
View File
@@ -94,5 +94,6 @@ class ChannelLogs:
channel = dict(self.__dict__) channel = dict(self.__dict__)
channel.pop("channel", None) channel.pop("channel", None)
channel.pop("guild", None) channel.pop("guild", None)
channel.pop("start_date", None)
channel["messages"] = [message.dict() for message in self.messages] channel["messages"] = [message.dict() for message in self.messages]
return channel return channel
+11 -3
View File
@@ -1,5 +1,5 @@
from calendar import month from calendar import month
from typing import List, Dict, Union, Optional, Any from typing import Callable, List, Dict, Union, Optional, Any
import os import os
import logging import logging
import discord import discord
@@ -117,11 +117,19 @@ def no_duplicate(seq: list) -> list:
# DICTS # DICTS
def top_key(d: Dict[Union[str, int], int]) -> Union[str, int]: def top_key(
return sorted(d, key=lambda k: d[k])[-1] 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: def val_sum(d: Dict[Any, int]) -> int:
if len(d) == 0:
return 0
return sum(d.values()) return sum(d.values())