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/"
|
|
|
|
|
2019-05-23 09:28:14 +00:00
|
|
|
@utils.export("set", {"setting": "lastfm", "help": "Set last.fm username",
|
|
|
|
"example": "jesopo"})
|
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
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
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
|
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"],
|
|
|
|
"format": "json", "limit": "1"}, json=True)
|
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if "recenttracks" in page.data and len(page.data["recenttracks"
|
2016-03-29 11:56:58 +00:00
|
|
|
]["track"]):
|
2019-02-18 13:19:45 +00:00
|
|
|
now_playing = page.data["recenttracks"]["track"]
|
|
|
|
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"])
|
2018-09-23 01:21:43 +00:00
|
|
|
dts = int(datetime.now(tz=timezone.utc).timestamp())
|
|
|
|
np = bool((dts - played) < 120)
|
2018-09-23 00:39:38 +00:00
|
|
|
|
2018-09-23 01:21:43 +00:00
|
|
|
time_language = "is listening to" if np else "last " \
|
|
|
|
+ "listened to"
|
2018-09-23 00:39:38 +00:00
|
|
|
|
2018-09-22 23:55:36 +00:00
|
|
|
ytquery = " - ".join([artist, track_name])
|
|
|
|
|
2019-02-09 13:14:45 +00:00
|
|
|
short_url = self.exports.get_one("search-youtube")(ytquery)
|
2019-02-28 16:34:22 +00:00
|
|
|
short_url = " - " + short_url if short_url else ""
|
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"],
|
2018-08-13 15:04:08 +00:00
|
|
|
"user": lastfm_username, "format": "json"}, json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
tags = []
|
2018-12-11 22:26:38 +00:00
|
|
|
if "toptags" in info_page.data.get("track", []):
|
|
|
|
for tag in info_page.data["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 = ""
|
2019-02-28 16:22:35 +00:00
|
|
|
if ("userplaycount" in info_page.data.get("track", {}) and
|
2019-02-28 16:23:53 +00:00
|
|
|
int(info_page.data["track"]["userplaycount"]) > 0):
|
2018-12-11 22:26:38 +00:00
|
|
|
play_count = int(info_page.data["track"]["userplaycount"])
|
2016-04-05 17:42:36 +00:00
|
|
|
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(
|
2018-09-23 00:39:38 +00:00
|
|
|
"%s %s: %s - %s%s%s%s" % (
|
|
|
|
shown_username, time_language, artist, track_name,
|
|
|
|
play_count,
|
|
|
|
tags,
|
2018-09-22 23:55:36 +00:00
|
|
|
short_url))
|
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:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|