From e5283ed2e20e95df58c7a162fe76f88ebf3694e1 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 23 Sep 2018 00:55:36 +0100 Subject: Update last.fm to include a youtube link. Also change the module name to last.fm --- modules/lastfm.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'modules/lastfm.py') diff --git a/modules/lastfm.py b/modules/lastfm.py index e448d9ea..3675bbd5 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -5,8 +5,10 @@ import Utils URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/" class Module(object): + _name = "last.fm" def __init__(self, bot, events, exports): self.bot = bot + self.events = events exports.add("set", {"setting": "lastfm", "help": "Set username on last.fm"}) @@ -34,6 +36,14 @@ class Module(object): track_name = now_playing["name"] artist = now_playing["artist"]["#text"] + ytquery = " - ".join([artist, track_name]) + + short_url = self.events.on( + "get.youtubefromlastfm").call_for_result( + query=ytquery) + + short_url = " -- " + short_url if short_url else "" + info_page = Utils.get_url(URL_SCROBBLER, get_params={ "method": "track.getInfo", "artist": artist, "track": track_name, "autocorrect": "1", @@ -55,8 +65,9 @@ class Module(object): "s" if play_count > 1 else "") event["stdout"].write( - "%s is now playing: %s - %s%s%s" % ( - shown_username, artist, track_name, play_count, tags)) + "%s is now playing: %s - %s%s%s%s" % ( + shown_username, artist, track_name, play_count, tags, + short_url)) else: event["stderr"].write( "The user '%s' has never scrobbled before" % ( -- cgit v1.3.1-10-gc9f91 From d335deed40423c9a64de6bcffde301086e798822 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 23 Sep 2018 01:39:38 +0100 Subject: make last.fm say "is now playing", if they're currently playing. --- modules/lastfm.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'modules/lastfm.py') diff --git a/modules/lastfm.py b/modules/lastfm.py index 3675bbd5..35c903ba 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -1,6 +1,7 @@ #--require-config lastfm-api-key import Utils +import time URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/" @@ -36,6 +37,16 @@ class Module(object): track_name = now_playing["name"] artist = now_playing["artist"]["#text"] + if '@attr' in now_playing: + np = True + else: + played = int(now_playing["date"]["uts"]) + timenow = int(time.time()) + np = bool(timenow - played > 240) + + time_language = "last listened to" if np == False \ + else "is now playing" + ytquery = " - ".join([artist, track_name]) short_url = self.events.on( @@ -65,8 +76,10 @@ class Module(object): "s" if play_count > 1 else "") event["stdout"].write( - "%s is now playing: %s - %s%s%s%s" % ( - shown_username, artist, track_name, play_count, tags, + "%s %s: %s - %s%s%s%s" % ( + shown_username, time_language, artist, track_name, + play_count, + tags, short_url)) else: event["stderr"].write( -- cgit v1.3.1-10-gc9f91 From bb15f8b8e2383a84b1e28bb6ee85f01687df819e Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 23 Sep 2018 02:21:43 +0100 Subject: make last.fm say "is now playing", if they're currently playing. --- modules/lastfm.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'modules/lastfm.py') diff --git a/modules/lastfm.py b/modules/lastfm.py index 35c903ba..0d6873a2 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -1,7 +1,7 @@ #--require-config lastfm-api-key import Utils -import time +from datetime import datetime, timezone URL_SCROBBLER = "http://ws.audioscrobbler.com/2.0/" @@ -41,11 +41,11 @@ class Module(object): np = True else: played = int(now_playing["date"]["uts"]) - timenow = int(time.time()) - np = bool(timenow - played > 240) + dts = int(datetime.now(tz=timezone.utc).timestamp()) + np = bool((dts - played) < 120) - time_language = "last listened to" if np == False \ - else "is now playing" + time_language = "is listening to" if np else "last " \ + + "listened to" ytquery = " - ".join([artist, track_name]) -- cgit v1.3.1-10-gc9f91 From 8b8c59ee213a4c006f90767fe379030d4a1e1524 Mon Sep 17 00:00:00 2001 From: dngfx Date: Sun, 23 Sep 2018 10:23:12 +0100 Subject: Rename the functions to be more generic. --- modules/lastfm.py | 2 +- modules/youtube.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/lastfm.py') diff --git a/modules/lastfm.py b/modules/lastfm.py index 0d6873a2..68b6249b 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -50,7 +50,7 @@ class Module(object): ytquery = " - ".join([artist, track_name]) short_url = self.events.on( - "get.youtubefromlastfm").call_for_result( + "get.searchyoutube").call_for_result( query=ytquery) short_url = " -- " + short_url if short_url else "" diff --git a/modules/youtube.py b/modules/youtube.py index 20dc072b..c70e9ef8 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -24,7 +24,7 @@ class Module(object): help="Find a video on youtube", usage="[query]") events.on("received.message.channel").hook(self.channel_message) - events.on("get.youtubefromlastfm").hook(self.get_yt_from_lastfm) + events.on("get.searchyoutube").hook(self.search_video) exports.add("channelset", {"setting": "auto-youtube", "help": "Disable/Enable automatically getting info from " @@ -65,7 +65,7 @@ class Module(object): video_title, video_duration, video_uploader, "{:,}".format( int(video_views)), video_opinions, URL_YOUTUBESHORT % video_id) - def get_yt_from_lastfm(self, event): + def search_video(self, event): search = event["query"] video_id = "" -- cgit v1.3.1-10-gc9f91