commit 475cca2caaf1934b98937453e96622c89dc07bd3
parent 5205b3d7e2c8002e12fe4699f6f67b7e2f0ac61a
Author: John Kubach <johnkubach@gmail.com>
Date: Wed, 5 Aug 2020 20:26:42 -0400
Cleanup on_message
Break out on_message function into separate youtube and send_message
functions. Should help with adding features later on.
Diffstat:
M | music.py | | | 41 | ++++++++++++++++++++++++++--------------- |
1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/music.py b/music.py
@@ -28,26 +28,37 @@ def parse_config():
return host, user, password, room_id
-def on_message(room, event):
- if event['type'] == 'm.room.message':
- if event['content']['msgtype'] == "m.text":
- if 'youtube.com' in event['content']['body'] or 'youtu.be' in event['content']['body']:
- message = event['content']['body']
- message = message.split()
- url = message[0]
+def youtube(event):
+ message = event['content']['body']
+ message = message.split()
+ url = message[0]
+
+ title = get_title(url)
+ song_info = [title]
+
+ artist, song = get_artist_song(title)
+
+ if artist:
+ tags = get_tags(artist)
+ bio = get_artist_info(artist)
- title = get_title(url)
- artist, song = get_artist_song(title)
+ song_info.append("Genre: " + tags)
+ song_info.append(re.sub('<.*?>', '', bio))
- room.send_text(title)
+ return song_info
- if artist:
- tags = get_tags(artist)
- bio = get_artist_info(artist)
- room.send_text("Genre: " + tags)
+def send_message(room, message):
+ for m in message:
+ room.send_text(m)
- room.send_text(re.sub('<.*?>', '', bio))
+
+def on_message(room, event):
+ if event['type'] == 'm.room.message':
+ if event['content']['msgtype'] == "m.text":
+ if 'youtube.com' in event['content']['body'] or 'youtu.be' in event['content']['body']:
+ message = youtube(event)
+ send_message(room, message)
def main():