aboutsummaryrefslogtreecommitdiff
path: root/modules/lastfm.py
diff options
context:
space:
mode:
authorGravatar jesopo2018-09-24 12:28:33 +0100
committerGravatar GitHub2018-09-24 12:28:33 +0100
commit7522b0fae22c41c8e57d5494a239c97736e2c2b6 (patch)
tree0da6d00ce4bb86ddc88acff85ef6e3ef3f7fba4b /modules/lastfm.py
parentAsk for alias of first server in start.py (diff)
parentMerge pull request #1 from jesopo/master (diff)
Merge pull request #23 from dngfx/master
Make tweets say Twitter instead of tweets, and update ducks
Diffstat (limited to 'modules/lastfm.py')
-rw-r--r--modules/lastfm.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/modules/lastfm.py b/modules/lastfm.py
index e448d9ea..68b6249b 100644
--- a/modules/lastfm.py
+++ b/modules/lastfm.py
@@ -1,12 +1,15 @@
#--require-config lastfm-api-key
import Utils
+from datetime import datetime, timezone
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 +37,24 @@ 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"])
+ dts = int(datetime.now(tz=timezone.utc).timestamp())
+ np = bool((dts - played) < 120)
+
+ time_language = "is listening to" if np else "last " \
+ + "listened to"
+
+ ytquery = " - ".join([artist, track_name])
+
+ short_url = self.events.on(
+ "get.searchyoutube").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 +76,11 @@ 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 %s: %s - %s%s%s%s" % (
+ shown_username, time_language, artist, track_name,
+ play_count,
+ tags,
+ short_url))
else:
event["stderr"].write(
"The user '%s' has never scrobbled before" % (