From 7e3d3b78051559d358b2fb75546948a4e65597ed Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Sat, 8 Jun 2019 20:55:56 +0100 Subject: [PATCH] =?UTF-8?q?Nova=20vers=C3=A3o,=20ja=20mostra=20as=20tags?= =?UTF-8?q?=20e=20supostamente=20deveria=20mostrar=20a=20playcount.=20Abri?= =?UTF-8?q?=20uma=20issue=20no=20pylast.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LastFM/plugin.py | 67 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/LastFM/plugin.py b/LastFM/plugin.py index fca931c..3e60ad9 100644 --- a/LastFM/plugin.py +++ b/LastFM/plugin.py @@ -104,18 +104,40 @@ class LastFM(callbacks.Plugin): self.__parent = super(LastFM, self) self.__parent.__init__(irc) self.db = LastFMDB() + self.network = None + self.prepend = "0,5last.fm" def die(self): self.__parent.die() self.db.close() + def connect(self): + key = self.registryValue('apiKey') + secret = self.registryValue('apiSecret') + + if not key: + irc.error("{} API key not set".format(self.prepend)) + return False + if not secret: + irc.error("{} API secret not set".format(self.prepend)) + return False + + if self.network is None: + self.network = pylast.LastFMNetwork(api_key=key, api_secret=secret) + + return True + def setuser(self, irc, msg, args, channel, user): """[] Assigns the last.fm user to the current nick. """ self.db.set_user(channel, msg.nick.lower(), user) - irc.reply("Last.fm user set as {} for {}".format(user, msg.nick), prefixNick=False) + irc.reply("{} User set as {} for {}".format( + self.prepend, + user, + msg.nick + ), prefixNick=False) setuser = wrap(setuser, ['channel', 'anything']) def nowplaying(self, irc, msg, args, channel, user): @@ -123,14 +145,7 @@ class LastFM(callbacks.Plugin): Show the currently playing song. """ - key = self.registryValue('apiKey') - secret = self.registryValue('apiSecret') - - if not key: - irc.error("Last.fm API key not set") - return - if not secret: - irc.error("Last.fm API secret not set") + if self.connect() is False: return if user: @@ -138,9 +153,37 @@ class LastFM(callbacks.Plugin): else: username = self.db.get_user(channel, msg.nick.lower()) - network = pylast.LastFMNetwork(api_key=key, api_secret=secret) - userinfo = network.get_user(username) - irc.reply(userinfo.get_now_playing(), prefixNick=False) + userinfo = self.network.get_user(username) + track = userinfo.get_now_playing() + playcount = userinfo.get_now_playing().get_userplaycount() + #track_with_user = pylast.Track(artist=track.artist, title=track.title, network=self.network, username=username) + + message = "{} {} is not playing anything right now".format( + self.prepend, + msg.nick + ) + + if track: + # Get all tags from track and create a string with ", tag1, tag2, tag3, " + tags = track.get_top_tags() + tagmsg = "" + if len(tags) > 0: + tagmsg = ", " + for idx in range(len(tags)): + if idx < 5: + tagmsg += "{}, ".format(tags[idx][0]) + + # strip the last 2 chars from string + tagmsg = tagmsg[:-2] + + message = "{} {} is listening to: {} ({} plays{})".format( + self.prepend, + msg.nick, + track, + playcount, + tagmsg + ) + irc.reply(message, prefixNick=False) nowplaying = wrap(nowplaying, ['channel', optional('anything')]) Class = LastFM