Receive ice cast meta data with python

自作多情 提交于 2019-12-23 04:13:07

问题


I tried to get the ice cast meta data of a mp3 stream with this script:

import requests

url = 'http://stream.jam.fm/jamfm-nmr/mp3-128/konsole/'

try:
    response = requests.get(url, headers={'Icy-MetaData': 1}, stream=True)
    response.raise_for_status()
except requests.RequestException, e:
    print 'Error:', e
else:
    headers, stream = response.headers, response.raw
    meta_int = headers.get('icy-metaint')

    if meta_int is not None:
        audio_length = int(meta_int)

        while True:
            try:
                audio_data = stream.read(audio_length)
                meta_byte = stream.read(1)
                if (meta_byte):
                    meta_length = ord(meta_byte) * 16
                    meta_data = stream.read(meta_length)
                    print meta_data
            except KeyboardInterrupt:
                break

    response.close()

This works but just for the first package. I will never receive an update on the title information when the track changes. My question is: Is this intended behavior and the track info is just send once or did I something wrong? I would like to be able to notice a track change without polling the stream from time to time.


回答1:


while True:

    try:
        #new request
        response = requests.get(url, headers={'Icy-MetaData': 1}, stream=True)
        response.raise_for_status()
        headers, stream = response.headers, response.raw
        meta_int = headers.get('icy-metaint')

        audio_data = stream.read(audio_length)
        meta_byte = stream.read(1)
        if (meta_byte):
            meta_length = ord(meta_byte) * 16
            meta_data = stream.read(meta_length)
            print (meta_data)
    except KeyboardInterrupt:
        break


来源:https://stackoverflow.com/questions/28611373/receive-ice-cast-meta-data-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!