diff --git a/Dockerfile b/Dockerfile index 63bf6d7..39666da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,9 @@ RUN apt-get -yq install ffmpeg build-essential RUN pip --no-cache-dir install \ discord.py[voice] \ - pynacl + pynacl \ + httplib2 \ + urllib3 WORKDIR /app diff --git a/main.py b/main.py index e197a5d..557a78a 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,21 @@ import os -import discord -from discord.ext import commands +import sys +import re +import struct import asyncio from subprocess import Popen +import discord +from discord.ext import commands +import urllib.request as urllib2 + bot_version = os.environ['version'] print('radiobot ' + bot_version + ' starting') login_token = os.environ['token'] voice_channel_id = os.environ['channel_voice'] text_channel_id = os.environ['channel_text'] +source = os.environ['url'] # Configure the bot description = '''Radiobot''' @@ -23,6 +29,8 @@ bot = commands.Bot(command_prefix='!', description=description) voice_client = None text_channel = None isConnected = False +encoding = 'latin1' + @bot.event async def on_ready(): @@ -35,7 +43,6 @@ async def on_ready(): print("[WARN] No voice channel " + voice_channel_id + " found!") if not debug_channel: print("[WARN] No text channel " + text_channel_id + " found!") - await debug_channel.send('] ready.') @bot.event @@ -49,9 +56,33 @@ async def on_message(message): print('<' + message.author.nick + '> ' + message.content) + if message.content == '!help': + await message.channel.send('] radiobot commands: help, version, song') + if message.content == '!version': await message.channel.send('] radiobot ' + bot_version + ' - python ' + os.environ['PYTHON_VERSION'] + ' - https://github.com/deflax/radiobot :purple_heart:') + if message.content == '!song': + request = urllib2.Request(source, headers={'Icy-MetaData': 1}) # request metadata + response = urllib2.urlopen(request) + metaint = int(response.headers['icy-metaint']) + for _ in range(10): # # title may be empty initially, try several times + response.read(metaint) # skip to metadata + metadata_length = struct.unpack('B', response.read(1))[0] * 16 # length byte + metadata = response.read(metadata_length).rstrip(b'\0') + m = re.search(br"StreamTitle='([^']*)';", metadata) + if m: + title = m.group(1) + if title: + break + else: + print('no title found') + return + meta = (title.decode(encoding, errors='replace')) + + metastar = meta.split("*", 1)[0] + await message.channel.send('] ' + meta) + @bot.event async def on_voice_state_update(member, before, after): """ @@ -79,11 +110,11 @@ async def on_voice_state_update(member, before, after): #await debug_channel.send('] voice #' + voice_channel_id + ' member count: ' + str(member_ids)) - if member_ids == 1 and isConnected == False: + if member_ids > 1 and isConnected == False: isConnected = True await debug_channel.send('] connecting to #' + voice_channel_id) voice_client = await voice_channel.connect() - player = voice_client.play(discord.FFmpegPCMAudio(os.environ['url'], **FFMPEG_OPTS)) + player = voice_client.play(discord.FFmpegPCMAudio(source, **FFMPEG_OPTS)) return if member_ids == 1 and isConnected == True: