Python: Get name of shoutcast/internet radio station from url

寵の児 提交于 2019-12-04 12:15:21

From a little curl, it seems to be using shoutcast protocol, so you're looking for an early line starting with icy-name:

$ curl http://89.238.146.142:7030 | head -5
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13191    0 13191    0     0  16013      0 --:--:-- --:--:-- --:--:-- 28516ICY 200 OK
icy-notice1:<BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR>
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-name:Ibiza Global Radio
icy-genre:Electronic
100 33463    0 33463    0     0  30954      0 --:--:--  0:00:01 --:--:-- 46579
curl: (23) Failed writing body
$ 

Therefore:

>>> import urllib2
>>> f = urllib2.urlopen('http://89.238.146.142:7030')
>>> for i, line in enumerate(f):
...   if line.startswith('icy-name') or i > 20: break
... 
>>> if i > 20: print 'failed to find station name'
... else: print 'station name is', line.replace('icy-name:', '')
... 
station name is Ibiza Global Radio

>>> 

You may want to add e.g. some .lower() calls because I believe these header names are meant to be case-insensitive, but that's the general idea.

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