From d04f84cb4b7e1075cfce68c4d1d63f0989a8e815 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Thu, 6 Feb 2020 19:48:45 +0000 Subject: [PATCH] Add compare by b0nk. And some minor fixes. --- LastFM/plugin.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/LastFM/plugin.py b/LastFM/plugin.py index 55e5b84..d18f4b5 100644 --- a/LastFM/plugin.py +++ b/LastFM/plugin.py @@ -109,6 +109,11 @@ class LastFM(callbacks.Plugin): self.network = None self.prepend = "0,5last.fm" self.loved = "13<3 " + self.pretty_bar = ["[04==== ]", + "[04====07==== ]", + "[04====07====08==== ]", + "[04====07====08====09====]", + "[ ]"] def die(self): self.__parent.die() @@ -133,8 +138,13 @@ class LastFM(callbacks.Plugin): def setuser(self, irc, msg, args, channel, user): """[] - Assigns the to the current nick, in [channel]. + Assigns the to the current nick, in [channel]. """ + userinfo = self.network.get_user(user) + if not userinfo: + irc.error("User not found", prefixNick=False) + return + self.db.set_user(channel, msg.nick.lower(), user) irc.reply("{} User set as {} for {}".format( self.prepend, @@ -143,6 +153,65 @@ class LastFM(callbacks.Plugin): ), prefixNick=False) setuser = wrap(setuser, ['channel', 'anything']) + def compare(self, irc, msg, args, channel, user): + """[] + + Compares your account with the account, in [channel]. + """ + if self.connect() is False: + return + + user1 = msg.nick.lower() + user2 = user.lower() + + username1 = self.db.get_user(channel, user1) + if not username1: + username1 = user1 + username2 = self.db.get_user(channel, user2) + if not username2: + username2 = user2 + + try: + user1_favs = self.network.get_user(username1).get_top_artists('overall', 100) + user2_favs = self.network.get_user(username2).get_top_artists('overall', 100) + except pylast.WSError as e: + irc.error(str(e), prefixNick=False) + return + + n_artists1 = len(user1_favs) + n_artists2 = len(user2_favs) + + user1_favs = [str(i.item) for i in user1_favs] + user2_favs = [str(i.item) for i in user2_favs] + + intersection = [artist for artist in user1_favs if artist in user2_favs] + artist_list = intersection[:5] + + comparison_index = round(200.0 * len(intersection) / (n_artists1 + n_artists2), 2) + + if comparison_index < 1.0: + bar = self.pretty_bar[4] + else: + bar = self.pretty_bar[int(comparison_index / 25.01)] + + if artist_list: + parsed_list = [str(item) for item in artist_list] + chart_text = ", ".join(parsed_list) + else: + chart_text = "N/A" + + + message = "{} Comparison: {} {} {}: Similarity: {}% - Common Artists: {}".format( + self.prepend, + user1, + bar, + user2, + comparison_index, + chart_text + ) + irc.reply(message, prefixNick=False) + compare = wrap(compare, ['channel', 'anything']) + def nowplaying(self, irc, msg, args, channel, user): """[] [] @@ -157,6 +226,10 @@ class LastFM(callbacks.Plugin): username = self.db.get_user(channel, msg.nick.lower()) userinfo = self.network.get_user(username) + if not userinfo: + irc.error("User not found", prefixNick=False) + return + track = userinfo.get_now_playing() if not track: