2009年10月19日 星期一

[小程式]hipls

有時候無聊會想聽聽廣播,又懶得多開一個網頁, hinedo 似乎可以直接播放,但之前用 openSUSE 時編不起來,就乾脆自己寫一個,這程式會直接到 hiChannel 抓所有的廣播電台清單,讓使用者選擇想聽那幾個電台,然後再將這幾個電台網址存成一份 playlist ,只要使用支援 mms 協定的播放器播放就可以聽了,目前遇到的問題是用 mpayer 播放會顯示亂碼...

用 python 內建函式庫寫的,照理說 windows 也可以使用,不過我只在 linux 上試過,有問題歡迎回報 :)

Updated 11/05:修改顯示電台清單時,因為中文字元長度問題而無法對齊的狀況

Usage:
1. save as hipls.py
2. python hipls.py

Download:hipls.py

Source Code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author : lefthaha (at) gmail{dot}com
# License :http://creativecommons.org/licenses/by-nc-sa/2.5/tw
# Last Modify : 2009/11/05
#

import sys
import urllib2
import re
import string

def get_html(url):
# must add header to connect hichannel
# http://www.voidspace.org.uk/python/articles/urllib2.shtml
headers = {"User-Agent":" Mozilla/5.0 "}
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req)
return response.readlines()

def get_match(pattern, target):
result = []
for line in target:
result.append( pattern.findall(line) )
# strip empty result , able to handle multiple matches
result = filter(lambda x: x is not None, result)
result = [ x[i] for x in result for i in range( len(x) ) ]
return result

def show_all(rlist):
# Updated 2009/11/05
print "目前搜尋到的電台有:"
for num in range(0, len(rlist)-1, 2):
name1 = rlist[num].name.decode("UTF8")
mytab = len(name1.encode("BIG5")) - len(name1)
print "({0:02}) {1:{4}} ({2:02}) {3}".format(num+1,\
rlist[num].name, num+2, rlist[num+1].name, 30+mytab)
print "請輸入你喜歡的電台編號,並且以逗號區別 (ex : 5,4,3,2,1):"

def edit_list(rlist):
show_all(rlist)
inl = re.split(r",|," ,sys.stdin.readline() )
print "你所輸入的電台順序是:"
for num in range( len(inl) ):
inl[num] = string.atoi(inl[num])-1
print "({0}) {1}".format(num+1,rlist[inl[num]].name)
print "以上是否正確?(Y/n)"
if re.match(r"[y|Y]", sys.stdin.readline() ) is not None:
return inl
else:
print "請重新選擇電台順序"
return None

def create_list(all_list, to_save):
entries = len(to_save)
content = "[playlist]\nNumberOfEntries={0}\n\n".format(entries)
for num in range(entries):
content = content + \
"File{0}={1}\nTitle{0}={2}\nLength{0}=-1\n\n".format(num+1,\
all_list[ to_save[num]].get_mms(),all_list[ to_save[num]].name )
content = content + "Version=2"
plsf = open("MyHipls.pls",'w')
plsf.write(content)
plsf.close()

class Radio:
def __init__(self, rid_url):
# strip useless strings
rid_url = re.sub( r".*id=|<", "", rid_url)
rid_url = re.sub( r"\">", ",", rid_url)
# split and assign radio id and name
self.rid, self.name = re.split(",", rid_url)
self.player_url="http://hichannel.hinet.net/player/radio/\
index.jsp?radio_id=" + self.rid
def get_mms(self):
page = get_html(self.player_url)
#?data=mms://bcr.media.hinet.net/RA000007&id=RADIO:206&group="+g);
line = get_match(re.compile(r".*=mms:.*"), page)
line = re.sub(r".*mms", "mms", line[0])
self.mms_url = re.sub(r"&id.*", '', line)
return self.mms_url

def main():
list_url = "http://hichannel.hinet.net/radioRank.do?typeId=99"
list_page = get_html(list_url)
# example : <a href="/radio.do?id=177">ICRT</a>
rids = get_match( re.compile(r"<a.*\"/radio.+\d\">[^<]+?<"), list_page)
radio_list=[]
for line in rids:
# initialize class Radio
radio_list.append( Radio(line) )
list_to_save = None
while( list_to_save is None):
list_to_save = edit_list(radio_list)
create_list(radio_list, list_to_save)
print "已將所選擇的電台清單儲存至 MyHipls.pls !"

if __name__ == "__main__":
main()

沒有留言: