| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from matrix_bot_api.matrix_bot_api import MatrixBotAPI
- from matrix_bot_api.mhandler import MHandler
- from matrix_bot_api.mregex_handler import MRegexHandler
- from matrix_bot_api.mcommand_handler import MCommandHandler
- import configparser
- import youtube_dl
- import eyed3
- import os
- from pathlib import Path
- #tempdir, final dir required
- def addsong_callback(room, event):
- args = event['content']['body'].split(',')
- args.pop(0)
- ydl_options = {
- 'format': 'bestaudio/best','postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- 'preferredquality': '192',
- },
- ],
- }
- room.send_notice("Downloading and converting...")
- with youtube_dl.YoutubeDL(ydl_options) as ydl:
- ydl.download([args[0]])
- room.send_notice("Moving and renaming...")
- for filename in os.listdir("."):
- if filename.endswith(".mp3"):
- p = Path(filename)
- song = eyed3.load(filename)
- song.tag.artist = args[2]
- song.tag.title = args[1]
- song.tag.save()
- p.rename('/home/whoishex/Music/'+args[1]+"-"+args[2]+".mp3")
- room.send_notice("Done!")
- def songhelp_callback(room,event):
- help_str = "!addsong - Add a song, in the form URL, Title, Artist. Don't put extra spaces and delimit with commas. \n"
- help_str += "!songhelp - Print this\n"
- help_str += "example: !addsong,https://youtu.be/yzdZj_9DxVs,Zero Machine,Le Castle Vania"
- room.send_notice(help_str)
- def main():
- config = configparser.ConfigParser()
- config.read("config.ini")
- username = config.get("Matrix","Username")
- password = config.get("Matrix","Password")
- server = config.get("Matrix", "Homeserver")
- global url
- global title
- global artist
- bot = MatrixBotAPI(username, password, server)
- m_addsong_handler = MCommandHandler('addsong', addsong_callback)
- bot.add_handler(m_addsong_handler)
- m_songhelp_handler = MCommandHandler('songhelp', songhelp_callback)
- bot.add_handler(m_songhelp_handler)
- bot.start_polling()
- while True:
- input()
- if __name__ == "__main__":
- main()
|