element_music_bot

An element chatbot written in Python.
Log | Files | Refs | README

commit 8786e09c51397db537ac5b334096f09c48219263
Author: John Kubach <johnkubach@gmail.com>
Date:   Sun,  2 Aug 2020 13:47:30 -0400

Initial Commit

Diffstat:
Aconfig.ini | 11+++++++++++
Aelement_functions.py | 25+++++++++++++++++++++++++
Aextract_info_functions.py | 36++++++++++++++++++++++++++++++++++++
Alastfm_init.py | 34++++++++++++++++++++++++++++++++++
Amusic.py | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 186 insertions(+), 0 deletions(-)

diff --git a/config.ini b/config.ini @@ -0,0 +1,11 @@ +[server] +url = +username = +password = +room_id = + +[lastfm] +username = +password = +api = +api_secret = diff --git a/element_functions.py b/element_functions.py @@ -0,0 +1,25 @@ +from getpass import getpass + +try: + get_input = raw_input +except NameError: + get_input = input + + +def get_user_details(argv): + try: + host = argv[1] + except IndexError: + host = get_input("Host (ex: http://localhost:8008 ): ") + + try: + username = argv[2] + except IndexError: + username = get_input("Username: ") + + try: + password = argv[3] + except IndexError: + password = getpass() + + return host, username, password diff --git a/extract_info_functions.py b/extract_info_functions.py @@ -0,0 +1,36 @@ +import youtube_dl +import pylast +from youtube_title_parse import get_artist_title +from lastfm_init import lastfm_network + +def get_title(url): + ytdl_opts = {'source_address': '0.0.0.0'} + ydl = youtube_dl.YoutubeDL(ytdl_opts) + with ydl: + video = ydl.extract_info(url, download=False) + title = video['title'] + + return title + +def get_artist_song(title): + try: + artist, song = get_artist_title(title) + except TypeError as e: + artist = None + song = None + + return artist, song + +def get_artist_info(artist): + info = lastfm_network.get_artist(artist).get_bio_summary() + return info + +def get_tags(artist): + info = lastfm_network.get_artist(artist).get_top_tags(limit=2) + genre_list = [] + for t in info: + genre_list.append(t.item.get_name()) + + format_tags = ', '.join(genre_list) + + return format_tags diff --git a/lastfm_init.py b/lastfm_init.py @@ -0,0 +1,34 @@ +import os +import sys +import configparser +import pylast + +config = configparser.ConfigParser() +config.read('config.ini') + +credentials = config['lastfm'] +user = credentials['username'] +passw = credentials['password'] +api_key = credentials['api'] +api_secret = credentials['api_secret'] + +try: + API_KEY = os.environ["LASTFM_API_KEY"] + API_SECRET = os.environ["LASTFM_API_SECRET"] +except KeyError: + API_KEY = api_key + API_SECRET = api_secret + +try: + lastfm_username = os.environ["LASTFM_USERNAME"] + lastfm_password_hash = os.environ["LASTFM_PASSWORD_HASH"] +except KeyError: + lastfm_username = user + lastfm_password_hash = passw + +lastfm_network = pylast.LastFMNetwork( + api_key=API_KEY, + api_secret=API_SECRET, + username=lastfm_username, + password_hash=lastfm_password_hash, +) diff --git a/music.py b/music.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +import sys +import re +import configparser +import element_functions +from extract_info_functions import ( + get_title, + get_artist_song, + get_artist_info, + get_tags + ) + +from Naked.toolshed.shell import execute_js, muterun_js +from matrix_client.client import MatrixClient +from matrix_client.api import MatrixRequestError + + +def parse_config(): + config = configparser.ConfigParser() + config.read('config.ini') + + server = config['server'] + host = server['url'] + user = server['username'] + password = server['password'] + room_id = server['room_id'] + + 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']: + words = event['content']['body'] + words = words.split() + url = words[0] + + title = get_title(url) + artist, song = get_artist_song(title) + + room.send_text(title) + + if artist: + tags = get_tags(artist) + bio = get_artist_info(artist) + + room.send_text("Genre: " + tags) + + room.send_text(re.sub('<.*?>', '', bio)) + + +def main(): + host, user, password, room_id = parse_config() + + client = MatrixClient(host) + + try: + client.login_with_password(user, password) + except MatrixRequestError as e: + print(e) + sys.exit(1) + + try: + room = client.join_room(room_id) + except MatrixRequestError as e: + print(e) + sys.exit(1) + + room.add_listener(on_message) + client.start_listener_thread() + + while True: + msg = element_functions.get_input() + room.send_text(msg) + + +if __name__ == '__main__': + main()