blog.sina.com.cn/s/blog_5a3640220100b0se.html - Translator
Using PyQt first procedure-bupt IPTV client
[此博文包含图片] (2008-11-11 14:59:14)
Reprint▼
Tags:
qt
iptv
it
Category: Python
To practice how to write a Gui program that is relatively simple, is to link hold down on your Web site, and then double-click called MPlayer for playback:
使用pyqt的第一个程序--北邮人IPTV客户端
(1) get the link code (uses urllib and sgmllib library):
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import sys
from sgmllib import SGMLParser
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.pieces=[]
self.urls=[]
self.data=[]
self.flag = 0
def start_a(self,attrs):
href=[v for k,v in attrs if k=="href"]
if "mms" in str(href):
self.pieces.extend(href)
self.flag = 1
def end_a(self):
self.flag = 0
def handle_data(self,text):
if(self.flag == 1):
self.pieces.extend([text.decode('GB2312').encode('UTF-8')]) # (sys.stdin.encoding)])
def getplaylist():
playlist = []
usock=urllib.urlopen("http://iptv.bupt.edu.cn/")
parser=URLLister()
parser.feed(usock.read())
usock.close()
parser.close()
length = len(parser.pieces)/2
for i in range(length) :
playlist.extend(["%s %s" % (parser.pieces[i*2+1],parser.pieces[i*2])])
return playlist
(2) Gui part of the code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from iptv import Ui_iptv
import playlist
import string
class StartQt4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_iptv()
self.ui.setupUi(self)
self.codec = QtCore.QTextCodec.codecForName('UTF-8')
self.tvlist = playlist.getplaylist()
self.ui.listTable.clear()
for url in self.tvlist:
item = QtGui.QListWidgetItem(self.ui.listTable)
item.setText(QtGui.QApplication.translate("iptv", url, None, QtGui.QApplication.UnicodeUTF8))
QtCore.QObject.connect(self.ui.listTable,QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"),
self.play)
QtCore.QMetaObject.connectSlotsByName(self)
def play(self):
import subprocess
signalList = self.sender()
Itemcount = signalList.currentRow()
Itemtext = self.tvlist[Itemcount]
url = string.split(Itemtext)[1]
subprocess. Popen(["smplayer",url])
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())