Skip to content Skip to sidebar Skip to footer

Monitoring Icy Stream Metadata Title (python)

I would like to continually read the stream and update the title information. (Something like this is posted here: Receive ice cast meta data with python, however it makes multiple

Solution 1:

Not sure if you did already find a way in the meantime. But as I came across the same question - here is my (working but barely tested) version.
Widely based on your suggestion, with some adaptions and python3 support:

from __future__ import unicode_literals

import re
import requests
import sys

try:
    from StringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

def icy_monitor(stream_url, callback=None):

    r = requests.get(stream_url, headers={'Icy-MetaData': '1'}, stream=True)
    if r.encoding is None:
        r.encoding = 'utf-8'

    byte_counter = 0
    meta_counter = 0
    metadata_buffer = BytesIO()

    metadata_size = int(r.headers['icy-metaint']) + 255

    data_is_meta = False


    for byte in r.iter_content(1):

        byte_counter += 1

        if (byte_counter <= 2048):
            pass

        if (byte_counter > 2048):
            if (meta_counter == 0):
                meta_counter += 1

            elif (meta_counter <= int(metadata_size + 1)):

                metadata_buffer.write(byte)
                meta_counter += 1
            else:
                data_is_meta = True

        if (byte_counter > 2048 + metadata_size):
            byte_counter = 0

        if data_is_meta:

            metadata_buffer.seek(0)

            meta = metadata_buffer.read().rstrip(b'\0')

            m = re.search(br"StreamTitle='([^']*)';", bytes(meta))
            if m:
                title = m.group(1).decode(r.encoding, errors='replace')
                print('New title: {}'.format(title))

                if callback:
                    callback(title)

            byte_counter = 0
            meta_counter = 0
            metadata_buffer = BytesIO()

            data_is_meta = False


def print_title(title):
    print('Title: {}'.format(title))



if __name__ == '__main__':

    stream_url = sys.argv[1]
    icy_monitor(stream_url, callback=print_title)

Post a Comment for "Monitoring Icy Stream Metadata Title (python)"