Merge pull request #41 from BafDyce/config-param
Add optional command line option for config file
This commit is contained in:
commit
73c69fc013
90
mumbleBot.py
90
mumbleBot.py
@ -20,17 +20,11 @@ import youtube_dl
|
|||||||
import media
|
import media
|
||||||
import util
|
import util
|
||||||
|
|
||||||
|
|
||||||
class MumbleBot:
|
class MumbleBot:
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
signal.signal(signal.SIGINT, self.ctrl_caught)
|
signal.signal(signal.SIGINT, self.ctrl_caught)
|
||||||
|
|
||||||
self.config = configparser.ConfigParser(interpolation=None)
|
self.volume = var.config.getfloat('bot', 'volume')
|
||||||
self.config.read("configuration.ini", encoding='latin-1')
|
|
||||||
|
|
||||||
|
|
||||||
self.volume = self.config.getfloat('bot', 'volume')
|
|
||||||
|
|
||||||
self.channel = args.channel
|
self.channel = args.channel
|
||||||
var.current_music = None
|
var.current_music = None
|
||||||
|
|
||||||
@ -52,8 +46,8 @@ class MumbleBot:
|
|||||||
var.playlist = []
|
var.playlist = []
|
||||||
|
|
||||||
var.user = args.user
|
var.user = args.user
|
||||||
var.music_folder = self.config.get('bot', 'music_folder')
|
var.music_folder = var.config.get('bot', 'music_folder')
|
||||||
var.is_proxified = self.config.getboolean("bot", "is_proxified")
|
var.is_proxified = var.config.getboolean("bot", "is_proxified")
|
||||||
self.exit = False
|
self.exit = False
|
||||||
self.nb_exit = 0
|
self.nb_exit = 0
|
||||||
self.thread = None
|
self.thread = None
|
||||||
@ -65,7 +59,7 @@ class MumbleBot:
|
|||||||
tt.start()
|
tt.start()
|
||||||
|
|
||||||
self.mumble = pymumble.Mumble(args.host, user=args.user, port=args.port, password=args.password,
|
self.mumble = pymumble.Mumble(args.host, user=args.user, port=args.port, password=args.password,
|
||||||
debug=self.config.getboolean('debug', 'mumbleConnection'))
|
debug=var.config.getboolean('debug', 'mumbleConnection'))
|
||||||
self.mumble.callbacks.set_callback("text_received", self.message_received)
|
self.mumble.callbacks.set_callback("text_received", self.message_received)
|
||||||
|
|
||||||
self.mumble.set_codec_profile("audio")
|
self.mumble.set_codec_profile("audio")
|
||||||
@ -102,8 +96,8 @@ class MumbleBot:
|
|||||||
|
|
||||||
print(command + ' - ' + parameter + ' by ' + self.mumble.users[text.actor]['name'])
|
print(command + ' - ' + parameter + ' by ' + self.mumble.users[text.actor]['name'])
|
||||||
|
|
||||||
if command == self.config.get('command', 'play_file') and parameter:
|
if command == var.config.get('command', 'play_file') and parameter:
|
||||||
music_folder = self.config.get('bot', 'music_folder')
|
music_folder = var.config.get('bot', 'music_folder')
|
||||||
# sanitize "../" and so on
|
# sanitize "../" and so on
|
||||||
path = os.path.abspath(os.path.join(music_folder, parameter))
|
path = os.path.abspath(os.path.join(music_folder, parameter))
|
||||||
if path.startswith(music_folder):
|
if path.startswith(music_folder):
|
||||||
@ -114,52 +108,52 @@ class MumbleBot:
|
|||||||
# try to do a partial match
|
# try to do a partial match
|
||||||
matches = [file for file in util.get_recursive_filelist_sorted(music_folder) if parameter.lower() in file.lower()]
|
matches = [file for file in util.get_recursive_filelist_sorted(music_folder) if parameter.lower() in file.lower()]
|
||||||
if len(matches) == 0:
|
if len(matches) == 0:
|
||||||
self.mumble.users[text.actor].send_message(self.config.get('strings', 'no_file'))
|
self.mumble.users[text.actor].send_message(var.config.get('strings', 'no_file'))
|
||||||
elif len(matches) == 1:
|
elif len(matches) == 1:
|
||||||
var.playlist.append(["file", matches[0]])
|
var.playlist.append(["file", matches[0]])
|
||||||
else:
|
else:
|
||||||
msg = self.config.get('strings', 'multiple_matches') + '<br />'
|
msg = var.config.get('strings', 'multiple_matches') + '<br />'
|
||||||
msg += '<br />'.join(matches)
|
msg += '<br />'.join(matches)
|
||||||
self.mumble.users[text.actor].send_message(msg)
|
self.mumble.users[text.actor].send_message(msg)
|
||||||
else:
|
else:
|
||||||
self.mumble.users[text.actor].send_message(self.config.get('strings', 'bad_file'))
|
self.mumble.users[text.actor].send_message(var.config.get('strings', 'bad_file'))
|
||||||
|
|
||||||
elif command == self.config.get('command', 'play_url') and parameter:
|
elif command == var.config.get('command', 'play_url') and parameter:
|
||||||
var.playlist.append(["url", parameter])
|
var.playlist.append(["url", parameter])
|
||||||
|
|
||||||
elif command == self.config.get('command', 'play_radio') and parameter:
|
elif command == var.config.get('command', 'play_radio') and parameter:
|
||||||
var.playlist.append(["radio", parameter])
|
var.playlist.append(["radio", parameter])
|
||||||
|
|
||||||
elif command == self.config.get('command', 'help'):
|
elif command == var.config.get('command', 'help'):
|
||||||
self.send_msg_channel(self.config.get('strings', 'help'))
|
self.send_msg_channel(var.config.get('strings', 'help'))
|
||||||
|
|
||||||
elif command == self.config.get('command', 'stop'):
|
elif command == var.config.get('command', 'stop'):
|
||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
elif command == self.config.get('command', 'kill'):
|
elif command == var.config.get('command', 'kill'):
|
||||||
if self.is_admin(text.actor):
|
if self.is_admin(text.actor):
|
||||||
self.stop()
|
self.stop()
|
||||||
self.exit = True
|
self.exit = True
|
||||||
else:
|
else:
|
||||||
self.mumble.users[text.actor].send_message(self.config.get('strings', 'not_admin'))
|
self.mumble.users[text.actor].send_message(var.config.get('strings', 'not_admin'))
|
||||||
|
|
||||||
elif command == self.config.get('command', 'stop_and_getout'):
|
elif command == var.config.get('command', 'stop_and_getout'):
|
||||||
self.stop()
|
self.stop()
|
||||||
if self.channel:
|
if self.channel:
|
||||||
self.mumble.channels.find_by_name(self.channel).move_in()
|
self.mumble.channels.find_by_name(self.channel).move_in()
|
||||||
|
|
||||||
elif command == self.config.get('command', 'joinme'):
|
elif command == var.config.get('command', 'joinme'):
|
||||||
self.mumble.users.myself.move_in(self.mumble.users[text.actor]['channel_id'])
|
self.mumble.users.myself.move_in(self.mumble.users[text.actor]['channel_id'])
|
||||||
|
|
||||||
elif command == self.config.get('command', 'volume'):
|
elif command == var.config.get('command', 'volume'):
|
||||||
if parameter is not None and parameter.isdigit() and 0 <= int(parameter) <= 100:
|
if parameter is not None and parameter.isdigit() and 0 <= int(parameter) <= 100:
|
||||||
self.volume = float(float(parameter) / 100)
|
self.volume = float(float(parameter) / 100)
|
||||||
self.send_msg_channel(self.config.get('strings', 'change_volume') % (
|
self.send_msg_channel(var.config.get('strings', 'change_volume') % (
|
||||||
int(self.volume * 100), self.mumble.users[text.actor]['name']))
|
int(self.volume * 100), self.mumble.users[text.actor]['name']))
|
||||||
else:
|
else:
|
||||||
self.send_msg_channel(self.config.get('strings', 'current_volume') % int(self.volume * 100))
|
self.send_msg_channel(var.config.get('strings', 'current_volume') % int(self.volume * 100))
|
||||||
|
|
||||||
elif command == self.config.get('command', 'current_music'):
|
elif command == var.config.get('command', 'current_music'):
|
||||||
if var.current_music:
|
if var.current_music:
|
||||||
source = var.current_music[0]
|
source = var.current_music[0]
|
||||||
if source == "radio":
|
if source == "radio":
|
||||||
@ -181,38 +175,38 @@ class MumbleBot:
|
|||||||
var.current_music[2],
|
var.current_music[2],
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
reply = self.config.get('strings', 'not_playing')
|
reply = var.config.get('strings', 'not_playing')
|
||||||
|
|
||||||
self.mumble.users[text.actor].send_message(reply)
|
self.mumble.users[text.actor].send_message(reply)
|
||||||
|
|
||||||
elif command == self.config.get('command', 'next'):
|
elif command == var.config.get('command', 'next'):
|
||||||
var.current_music = [var.playlist[0][0], var.playlist[0][1], None, None]
|
var.current_music = [var.playlist[0][0], var.playlist[0][1], None, None]
|
||||||
var.playlist.pop(0)
|
var.playlist.pop(0)
|
||||||
self.launch_next()
|
self.launch_next()
|
||||||
elif command == self.config.get('command', 'list'):
|
elif command == var.config.get('command', 'list'):
|
||||||
folder_path = self.config.get('bot', 'music_folder')
|
folder_path = var.config.get('bot', 'music_folder')
|
||||||
|
|
||||||
files = util.get_recursive_filelist_sorted(folder_path)
|
files = util.get_recursive_filelist_sorted(folder_path)
|
||||||
if files :
|
if files :
|
||||||
self.mumble.users[text.actor].send_message('<br>'.join(files))
|
self.mumble.users[text.actor].send_message('<br>'.join(files))
|
||||||
else :
|
else :
|
||||||
self.mumble.users[text.actor].send_message(self.config.get('strings', 'no_file'))
|
self.mumble.users[text.actor].send_message(var.config.get('strings', 'no_file'))
|
||||||
|
|
||||||
elif command == self.config.get('command', 'queue'):
|
elif command == var.config.get('command', 'queue'):
|
||||||
if len(var.playlist) == 0:
|
if len(var.playlist) == 0:
|
||||||
msg = self.config.get('strings', 'queue_empty')
|
msg = var.config.get('strings', 'queue_empty')
|
||||||
else:
|
else:
|
||||||
msg = self.config.get('strings', 'queue_contents') + '<br />'
|
msg = var.config.get('strings', 'queue_contents') + '<br />'
|
||||||
for (type, path) in var.playlist:
|
for (type, path) in var.playlist:
|
||||||
msg += '({}) {}<br />'.format(type, path)
|
msg += '({}) {}<br />'.format(type, path)
|
||||||
|
|
||||||
self.send_msg_channel(msg)
|
self.send_msg_channel(msg)
|
||||||
else:
|
else:
|
||||||
self.mumble.users[text.actor].send_message(self.config.get('strings', 'bad_command'))
|
self.mumble.users[text.actor].send_message(var.config.get('strings', 'bad_command'))
|
||||||
|
|
||||||
def launch_play_file(self, path):
|
def launch_play_file(self, path):
|
||||||
self.stop()
|
self.stop()
|
||||||
if self.config.getboolean('debug', 'ffmpeg'):
|
if var.config.getboolean('debug', 'ffmpeg'):
|
||||||
ffmpeg_debug = "debug"
|
ffmpeg_debug = "debug"
|
||||||
else:
|
else:
|
||||||
ffmpeg_debug = "warning"
|
ffmpeg_debug = "warning"
|
||||||
@ -222,7 +216,7 @@ class MumbleBot:
|
|||||||
|
|
||||||
def is_admin(self, user):
|
def is_admin(self, user):
|
||||||
username = self.mumble.users[user]['name']
|
username = self.mumble.users[user]['name']
|
||||||
list_admin = self.config.get('bot', 'admin').split(';')
|
list_admin = var.config.get('bot', 'admin').split(';')
|
||||||
if username in list_admin:
|
if username in list_admin:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
@ -239,7 +233,7 @@ class MumbleBot:
|
|||||||
var.current_music[1] = url
|
var.current_music[1] = url
|
||||||
|
|
||||||
elif var.current_music[0] == "file":
|
elif var.current_music[0] == "file":
|
||||||
path = self.config.get('bot', 'music_folder') + var.current_music[1]
|
path = var.config.get('bot', 'music_folder') + var.current_music[1]
|
||||||
title = var.current_music[1]
|
title = var.current_music[1]
|
||||||
|
|
||||||
elif var.current_music[0] == "radio":
|
elif var.current_music[0] == "radio":
|
||||||
@ -250,7 +244,7 @@ class MumbleBot:
|
|||||||
path = url
|
path = url
|
||||||
title = media.get_radio_server_description(url)
|
title = media.get_radio_server_description(url)
|
||||||
|
|
||||||
if self.config.getboolean('debug', 'ffmpeg'):
|
if var.config.getboolean('debug', 'ffmpeg'):
|
||||||
ffmpeg_debug = "debug"
|
ffmpeg_debug = "debug"
|
||||||
else:
|
else:
|
||||||
ffmpeg_debug = "warning"
|
ffmpeg_debug = "warning"
|
||||||
@ -262,7 +256,7 @@ class MumbleBot:
|
|||||||
|
|
||||||
def download_music(self, url):
|
def download_music(self, url):
|
||||||
url_hash = hashlib.md5(url.encode()).hexdigest()
|
url_hash = hashlib.md5(url.encode()).hexdigest()
|
||||||
path = self.config.get('bot', 'tmp_folder') + url_hash + ".mp3"
|
path = var.config.get('bot', 'tmp_folder') + url_hash + ".mp3"
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
'outtmpl': path,
|
'outtmpl': path,
|
||||||
@ -318,7 +312,7 @@ class MumbleBot:
|
|||||||
var.playlist = []
|
var.playlist = []
|
||||||
|
|
||||||
def set_comment(self):
|
def set_comment(self):
|
||||||
self.mumble.users.myself.comment(self.config.get('bot', 'comment'))
|
self.mumble.users.myself.comment(var.config.get('bot', 'comment'))
|
||||||
|
|
||||||
def send_msg_channel(self, msg, channel=None):
|
def send_msg_channel(self, msg, channel=None):
|
||||||
if not channel:
|
if not channel:
|
||||||
@ -334,6 +328,9 @@ def start_web_interface(addr, port):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='Bot for playing radio stream on Mumble')
|
parser = argparse.ArgumentParser(description='Bot for playing radio stream on Mumble')
|
||||||
|
|
||||||
|
# General arguments
|
||||||
|
parser.add_argument("--config", dest='config', type=str, default='configuration.ini', help='Load configuration from this file. Default: configuration.ini')
|
||||||
|
|
||||||
# Mumble arguments
|
# Mumble arguments
|
||||||
parser.add_argument("-s", "--server", dest="host", type=str, required=True, help="The server's hostame of a mumble server")
|
parser.add_argument("-s", "--server", dest="host", type=str, required=True, help="The server's hostame of a mumble server")
|
||||||
parser.add_argument("-u", "--user", dest="user", type=str, required=True, help="Username you wish, Default=abot")
|
parser.add_argument("-u", "--user", dest="user", type=str, required=True, help="Username you wish, Default=abot")
|
||||||
@ -346,4 +343,11 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('--wi-addr', dest='wi_addr', type=str, default=None, help='Listening address of the web interface')
|
parser.add_argument('--wi-addr', dest='wi_addr', type=str, default=None, help='Listening address of the web interface')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
config = configparser.ConfigParser(interpolation=None)
|
||||||
|
parsed_configs = config.read(args.config, encoding='latin-1')
|
||||||
|
if len(parsed_configs) == 0:
|
||||||
|
print('Could not read configuration from file \"{}\"'.format(args.config), file=sys.stderr)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
var.config = config
|
||||||
botamusique = MumbleBot(args)
|
botamusique = MumbleBot(args)
|
||||||
|
10
util.py
10
util.py
@ -1,25 +1,21 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import configparser
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import magic
|
import magic
|
||||||
import os
|
import os
|
||||||
import variables as var
|
import variables as var
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
__CONFIG = configparser.ConfigParser(interpolation=None)
|
|
||||||
__CONFIG.read("configuration.ini", encoding='latin-1')
|
|
||||||
|
|
||||||
def get_recursive_filelist_sorted(path):
|
def get_recursive_filelist_sorted(path):
|
||||||
filelist = []
|
filelist = []
|
||||||
for root, dirs, files in os.walk(path):
|
for root, dirs, files in os.walk(path):
|
||||||
relroot = root.replace(path, '')
|
relroot = root.replace(path, '')
|
||||||
if relroot != '' and relroot in __CONFIG.get('bot', 'ignored_folders'):
|
if relroot != '' and relroot in var.config.get('bot', 'ignored_folders'):
|
||||||
continue
|
continue
|
||||||
if len(relroot):
|
if len(relroot):
|
||||||
relroot += '/'
|
relroot += '/'
|
||||||
for file in files:
|
for file in files:
|
||||||
if file in __CONFIG.get('bot', 'ignored_files'):
|
if file in var.config.get('bot', 'ignored_files'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
fullpath = os.path.join(path, relroot, file)
|
fullpath = os.path.join(path, relroot, file)
|
||||||
@ -38,7 +34,7 @@ def get_recursive_filelist_sorted(path):
|
|||||||
# - hash is a sha1 of the string representation of the directories' contents (which are
|
# - hash is a sha1 of the string representation of the directories' contents (which are
|
||||||
# zipped)
|
# zipped)
|
||||||
def zipdir(zippath, zipname_prefix=None):
|
def zipdir(zippath, zipname_prefix=None):
|
||||||
zipname = __CONFIG.get('bot', 'tmp_folder')
|
zipname = var.config.get('bot', 'tmp_folder')
|
||||||
if zipname_prefix and '../' not in zipname_prefix:
|
if zipname_prefix and '../' not in zipname_prefix:
|
||||||
zipname += zipname_prefix.strip().replace('/', '_') + '_'
|
zipname += zipname_prefix.strip().replace('/', '_') + '_'
|
||||||
|
|
||||||
|
@ -3,3 +3,4 @@ playlist = []
|
|||||||
user = ""
|
user = ""
|
||||||
music_folder = ""
|
music_folder = ""
|
||||||
is_proxified = False
|
is_proxified = False
|
||||||
|
config = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user