2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
2016-03-30 18:31:23 +00:00
|
|
|
#--require-config lastfm-api-key
|
|
|
|
|
2018-09-23 01:21:43 +00:00
|
|
|
from datetime import datetime, timezone
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/"
|
|
|
|
|
2020-05-07 21:23:04 +00:00
|
|
|
SETTING_YT = utils.BoolSetting("lastfm-youtube",
|
|
|
|
"Whether or not to search last.fm now-playing results on youtube")
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("set", utils.Setting("lastfm", "Set last.fm username",
|
|
|
|
example="jesopo"))
|
2020-05-07 21:23:04 +00:00
|
|
|
@utils.export("botset", SETTING_YT)
|
|
|
|
@utils.export("serverset", SETTING_YT)
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-09-22 23:55:36 +00:00
|
|
|
_name = "last.fm"
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.np", alias_of="nowplaying")
|
|
|
|
@utils.hook("received.command.listening", alias_of="nowplaying")
|
|
|
|
@utils.hook("received.command.nowplaying")
|
2016-03-29 11:56:58 +00:00
|
|
|
def np(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get the last listened to track from a user
|
|
|
|
:usage: [username]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-06-03 15:00:53 +00:00
|
|
|
user = None
|
|
|
|
lastfm_username = None
|
|
|
|
shown_username = None
|
|
|
|
|
|
|
|
if event["args"]:
|
|
|
|
arg_username = event["args_split"][0]
|
|
|
|
if event["server"].has_user_id(arg_username):
|
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
|
|
|
else:
|
|
|
|
lastfm_username = shown_username = arg_username
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2019-06-03 15:00:53 +00:00
|
|
|
user = event["user"]
|
|
|
|
|
|
|
|
if user:
|
|
|
|
lastfm_username = user.get_setting("lastfm", user.nickname)
|
|
|
|
shown_username = user.nickname
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(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"],
|
2019-11-25 18:17:30 +00:00
|
|
|
"format": "json", "limit": "1"}).json()
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
2019-11-25 18:17:30 +00:00
|
|
|
if "recenttracks" in page and len(page["recenttracks"]["track"]):
|
|
|
|
now_playing = page["recenttracks"]["track"]
|
2019-02-18 13:19:45 +00:00
|
|
|
if type(now_playing) == list:
|
|
|
|
now_playing = now_playing[0]
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
track_name = now_playing["name"]
|
|
|
|
artist = now_playing["artist"]["#text"]
|
|
|
|
|
2018-09-23 00:39:38 +00:00
|
|
|
if '@attr' in now_playing:
|
|
|
|
np = True
|
|
|
|
else:
|
|
|
|
played = int(now_playing["date"]["uts"])
|
2019-11-25 18:18:09 +00:00
|
|
|
dt = utils.datetime.utcnow()
|
2019-11-15 17:00:38 +00:00
|
|
|
np = bool((dt.timestamp()-played) < 120)
|
2018-09-23 00:39:38 +00:00
|
|
|
|
2019-11-15 17:00:38 +00:00
|
|
|
time_language = "is listening to" if np else "last listened to"
|
2018-09-23 00:39:38 +00:00
|
|
|
|
2019-11-15 17:00:38 +00:00
|
|
|
yt_url_str = ""
|
2020-05-07 21:23:04 +00:00
|
|
|
if event["server"].get_setting("lastfm-youtube",
|
|
|
|
self.bot.get_setting("lastfm-youtube", False)):
|
|
|
|
yt_url = self.exports.get("search-youtube")(
|
|
|
|
"%s - %s" % (artist, track_name))
|
|
|
|
if yt_url:
|
|
|
|
yt_url_str = " - %s" % yt_url
|
2018-09-22 23:55:36 +00:00
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
info_page = utils.http.request(URL_SCROBBLER, get_params={
|
2016-04-04 11:37:17 +00:00
|
|
|
"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"],
|
2019-11-25 18:17:30 +00:00
|
|
|
"user": lastfm_username, "format": "json"}).json()
|
2016-04-05 17:42:36 +00:00
|
|
|
|
2019-11-25 18:17:30 +00:00
|
|
|
track = info_page.get("track", {})
|
2019-11-15 17:00:38 +00:00
|
|
|
|
|
|
|
tags_str = ""
|
2019-11-22 11:48:10 +00:00
|
|
|
if "toptags" in track and track["toptags"]["tag"]:
|
2021-07-10 16:57:59 +00:00
|
|
|
tags_list = track["toptags"]["tag"]
|
|
|
|
if not type(tags_list) == list:
|
|
|
|
tags_list = [tags_list]
|
|
|
|
tags = [t["name"] for t in tags_list]
|
2019-11-15 17:00:38 +00:00
|
|
|
tags_str = " [%s]" % ", ".join(tags)
|
|
|
|
|
|
|
|
play_count_str = ""
|
|
|
|
if "userplaycount" in track:
|
|
|
|
play_count = int(track["userplaycount"])
|
|
|
|
if play_count > 0:
|
|
|
|
play_count_str = " (%d play%s)" % (play_count,
|
|
|
|
"" if play_count == 1 else "s")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-11-15 17:00:38 +00:00
|
|
|
track_name = utils.irc.bold("%s - %s" % (artist, track_name))
|
2019-11-15 16:34:28 +00:00
|
|
|
|
|
|
|
event["stdout"].write("%s %s: %s%s%s%s" % (
|
2019-11-15 17:00:38 +00:00
|
|
|
shown_username, time_language, track_name, play_count_str,
|
|
|
|
tags_str, yt_url_str))
|
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:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|