diff --git a/modules/lastfm.py b/modules/lastfm.py index 9c18db14..c87fef61 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -42,8 +42,8 @@ class Module(ModuleManager.BaseModule): "api_key": self.bot.config["lastfm-api-key"], "format": "json", "limit": "1"}, json=True) if page: - if "recenttracks" in page.data and len(page.data["recenttracks" - ]["track"]): + if ("recenttracks" in page.data and + len(page.data["recenttracks"]["track"])): now_playing = page.data["recenttracks"]["track"] if type(now_playing) == list: now_playing = now_playing[0] @@ -55,43 +55,42 @@ class Module(ModuleManager.BaseModule): np = True else: played = int(now_playing["date"]["uts"]) - dts = int(datetime.now(tz=timezone.utc).timestamp()) - np = bool((dts - played) < 120) + dt = utils.datetime.datetime_utcnow() + np = bool((dt.timestamp()-played) < 120) - time_language = "is listening to" if np else "last " \ - + "listened to" + time_language = "is listening to" if np else "last listened to" - ytquery = " - ".join([artist, track_name]) - - short_url = self.exports.get_one("search-youtube")(ytquery) - short_url = " - " + short_url if short_url else "" + yt_url = self.exports.get_one("search-youtube")( + "%s - %s" % (artist, track_name)) + yt_url_str = "" + if yt_url: + yt_url_str = " - %s" % yt_url info_page = utils.http.request(URL_SCROBBLER, get_params={ "method": "track.getInfo", "artist": artist, "track": track_name, "autocorrect": "1", "api_key": self.bot.config["lastfm-api-key"], "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 = "" - 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 = info_page.data.get("track", {}) - 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" % ( - shown_username, time_language, track, play_count, tags, - short_url)) + shown_username, time_language, track_name, play_count_str, + tags_str, yt_url_str)) else: event["stderr"].write( "The user '%s' has never scrobbled before" % (