mirror of
https://github.com/Matthww/TuneBot.git
synced 2026-09-21 20:47:44 +00:00
61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import re
|
|
import struct
|
|
import sys
|
|
import urllib.request as urllib2
|
|
import aiohttp
|
|
|
|
key = "b9803d262fab977f:04c791f69496359d638fc634e60c08d0"
|
|
|
|
|
|
def hasNumbers(inputString):
|
|
return any(char.isdigit() for char in inputString)
|
|
|
|
|
|
def process_stream_title(title: str) -> list:
|
|
l = title.split(' - ', 1)
|
|
if l[0].isdigit():
|
|
return l[1].rsplit(" - ", 1)
|
|
return title.rsplit(" - ", 1)
|
|
|
|
|
|
async def fetch_metadata():
|
|
url = "https://azuracast.exobot.site/api/nowplaying"
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(url) as response:
|
|
data = await response.json()
|
|
source = data["playing_next"]["text"]
|
|
name, vid = process_stream_title(source["title"])
|
|
return {"name": name, "thumbnail": data["playing_next"]["art"]}
|
|
|
|
|
|
async def get_metadata():
|
|
"""Deprecated: use fetch_metadata()"""
|
|
url = 'https://icecast.exobot.site/stream' # radio stream
|
|
encoding = 'iso-8859-1' # default: iso-8859-1 for mp3 and utf-8 for ogg streams
|
|
request = urllib2.Request(url, headers={'Icy-MetaData':
|
|
1}) # request metadata
|
|
response = urllib2.urlopen(request)
|
|
# print(response.headers, file=sys.stderr)
|
|
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')
|
|
# print(metadata, file=sys.stderr)
|
|
# extract title from the metadata
|
|
m = re.search(br"StreamTitle='([^']*)';", metadata)
|
|
if m:
|
|
title = m.group(1)
|
|
if title:
|
|
break
|
|
else:
|
|
sys.exit('no title found')
|
|
|
|
title = title.decode(encoding, errors='replace')
|
|
#print(f"DEBUG: {title}")
|
|
if hasNumbers(str(title[:4])):
|
|
return title[7:][:-14]
|
|
else:
|
|
return title[:-14]
|