urlib和aiohttp之间的结果不同



所以基本上我试图从在线广播直接链接中获得当前播放的曲目(示例-http://air.radiorecord.ru:8101/rr_320)。

首先,我在互联网上发现了一些用urlib编写的东西,我的应用程序是异步的,所以我需要使用aiohttp。有了urlib,它工作得很好,而aiohttp有时什么都找不到。请帮忙:(

之前:

def get_now(self, session):
request = urllib.Request(self.data.get('url'),headers={'Icy-MetaData': 1} ) # request metadata
response = urllib.urlopen(request)
metadata = response.headers
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'')
# extract title from the metadata
m = re.search(br"StreamTitle='([^']*)';", metadata)
if m:
title = m.group(1)
if title:
break
else:
return "No title found"
return title.decode('utf8', errors='replace')  
except:
return "No title found"

之后:

async def get_now(self, session):

async with session.get(self.stream_url, headers={'Icy-MetaData': "1"}) as resp:

content = resp.content
metadata = resp.headers
metaint = int(metadata['icy-metaint'])
for _ in range(30):
await content.read(metaint)
metadata_length = struct.unpack('B', await content.read(1))[0] * 16  # length byte
metadata = (await content.read(metadata_length)).rstrip(b'')
m = re.search(br"StreamTitle='([^']*)';", metadata)
if m:
title = m.group(1)
if title:
return title.decode('utf8', errors='replace')
else:
return "No title found"

return "Nothing found"

下面的片段总是能够检测到当前轨道(大约在400ms中(,但它不是只处理部分块,而是在读取时检查整个块:

import aiohttp
import asyncio
import re

async def get_now(stream_url, session):
headers={"Icy-MetaData": "1"}
async with session.get(stream_url, headers=headers) as resp:
for _ in range(10):
data = await resp.content.read(8192)
m = re.search(br"StreamTitle='([^']*)';", data.rstrip(b""))
if m:
title = m.group(1)
if title:
return title.decode("utf8", errors="replace")
else:
return "No title found"
return "Nothing found"

async def get_track():
session = aiohttp.ClientSession()
stream_url = "http://air.radiorecord.ru:8101/rr_320"
result = await get_now(stream_url, session)
print(f"result: {result}")
await session.close()

asyncio.run(get_track())

在我的计算机上的结果(在相当旧的CPU上CPU使用率非常低:i7-3517U(:

[ionut@ionut-pc ~]$ time python test.py 
result: Record Club - Nejtrino & Baur
real    0m0.401s
user    0m0.198s
sys 0m0.031s

相关内容

  • 没有找到相关文章

最新更新