quentin.dufour.io/assets/code/icy.py

38 lines
995 B
Python
Raw Permalink Normal View History

2019-08-06 11:48:51 +00:00
import asyncio
async def readHeaders(reader):
status_code = await reader.readline()
2019-08-06 12:41:28 +00:00
assert status_code, b'HTTP/1.0 200 OK\r\n'
2019-08-06 11:48:51 +00:00
headers = {}
while True:
data = await reader.readline()
if data == b'\r\n':
print("End of metadata part, all key/value headers have been read")
return headers
header_name, header_value = data.split(b':', 1)
headers[header_name] = header_value
async def icy():
reader, writer = await asyncio.open_connection('streaming.radionti.com', 80)
writer.write(b"""GET /nti-320.mp3 HTTP/1.0
Host: streaming.radionti.com
User-Agent: Icy-Test
Accept: */*
Icy-Metadata: 1
""")
headers = await readHeaders(reader)
metaint = int(headers[b'icy-metaint'])
while True:
audio = await reader.readexactly(metaint)
metadata_size = int.from_bytes(await reader.readexactly(1), "big") * 16
metadata_content = await reader.readexactly(metadata_size)
2019-08-06 12:41:28 +00:00
if metadata_size > 0: print(metadata_content)
2019-08-06 11:48:51 +00:00
asyncio.run(icy())