Update last.fm to include a youtube link. Also change the module name to last.fm

This commit is contained in:
dngfx 2018-09-23 00:55:36 +01:00
parent 9220f84c27
commit e5283ed2e2
2 changed files with 31 additions and 2 deletions

View file

@ -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" % (

View file

@ -24,6 +24,8 @@ 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)
exports.add("channelset", {"setting": "auto-youtube",
"help": "Disable/Enable automatically getting info from "
"youtube URLs", "validate": Utils.bool_or_none})
@ -63,6 +65,22 @@ 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):
search = event["query"]
video_id = ""
search_page = Utils.get_url(URL_YOUTUBESEARCH,
get_params={"q": search, "part": "snippet",
"maxResults": "1", "type": "video",
"key": self.bot.config["google-api-key"]},
json=True)
if search_page:
if search_page["pageInfo"]["totalResults"] > 0:
video_id = search_page["items"][0]["id"]["videoId"]
return "https://youtu.be/%s" % video_id
def yt(self, event):
video_id = None
search = None