From 7057252420c89c3d1ff08efc66605f9c996b1a27 Mon Sep 17 00:00:00 2001 From: azlux Date: Thu, 31 May 2018 22:39:42 +0200 Subject: [PATCH] Lot of new features Web interface almost ready, better bot --- configuration.ini | 18 +++++++++---- interface.py | 21 ++++++++------- media.py | 19 ++++++++++--- mumbleBot.py | 63 ++++++++++++++++++++++++++++---------------- templates/index.html | 20 +++++++++++--- 5 files changed, 98 insertions(+), 43 deletions(-) diff --git a/configuration.ini b/configuration.ini index 894a72f..94a58c6 100644 --- a/configuration.ini +++ b/configuration.ini @@ -4,11 +4,8 @@ volume = 0.1 admin = Azlux;AzMobile music_folder = /home/dmichel/botamusique/music/ tmp_folder = /tmp/ -is_proxified = True - -[debug] -ffmpeg = False -mumbleConnection = False +web_interace = False +is_web_proxified = True [command] play_file = file @@ -25,6 +22,12 @@ kill = kill stop_and_getout = oust joinme = joinme +[radio] +ponyville = http://192.99.131.205:8000/stream.mp3 +luna = http://radio.ponyvillelive.com:8002/stream +radiobrony = http://62.210.138.34:8000/live +celestiaradio = http://celestia.aiverse.org:8000/mp3_256 +jazz = http://jazz-wr04.ice.infomaniak.ch/jazz-wr04-128.mp3 [strings] current_volume = volume : %d%% @@ -35,6 +38,7 @@ not_playing = Aucun stream en lecture bad_file = Bad file asked no_file = Not file here bad_url = Bad URL asked +empty_playlist = No more music into the playlist help = Command available:
!play_file @@ -45,3 +49,7 @@ help = Command available:
!play_file - stop + Go to default channel
!v - get or change the volume (in %)
!joinme + +[debug] +ffmpeg = False +mumbleConnection = False \ No newline at end of file diff --git a/interface.py b/interface.py index 8307595..05f1af0 100644 --- a/interface.py +++ b/interface.py @@ -64,27 +64,30 @@ def index(): files[director] = [f for f in listdir(folder_path + director) if os.path.isfile(os.path.join(folder_path + director, f))] if request.method == 'POST': - if 'add_file' in request.form and ".." not in request.form['add_music']: - var.playlist.append((request.form['type'], request.form['add_music'])) + if 'add_music' in request.form and ".." not in request.form['add_music']: + var.playlist.append(['file', request.form['add_music']]) + + if 'add_url' in request.form : + var.playlist.append(['url', request.form['add_url']]) + + if 'add_radio' in request.form: + var.playlist.append(['radio', request.form['add_radio']]) + if 'add_folder' in request.form and ".." not in request.form['add_folder']: - dir_files = [("file", request.form['add_folder'] + '/' + i) for i in files[request.form['add_folder']]] + dir_files = [["file", request.form['add_folder'] + '/' + i] for i in files[request.form['add_folder']]] var.playlist.extend(dir_files) elif 'delete_music' in request.form: try: - var.playlist.remove("file", request.form['delete_music']) + var.playlist.remove(["file", request.form['delete_music']]) except ValueError: pass elif 'action' in request.form: action = request.form['action'] if action == "randomize": random.shuffle(var.playlist) - if var.current_music: - current_music = var.current_music[len(var.music_folder):] - else: - current_music = None return render_template('index.html', - current_music=current_music, + current_music=var.current_music, user=var.user, playlist=var.playlist, all_files=files) diff --git a/media.py b/media.py index 64af452..8fbae1d 100644 --- a/media.py +++ b/media.py @@ -18,7 +18,7 @@ def get_radio_server_description(url): response = urllib.request.urlopen(request) data = json.loads(response.read().decode("utf-8")) title_server = data['servertitle'] - logging.debug("TITLE FOUND SHOUTCAST: " + title_server) + logging.info("TITLE FOUND SHOUTCAST: " + title_server) except urllib.error.HTTPError: pass except http.client.BadStatusLine: @@ -30,9 +30,9 @@ def get_radio_server_description(url): try: request = urllib.request.Request(url_icecast) response = urllib.request.urlopen(request) - data = json.loads(response.read().decode('utf-8',errors='ignore'),strict=False) + data = json.loads(response.read().decode('utf-8', errors='ignore'), strict=False) title_server = data['icestats']['source'][0]['server_name'] + ' - ' + data['icestats']['source'][0]['server_description'] - logging.debug("TITLE FOUND ICECAST: " + title_server) + logging.info("TITLE FOUND ICECAST: " + title_server) if not title_server: title_server = url except urllib.error.URLError: @@ -55,7 +55,7 @@ def get_radio_title(url): metadata_length = struct.unpack('B', response.read(1))[0] * 16 # length byte metadata = response.read(metadata_length).rstrip(b'\0') - logging.debug(metadata) + logging.info(metadata) # extract title from the metadata m = re.search(br"StreamTitle='([^']*)';", metadata) if m: @@ -65,3 +65,14 @@ def get_radio_title(url): except (urllib.error.URLError, urllib.error.HTTPError): pass return 'Impossible to get the music title' + + +def get_url(string): + if string.startswith('http'): + return string + p = re.compile('href="(.+)"', re.IGNORECASE) + res = re.search(p, string) + if res: + return res.group(1) + else: + return False diff --git a/mumbleBot.py b/mumbleBot.py index 66f7637..420ec01 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -18,6 +18,7 @@ import variables as var import hashlib import youtube_dl import media +import logging class MumbleBot: @@ -33,6 +34,7 @@ class MumbleBot: parser.add_argument("-P", "--password", dest="password", type=str, default="", help="Password if server requires one") parser.add_argument("-p", "--port", dest="port", type=int, default=64738, help="Port for the mumble server") parser.add_argument("-c", "--channel", dest="channel", type=str, default="", help="Default chanel for the bot") + parser.add_argument("-q", "--quiet", dest="quiet", action="store_true", help="Only Error logs") args = parser.parse_args() self.volume = self.config.getfloat('bot', 'volume') @@ -40,6 +42,12 @@ class MumbleBot: self.channel = args.channel var.current_music = None + FORMAT = '%(asctime)s: %(message)s' + if args.quiet: + logging.basicConfig(format=FORMAT, level=logging.ERROR, datefmt='%Y-%m-%d %H:%M:%S') + else: + logging.basicConfig(format=FORMAT, level=logging.DEBUG, datefmt='%Y-%m-%d %H:%M:%S') + ###### ## Format of the Playlist : ## [("","")] @@ -59,16 +67,17 @@ class MumbleBot: var.user = args.user var.music_folder = self.config.get('bot', 'music_folder') - var.is_proxified = self.config.getboolean("bot", "is_proxified") + var.is_proxified = self.config.getboolean("bot", "is_web_proxified") + self.exit = False self.nb_exit = 0 self.thread = None - interface.init_proxy() - - # t = threading.Thread(target=start_web_interface) - # t.daemon = True - # t.start() + if args.web_interace: + interface.init_proxy() + t = threading.Thread(target=start_web_interface) + t.daemon = True + t.start() self.mumble = pymumble.Mumble(args.host, user=args.user, port=args.port, password=args.password, debug=self.config.getboolean('debug', 'mumbleConnection')) @@ -86,11 +95,11 @@ class MumbleBot: self.loop() def ctrl_caught(self, signal, frame): - print("\ndeconnection asked") + logging.info("\ndeconnection asked") self.exit = True self.stop() if self.nb_exit > 1: - print("Forced Quit") + logging.info("Forced Quit") sys.exit(0) self.nb_exit += 1 @@ -106,7 +115,7 @@ class MumbleBot: else: return - print(command + ' - ' + parameter + ' by ' + self.mumble.users[text.actor]['name']) + logging.info(command + ' - ' + parameter + ' by ' + self.mumble.users[text.actor]['name']) if command == self.config.get('command', 'play_file') and parameter: path = self.config.get('bot', 'music_folder') + parameter @@ -121,6 +130,8 @@ class MumbleBot: var.playlist.append(["url", parameter]) elif command == self.config.get('command', 'play_radio') and parameter: + if self.config.has_option('radio', parameter): + parameter = self.config.get('radio', parameter) var.playlist.append(["radio", parameter]) elif command == self.config.get('command', 'help'): @@ -162,9 +173,14 @@ class MumbleBot: self.mumble.users[text.actor].send_message(self.config.get('strings', 'not_playing')) elif command == self.config.get('command', 'next'): - var.current_music = var.playlist[0] - var.playlist.pop(0) - self.launch_next() + if var.playlist: + var.current_music = var.playlist[0] + var.playlist.pop(0) + self.launch_next() + else: + self.mumble.users[text.actor].send_message(self.config.get('strings', 'empty_playlist')) + self.stop() + else: self.mumble.users[text.actor].send_message(self.config.get('strings', 'bad_command')) @@ -180,9 +196,9 @@ class MumbleBot: path = "" title = "" if var.current_music[0] == "url": - regex = re.compile(" 0: time.sleep(0.01) diff --git a/templates/index.html b/templates/index.html index 2550d46..07f1513 100644 --- a/templates/index.html +++ b/templates/index.html @@ -20,10 +20,24 @@ +
+ Add Youtube/Soundcloud URL : +
+ + +
+
+
+ Add Radio URL : +
+ + +
+
Current Playing : {% if current_music %} - {{ current_music }} + {{ current_music[0] }} > {{ current_music[2] }} {% else %} No music {% endif %} @@ -33,8 +47,8 @@
    {% for m in playlist %} -
  • {{ m }} -
    +
  • {{ m[0] }} - {{ m[1] }} +
  • {% endfor %}