2016-03-30 18:31:23 +00:00
|
|
|
#--require-config lastfm-api-key
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/"
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2017-12-26 10:32:36 +00:00
|
|
|
|
|
|
|
bot.events.on("postboot").on("configure").on(
|
2018-08-30 17:06:46 +00:00
|
|
|
"set").assure_call(setting="lastfm",
|
2017-12-26 10:32:36 +00:00
|
|
|
help="Set username on last.fm")
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
bot.events.on("received").on("command").on("np",
|
|
|
|
"listening", "nowplaying").hook(self.np,
|
2018-08-03 10:40:33 +00:00
|
|
|
help="Get the last listened to track from a user",
|
2016-04-06 11:02:44 +00:00
|
|
|
usage="[username]")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def np(self, event):
|
|
|
|
if event["args_split"]:
|
2018-08-13 15:04:08 +00:00
|
|
|
lastfm_username = event["args_split"][0]
|
|
|
|
shown_username = lastfm_username
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-08-15 09:34:58 +00:00
|
|
|
lastfm_username = event["user"].get_setting("lastfm",
|
2016-03-29 11:56:58 +00:00
|
|
|
event["user"].nickname)
|
2018-08-13 15:04:08 +00:00
|
|
|
shown_username = event["user"].nickname
|
2016-03-29 11:56:58 +00:00
|
|
|
page = Utils.get_url(URL_SCROBBLER, get_params={
|
2018-08-13 15:04:08 +00:00
|
|
|
"method": "user.getrecenttracks", "user": lastfm_username,
|
2016-03-29 11:56:58 +00:00
|
|
|
"api_key": self.bot.config["lastfm-api-key"],
|
|
|
|
"format": "json", "limit": "1"}, json=True)
|
|
|
|
if page:
|
|
|
|
if "recenttracks" in page and len(page["recenttracks"
|
|
|
|
]["track"]):
|
|
|
|
now_playing = page["recenttracks"]["track"][0]
|
|
|
|
track_name = now_playing["name"]
|
|
|
|
artist = now_playing["artist"]["#text"]
|
|
|
|
|
2016-04-04 11:37:17 +00:00
|
|
|
info_page = Utils.get_url(URL_SCROBBLER, get_params={
|
|
|
|
"method": "track.getInfo", "artist": artist,
|
2016-03-29 11:56:58 +00:00
|
|
|
"track": track_name, "autocorrect": "1",
|
|
|
|
"api_key": self.bot.config["lastfm-api-key"],
|
2018-08-13 15:04:08 +00:00
|
|
|
"user": lastfm_username, "format": "json"}, json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
tags = []
|
2016-04-06 15:22:52 +00:00
|
|
|
if "toptags" in info_page.get("track", []):
|
2016-04-04 11:37:17 +00:00
|
|
|
for tag in info_page["track"]["toptags"]["tag"]:
|
2016-03-29 11:56:58 +00:00
|
|
|
tags.append(tag["name"])
|
|
|
|
if tags:
|
|
|
|
tags = " (%s)" % ", ".join(tags)
|
|
|
|
else:
|
|
|
|
tags = ""
|
2016-04-05 17:42:36 +00:00
|
|
|
|
|
|
|
play_count = ""
|
2016-04-06 15:22:52 +00:00
|
|
|
if "userplaycount" in info_page.get("track", []):
|
2016-04-05 17:42:36 +00:00
|
|
|
play_count = int(info_page["track"]["userplaycount"])
|
|
|
|
play_count = " (%d play%s)" % (play_count,
|
|
|
|
"s" if play_count > 1 else "")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2016-04-04 11:37:17 +00:00
|
|
|
event["stdout"].write(
|
2016-04-05 17:42:36 +00:00
|
|
|
"%s is now playing: %s - %s%s%s" % (
|
2018-08-13 15:04:08 +00:00
|
|
|
shown_username, artist, track_name, play_count, tags))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write(
|
|
|
|
"The user '%s' has never scrobbled before" % (
|
2018-08-13 15:04:08 +00:00
|
|
|
shown_username))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|