show stream title

This commit is contained in:
deflax 2022-03-28 10:52:08 -04:00
parent c8cb8746b3
commit 9438a24131
2 changed files with 39 additions and 6 deletions

View file

@ -5,7 +5,9 @@ RUN apt-get -yq install ffmpeg build-essential
RUN pip --no-cache-dir install \ RUN pip --no-cache-dir install \
discord.py[voice] \ discord.py[voice] \
pynacl pynacl \
httplib2 \
urllib3
WORKDIR /app WORKDIR /app

41
main.py
View file

@ -1,15 +1,21 @@
import os import os
import discord import sys
from discord.ext import commands import re
import struct
import asyncio import asyncio
from subprocess import Popen from subprocess import Popen
import discord
from discord.ext import commands
import urllib.request as urllib2
bot_version = os.environ['version'] bot_version = os.environ['version']
print('radiobot ' + bot_version + ' starting') print('radiobot ' + bot_version + ' starting')
login_token = os.environ['token'] login_token = os.environ['token']
voice_channel_id = os.environ['channel_voice'] voice_channel_id = os.environ['channel_voice']
text_channel_id = os.environ['channel_text'] text_channel_id = os.environ['channel_text']
source = os.environ['url']
# Configure the bot # Configure the bot
description = '''Radiobot''' description = '''Radiobot'''
@ -23,6 +29,8 @@ bot = commands.Bot(command_prefix='!', description=description)
voice_client = None voice_client = None
text_channel = None text_channel = None
isConnected = False isConnected = False
encoding = 'latin1'
@bot.event @bot.event
async def on_ready(): async def on_ready():
@ -35,7 +43,6 @@ async def on_ready():
print("[WARN] No voice channel " + voice_channel_id + " found!") print("[WARN] No voice channel " + voice_channel_id + " found!")
if not debug_channel: if not debug_channel:
print("[WARN] No text channel " + text_channel_id + " found!") print("[WARN] No text channel " + text_channel_id + " found!")
await debug_channel.send('] ready.') await debug_channel.send('] ready.')
@bot.event @bot.event
@ -49,9 +56,33 @@ async def on_message(message):
print('<' + message.author.nick + '> ' + message.content) print('<' + message.author.nick + '> ' + message.content)
if message.content == '!help':
await message.channel.send('] radiobot commands: help, version, song')
if message.content == '!version': if message.content == '!version':
await message.channel.send('] radiobot ' + bot_version + ' - python ' + os.environ['PYTHON_VERSION'] + ' - https://github.com/deflax/radiobot :purple_heart:') 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 @bot.event
async def on_voice_state_update(member, before, after): 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)) #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 isConnected = True
await debug_channel.send('] connecting to #' + voice_channel_id) await debug_channel.send('] connecting to #' + voice_channel_id)
voice_client = await voice_channel.connect() 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 return
if member_ids == 1 and isConnected == True: if member_ids == 1 and isConnected == True: