commit 123ad0c4a4672fa340326e10dbb4d4bdd89abea3
parent 4c42df81816cb81ae2896a26ad5f26c9c85290e0
Author: John <mail@johnkubach.com>
Date:   Wed, 19 Oct 2022 21:38:14 -0400
Add Ability to Query For Song
Instead of copy & pasting a youtube url, a message can be prefixed with
!song to pull the first result off of youtube of a given query.
Diffstat:
2 files changed, 53 insertions(+), 22 deletions(-)
diff --git a/functions/extract_info.py b/functions/extract_info.py
@@ -3,21 +3,29 @@ import pylast
 from youtube_title_parse import get_artist_title
 from functions.lastfm_init import lastfm_network
 
-def get_video_info(url):
-    artist = None
-    Song = None
+def get_video_info(url, isUrl=True):
     ytdl_opts = {'source_address': '0.0.0.0'}
     ydl = yt_dlp.YoutubeDL(ytdl_opts)
     with ydl:
-        video = ydl.extract_info(url, download=False)
+        if isUrl:
+            video = ydl.extract_info(url, download=False)
+        else:
+            video = ydl.extract_info(f"ytsearch:{url}", download=False)['entries'][0]
+
         title = video['title']
         description = video['description']
-        if video['artist']:
-            artist = video['artist']
-        if video['track']:
-            song = video['track']
+        weburl = video['webpage_url']
+
+        try:
+            if video['artist']:
+                artist = video['artist']
+                if video['track']:
+                    song = video['track']
+        except KeyError as e:
+            artist = None
+            song = None
 
-    return (title, description, artist, song)
+    return (title, description, weburl, artist, song)
 
 def get_artist_song(title):
     try:
diff --git a/music.py b/music.py
@@ -29,31 +29,49 @@ def parse_config():
     return host, user, password, room_id
 
 
-def youtube(event):
+def process_url(event):
     message = event['content']['body']
     message = message.split()
     url = message[0]
-    
-    title, desc, artist, song = get_video_info(url)
+    return url
+
+
+def music_command(message):
+    if message.startswith('!song '):
+        query = message.replace("!song ","")
+    return query
+
+
+def youtube(url, isUrl=True):
+    if isUrl:
+        title, desc, weburl, artist, song = get_video_info(url)
+    else:
+        title, desc, weburl, artist, song = get_video_info(url, False)
+
     song_info = [title]
 
+    if artist is None:
+        artist, song = get_artist_song(title)
 
-    if artist and song:
+    if artist is not None:
         fulltitle = artist + " - " + song
+        song_info = [fulltitle]
+
+        if not isUrl:
+            song_info.append("URL: " + weburl)
+
         tags = get_tags(artist)
         bio = get_artist_info(artist)
         similar_artists = get_similar_artists(artist)
-        song_info = [fulltitle]
-        if tags and bio:
-            song_info.append("Genre: " + tags)
-            song_info.append("Similar Artists: " + similar_artists)
-            song_info.append(re.sub('<.*?>', '', bio))
-    else:
-        artist, song = get_artist_song(title)
 
-        if artist == None:
+    elif artist is None:
             song_info.append(desc)
 
+    if tags and bio:
+        song_info.append("Genre: " + tags)
+        song_info.append("Similar Artists: " + similar_artists)
+        song_info.append(re.sub('<.*?>', '', bio))
+
     return song_info
 
 
@@ -66,7 +84,12 @@ def on_message(room, event):
     if event['type'] == 'm.room.message':
         if event['content']['msgtype'] == "m.text":
             if event['content']['body'].startswith('https://youtu') or event['content']['body'].startswith('https://www.youtu'):
-                message = youtube(event)
+                url = process_url(event)
+                message = youtube(url)
+                send_message(room, message)
+            elif event['content']['body'].startswith('!'):
+                query = music_command(event['content']['body'])
+                message = youtube(query, False)
                 send_message(room, message)