Adds tags - thanks to b0nk

This commit is contained in:
Pedro de Oliveira 2020-02-05 23:33:07 +00:00
parent c10b0e87f7
commit 009e424991
1 changed files with 30 additions and 12 deletions

View File

@ -41,6 +41,7 @@ import supybot.utils.minisix as minisix
import os
import sqlite3
import pylast
import time
class SqliteLastFMDB(object):
def __init__(self, filename):
@ -107,7 +108,8 @@ class LastFM(callbacks.Plugin):
self.db = LastFMDB()
self.network = None
self.prepend = "0,5last.fm"
self.loved = "13<3 "
def die(self):
self.__parent.die()
self.db.close()
@ -157,32 +159,48 @@ class LastFM(callbacks.Plugin):
userinfo = self.network.get_user(username)
track = userinfo.get_now_playing()
if not track:
tracks = userinfo.get_recent_tracks()
if len(tracks) > 0:
track = tracks[0]
ts_track = int(track.timestamp)
ts_now = int(time.time())
diff = ts_now - ts_track
if diff <= 300: # Listened to anything in the last 5 minutes
track = track.track
else:
track = None
else:
track = None
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, "
# 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])
# If track has no tags, get the artist tags
if len(tags) == 0:
tags = track.artist.get_top_tags()
tags = tags[:5]
tagmsg = "".join([", " + str(t.item) for t in tags])
tagmsg = "- {}".format(tagmsg[2:])
# strip the last 2 chars from string
tagmsg = tagmsg[:-2]
# Did the user 'Like' this track
lovemsg = ""
if track.get_userloved():
lovemsg = self.loved
playcount = userinfo.get_now_playing().get_userplaycount()
#track_with_user = pylast.Track(artist=track.artist, title=track.title, network=self.network, username=username)
#playcount = track_with_user.get_userplaycount()
message = "{} {} is listening to: {} ({} plays{})".format(
message = "{} {} is listening to: {} {}({} plays) {}".format(
self.prepend,
msg.nick,
track,
lovemsg,
playcount,
tagmsg
)