matrix-music-bot

A matrix / synapse chatbot written in Python.
git clone git://johnkubach.com:/srv/git/matrix-music-bot
Log | Files | Refs | README

music.py (2978B)


      1 #!/usr/bin/env python3
      2 
      3 import sys
      4 import re
      5 import configparser
      6 import functions.element as element
      7 from functions.extract_info import (
      8         get_video_info,
      9         get_artist_song,
     10         get_artist_info,
     11         get_similar_artists,
     12         get_tags
     13         )
     14 
     15 from matrix_client.client import MatrixClient
     16 from matrix_client.api import MatrixRequestError
     17 
     18 
     19 def parse_config():
     20     config = configparser.ConfigParser()
     21     config.read('config.ini')
     22 
     23     server = config['server']
     24     host = server['url']
     25     user = server['username']
     26     password = server['password']
     27     room_id = server['room_id']
     28 
     29     return host, user, password, room_id
     30 
     31 
     32 def process_url(event):
     33     message = event['content']['body']
     34     message = message.split()
     35     url = message[0]
     36     return url
     37 
     38 
     39 def music_command(message):
     40     if message.startswith('!song '):
     41         query = message.replace("!song ","")
     42     return query
     43 
     44 
     45 def youtube(url, isUrl=True):
     46     if isUrl:
     47         title, desc, weburl, artist, song = get_video_info(url)
     48     else:
     49         title, desc, weburl, artist, song = get_video_info(url, False)
     50 
     51     song_info = [title]
     52 
     53     if artist is None:
     54         artist, song = get_artist_song(title)
     55 
     56     if artist is not None:
     57         fulltitle = artist + " - " + song
     58         song_info = [fulltitle]
     59 
     60         if not isUrl:
     61             song_info.append("URL: " + weburl)
     62 
     63         tags = get_tags(artist)
     64         bio = get_artist_info(artist)
     65         similar_artists = get_similar_artists(artist)
     66 
     67     elif artist is None:
     68             song_info.append(desc)
     69 
     70     if tags and bio:
     71         song_info.append("Genre: " + tags)
     72         song_info.append("Similar Artists: " + similar_artists)
     73         song_info.append(re.sub('<.*?>', '', bio))
     74 
     75     return song_info
     76 
     77 
     78 def send_message(room, message):
     79     for m in message:
     80         room.send_text(m)
     81 
     82 
     83 def on_message(room, event):
     84     if event['type'] == 'm.room.message':
     85         if event['content']['msgtype'] == "m.text":
     86             if event['content']['body'].startswith('https://youtu') or event['content']['body'].startswith('https://www.youtu'):
     87                 url = process_url(event)
     88                 message = youtube(url)
     89                 send_message(room, message)
     90             elif event['content']['body'].startswith('!'):
     91                 query = music_command(event['content']['body'])
     92                 message = youtube(query, False)
     93                 send_message(room, message)
     94 
     95 
     96 def main():
     97     host, user, password, room_id = parse_config()
     98 
     99     client = MatrixClient(host)
    100 
    101     try:
    102         client.login_with_password(user, password)
    103     except MatrixRequestError as e:
    104         print(e)
    105         sys.exit(1)
    106 
    107     try:
    108         room = client.join_room(room_id)
    109     except MatrixRequestError as e:
    110         print(e)
    111         sys.exit(1)
    112 
    113     room.add_listener(on_message)
    114     client.start_listener_thread()
    115 
    116     while True:
    117         msg = element.get_input()
    118         room.send_text(msg)
    119 
    120 
    121 if __name__ == '__main__':
    122     main()