refactor lastfm.py
This commit is contained in:
parent
90baf17d10
commit
79a6fd6609
1 changed files with 26 additions and 27 deletions
|
@ -42,8 +42,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
"api_key": self.bot.config["lastfm-api-key"],
|
"api_key": self.bot.config["lastfm-api-key"],
|
||||||
"format": "json", "limit": "1"}, json=True)
|
"format": "json", "limit": "1"}, json=True)
|
||||||
if page:
|
if page:
|
||||||
if "recenttracks" in page.data and len(page.data["recenttracks"
|
if ("recenttracks" in page.data and
|
||||||
]["track"]):
|
len(page.data["recenttracks"]["track"])):
|
||||||
now_playing = page.data["recenttracks"]["track"]
|
now_playing = page.data["recenttracks"]["track"]
|
||||||
if type(now_playing) == list:
|
if type(now_playing) == list:
|
||||||
now_playing = now_playing[0]
|
now_playing = now_playing[0]
|
||||||
|
@ -55,43 +55,42 @@ class Module(ModuleManager.BaseModule):
|
||||||
np = True
|
np = True
|
||||||
else:
|
else:
|
||||||
played = int(now_playing["date"]["uts"])
|
played = int(now_playing["date"]["uts"])
|
||||||
dts = int(datetime.now(tz=timezone.utc).timestamp())
|
dt = utils.datetime.datetime_utcnow()
|
||||||
np = bool((dts - played) < 120)
|
np = bool((dt.timestamp()-played) < 120)
|
||||||
|
|
||||||
time_language = "is listening to" if np else "last " \
|
time_language = "is listening to" if np else "last listened to"
|
||||||
+ "listened to"
|
|
||||||
|
|
||||||
ytquery = " - ".join([artist, track_name])
|
yt_url = self.exports.get_one("search-youtube")(
|
||||||
|
"%s - %s" % (artist, track_name))
|
||||||
short_url = self.exports.get_one("search-youtube")(ytquery)
|
yt_url_str = ""
|
||||||
short_url = " - " + short_url if short_url else ""
|
if yt_url:
|
||||||
|
yt_url_str = " - %s" % yt_url
|
||||||
|
|
||||||
info_page = utils.http.request(URL_SCROBBLER, get_params={
|
info_page = utils.http.request(URL_SCROBBLER, get_params={
|
||||||
"method": "track.getInfo", "artist": artist,
|
"method": "track.getInfo", "artist": artist,
|
||||||
"track": track_name, "autocorrect": "1",
|
"track": track_name, "autocorrect": "1",
|
||||||
"api_key": self.bot.config["lastfm-api-key"],
|
"api_key": self.bot.config["lastfm-api-key"],
|
||||||
"user": lastfm_username, "format": "json"}, json=True)
|
"user": lastfm_username, "format": "json"}, json=True)
|
||||||
tags = []
|
|
||||||
if "toptags" in info_page.data.get("track", []):
|
|
||||||
for tag in info_page.data["track"]["toptags"]["tag"]:
|
|
||||||
tags.append(tag["name"])
|
|
||||||
if tags:
|
|
||||||
tags = " (%s)" % ", ".join(tags)
|
|
||||||
else:
|
|
||||||
tags = ""
|
|
||||||
|
|
||||||
play_count = ""
|
track = info_page.data.get("track", {})
|
||||||
if ("userplaycount" in info_page.data.get("track", {}) and
|
|
||||||
int(info_page.data["track"]["userplaycount"]) > 0):
|
|
||||||
play_count = int(info_page.data["track"]["userplaycount"])
|
|
||||||
play_count = " (%d play%s)" % (play_count,
|
|
||||||
"s" if play_count > 1 else "")
|
|
||||||
|
|
||||||
track = utils.irc.bold("%s - %s" % (artist, track_name))
|
tags_str = ""
|
||||||
|
if "toptags" in track:
|
||||||
|
tags = [t["name"] for t in track["toptags"]["tag"]]
|
||||||
|
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")
|
||||||
|
|
||||||
|
track_name = utils.irc.bold("%s - %s" % (artist, track_name))
|
||||||
|
|
||||||
event["stdout"].write("%s %s: %s%s%s%s" % (
|
event["stdout"].write("%s %s: %s%s%s%s" % (
|
||||||
shown_username, time_language, track, play_count, tags,
|
shown_username, time_language, track_name, play_count_str,
|
||||||
short_url))
|
tags_str, yt_url_str))
|
||||||
else:
|
else:
|
||||||
event["stderr"].write(
|
event["stderr"].write(
|
||||||
"The user '%s' has never scrobbled before" % (
|
"The user '%s' has never scrobbled before" % (
|
||||||
|
|
Loading…
Reference in a new issue