From 18884475708fed3a1567bbdad0aaf13b3accb080 Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Sat, 25 May 2024 01:37:17 +0800 Subject: [PATCH] 0.1.4: versus rankings IN top tags --- lfcircle.py | 102 +++++++++++++++++++++++++++++++++++++++---------- pyproject.toml | 2 +- 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/lfcircle.py b/lfcircle.py index 2b1dda0..3e2f308 100644 --- a/lfcircle.py +++ b/lfcircle.py @@ -51,6 +51,8 @@ USER_AGENT: Final[str] = ( "Mozilla/5.0 " "(compatible; lfcircle; https://github.com/markjoshwel/lfcircle)" ) +GlobalTagCounter = dict[str, Counter[str]] + class FormatTypeEnum(Enum): """ @@ -251,11 +253,15 @@ class ListeningReport(NamedTuple): leaderboard_albums_pos: int, leaderboard_tracks_pos: int, leaderboard_n: int, + global_tag_counter: GlobalTagCounter, ) -> str: basket: list[str] = [] prefix: str = "" text: str = "" + tag_counter: Counter[str] + tags: list[str] + match behaviour.format: case FormatTypeEnum.ASCII: # intro @@ -328,20 +334,33 @@ class ListeningReport(NamedTuple): tag_counter = Counter[str]() for tag_name, tag_values in self.tags.items(): - tag_counter.update({tag_name: sum(tag_values)}) + tag_counter.update( + {tag_name: calculate_tag_score(tag_values, tags=self.tags)} + ) + + tags = [] + for tn, tc in tag_counter.most_common(5): + tv: str = "" + + if tn in global_tag_counter: + counter = global_tag_counter[tn] + + if len(counter) > 1: + _tv = sum( + [ + i if (user == self.user) else 0 + for i, (user, _) in enumerate( + counter.most_common(), start=1 + ) + ] + ) + tv = f": #{_tv}" if (_tv > 0) else "" + + tags.append(f"{tn} ({tc}%{tv})") basket.append( indent( - ( - "Top tags".ljust(len(d4_l) - 6) - + " : " - + ", ".join( - [ - f"{tn} ({tc})" - for tn, tc in tag_counter.most_common(5) - ] - ) - ), + ("Top tags".ljust(len(d4_l) - 6) + " : " + ", ".join(tags)), prefix=prefix, ) ) @@ -398,18 +417,34 @@ class ListeningReport(NamedTuple): # detail 5: top tags if len(self.tags) > 0: - tag_counter = Counter[str]() + tag_counter = Counter() for tag_name, tag_values in self.tags.items(): - tag_counter.update({tag_name: sum(tag_values)}) - - basket.append( - f"\n{prefix}Top tags" - + ": " - + ", ".join( - [f"{tn} ({tc})" for tn, tc in tag_counter.most_common(5)] + tag_counter.update( + {tag_name: calculate_tag_score(tag_values, tags=self.tags)} ) - ) + + tags = [] + for tn, tc in tag_counter.most_common(5): + tv = "" + + if tn in global_tag_counter: + counter = global_tag_counter[tn] + + if len(counter) > 1: + _tv = sum( + [ + i if (user == self.user) else 0 + for i, (user, _) in enumerate( + counter.most_common(), start=1 + ) + ] + ) + tv = f": #{_tv}" if (_tv > 0) else "" + + tags.append(f"{tn} ({tc}%{tv})") + + basket.append(f"\n{prefix}Top tags" + ": " + ", ".join(tags)) if not behaviour.lowercase: text = "\n".join(basket) @@ -682,6 +717,20 @@ def _rank( return 0 +def calculate_tag_score( + tag_values: tuple[int, ...], tags: dict[str, tuple[int, ...]] +) -> int: + v_cum_sum = sum([sum(tv) for tv in tags.values()]) + v_cum_avg = sum([(sum(tv) / len(tv)) for tv in tags.values()]) + + v_avg = (v_sum := sum(tag_values)) / len(tag_values) + + tag_obj_score = (0.5 * v_avg) + (0.5 * v_sum) + tag_cum_score = (0.5 * v_cum_sum) + (0.5 * v_cum_avg) + + return round(100 * (tag_obj_score / tag_cum_score)) + + def make_circle_report( listening_reports: list[ListeningReport], behaviour: Behaviour, @@ -698,6 +747,18 @@ def make_circle_report( case FormatTypeEnum.TELEGRAM: text.append(behaviour.header + "\n") + # pre-string building global tag versus calculation + global_tag_counter: GlobalTagCounter = {} + + for report in listening_reports: + for tag_name, tag_value in report.tags.items(): + if tag_name not in global_tag_counter: + global_tag_counter[tag_name] = Counter() + + global_tag_counter[tag_name][report.user] += calculate_tag_score( + tag_value, tags=report.tags + ) + for leaderboard_pos, report in enumerate( reversed( sorted( @@ -732,6 +793,7 @@ def make_circle_report( k=lambda r: r.tracks_count, ), leaderboard_n=len(listening_reports), + global_tag_counter=global_tag_counter, ) + "\n" ) diff --git a/pyproject.toml b/pyproject.toml index 7ab2092..2e3f30f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "lfcircle" -version = "0.1.3" +version = "0.1.4" description = "last.fm statistics generator for your friend circle!" authors = ["Mark Joshwel "] license = "Unlicense"