From c31390859f17de65ff9a083eed5c6540da9164b9 Mon Sep 17 00:00:00 2001 From: mtuszowski Date: Fri, 9 Aug 2024 16:43:52 +0200 Subject: [PATCH] Metadata --- bot.py | 2 +- communication_subroutine.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index bc9474d..afd87be 100644 --- a/bot.py +++ b/bot.py @@ -2543,7 +2543,7 @@ async def co_na_plejliscie_wariacie(ctx): async with ctx.typing(): logger.info("plejlista") logger.info(PREPPED_TRACKS) - await ctx.send(f"Didżej Hammer i jego sztuczna inteligencja (||he he||) prezentują: \n- obecnie grany jest {(PREPPED_TRACKS['now_playing']).rstrip()} \n- następny będzie{(PREPPED_TRACKS['next']).rstrip()} \n- na liście priorytetowej {(PREPPED_TRACKS['priority']).rstrip()} \n- na liście hitów obecnie czeka kawałek {(PREPPED_TRACKS['hit']).rstrip()} \n- na liście wszystkich kawałków {(PREPPED_TRACKS['all']).rstrip()} \n- najświeższym zamówieniem od słuchaczy jest {(PREPPED_TRACKS['requests']).rstrip()} \nZ kolei jingiel to: {(PREPPED_TRACKS['jingles']).rstrip()}") + await ctx.send(f"Didżej Hammer i jego sztuczna inteligencja (||he he||) prezentują: \n- obecnie grany jest {(PREPPED_TRACKS['now_playing']).rstrip()} (wg metadanych: {PREPPED_TRACKS['meta']}) \n- następny będzie{(PREPPED_TRACKS['next']).rstrip()} \n- na liście priorytetowej {(PREPPED_TRACKS['priority']).rstrip()} \n- na liście hitów obecnie czeka kawałek {(PREPPED_TRACKS['hit']).rstrip()} \n- na liście wszystkich kawałków {(PREPPED_TRACKS['all']).rstrip()} \n- najświeższym zamówieniem od słuchaczy jest {(PREPPED_TRACKS['requests']).rstrip()} \nZ kolei jingiel to: {(PREPPED_TRACKS['jingles']).rstrip()}") # *================================== Run if __name__ == "__main__": diff --git a/communication_subroutine.py b/communication_subroutine.py index d7f1488..716d483 100644 --- a/communication_subroutine.py +++ b/communication_subroutine.py @@ -2,14 +2,20 @@ import logging import threading import json import time +import re +from urllib import request as urequest + from queue import Queue, Empty from flask import Flask, jsonify, request from waitress import serve HOST_ADDRESS = "192.168.1.191" PORT_ADDRESS = 5000 +ICECAST_ADDRESS = "192.168.1.12:666" OUT_COMM_Q = Queue() IN_COMM_Q = Queue() +SRCHTITLE = re.compile(br'StreamTitle=\\*(?P[^;]*);').search + awaiting_q = [] incoming_q = Queue() app = Flask(__name__) @@ -20,7 +26,8 @@ PREPPED_TRACKS = { "priority":"", "jingles":"", "now_playing":"", - "next": "" + "next": "", + "meta" : "" } logger = logging.getLogger("discord") @@ -49,7 +56,9 @@ def log_radio_tracks(): record = json.loads(request.data) logger.info(record) if "next" in record[0]: + metadata = id3(ICECAST_ADDRESS) PREPPED_TRACKS["now_playing"] = PREPPED_TRACKS["next"] + PREPPED_TRACKS["meta"] = metadata["name"] + " - " + metadata["title"] + "(" + metadata["genre"] + ")" PREPPED_TRACKS[record[0]] = record[1] logger.info("DATA RECEIVED") return jsonify("SUCCESS") @@ -150,6 +159,28 @@ def scan_incoming(): except Empty: time.sleep(1) +def get_stream_title(tag:bytes) -> str: + title = '' + if m := SRCHTITLE(tag): + #decode, strip, unescape and remove surrounding quotes (may not even be the same type of quote) + title = m.group('title').decode('utf-8').strip().replace('\\', '')[1:-1] + return title + +def id3(url:str) -> dict: + request = urequest.Request(url, headers={'Icy-MetaData': 1}) + + with urequest.urlopen(request) as resp: + metaint = int(resp.headers.get('icy-metaint', '-1')) + if metaint<0: + return False + resp.read(metaint) #this isn't seekable so, arbitrarily read to the point we want + tagdata = dict( + site_url = resp.headers.get('icy-url' ) , + name = resp.headers.get('icy-name' ).title(), + genre = resp.headers.get('icy-genre').title(), + title = get_stream_title(resp.read(255)) ) + return tagdata + def comm_subroutine(): """