Kijk, zo zou het ook kunnen:
# plug-ins
self.plugins = {}
pluginsdir = QtCore.QDir('plugins/')
pluginsdir.setFilter(QtCore.QDir.Files)
for file in pluginsdir.entryList():
if file.endsWith('.py'): # ignore non-python files
file.chop(3) # remove .py extension
plugin = __import__('plugins.'+str(file), fromlist=[str(file)])
self.plugins[str(file)] = plugin
for id, plugin in self.plugins.iteritems():
if self.debug:
print 'Plug-in '+id+' loaded with website trigger '+plugin.website
def checksite(self):
pluginFound = False
for id, plugin in self.plugins.iteritems():
if plugin.website == self.website:
pluginFound = True
self.host = plugin.host
self.baseurl = plugin.baseurl
data = self.urlsource.split('\n')
if not data:
self.ui.output.setText(QtGui.QApplication.translate("Form", "Error: invalid url.", None, QtGui.QApplication.UnicodeUTF8))
else:
vidname = plugin.getVideoName(str(data))
if vidname != None:
self.videonaam = vidname
#print self.videonaam
else:
self.ready()
self.ui.output.setText(QtGui.QApplication.translate("Form", "Error: No video name found. Using 'Unknown'.", None, QtGui.QApplication.UnicodeUTF8))
self.videonaam = 'Unknown'
vidurl = plugin.getVideoUrl(str(data))
if vidurl != None:
self.videourl = vidurl
if (self.debug):
print "Video url found: "+self.videourl
self.checkBeforeDownload()
else:
self.ready()
self.ui.output.setText(QtGui.QApplication.translate("Form", "Error: Video url not found. Couldn't download the video.", None, QtGui.QApplication.UnicodeUTF8))
videourl = 'Not found'
break
if not pluginFound:
self.ready()
self.ui.output.setText(QtGui.QApplication.translate("Form", "The website \""+self.website+"\" is not supported (yet)." , None, QtGui.QApplication.UnicodeUTF8))
Sample plug-in:
##################################################
######### Plugin for youtube - by koenux #########
##################################################
import re
# plug info
website = 'youtube.com'
# host info
host = 'youtube.com'
baseurl = '/get_video.php?video_id='
# find videoname
def getVideoName(data):
vnpat = re.compile( "video_id=.*\s*&title=(.*)\s*'.*" )# This regexp does not seem to be right, it will return the title + the rest of the html page
vnfnd = vnpat.search( data )
if (vnfnd == None):
videoname = None
else:
videoname = vnfnd.group(1)
return videoname
# find video url
def getVideoUrl(data):
vupat = re.compile( "video_id=(.*)\s*&title=.*" )
vufnd = vupat.search( data )
if (vufnd == None):
videourl == None
else:
videourl = vufnd.group(1)
print videourl
return videourl
edit: om errors op te vangen zou ik een try/except structuur opzetten in ytr.py rond het laden van de plug-in en uitvoeren van functies en laden van variabelen van de plug-in..