From 9d30595e703e95cd0ecebe650576471a1cb1b0ab Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 27 Mar 2011 19:55:50 +0200 Subject: common: split MetadataFetcher into its own file also fix finished() signal signature --- nephilim/metadata_fetcher.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 nephilim/metadata_fetcher.py (limited to 'nephilim/metadata_fetcher.py') diff --git a/nephilim/metadata_fetcher.py b/nephilim/metadata_fetcher.py new file mode 100644 index 0000000..9e18aac --- /dev/null +++ b/nephilim/metadata_fetcher.py @@ -0,0 +1,79 @@ +# +# Copyright (C) 2010 Anton Khirnov +# +# Nephilim is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Nephilim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Nephilim. If not, see . +# + +from PyQt4 import QtCore, QtNetwork +from PyQt4.QtCore import pyqtSignal as Signal + +from song import Song + +class MetadataFetcher(QtCore.QObject): + """A basic class for metadata fetchers. Provides a fetch(song) function, + emits a finished(song, metadata) signal when done; lyrics is either a Python + unicode string or None if not found.""" + #public, read-only + logger = None + name = '' + + #private + nam = None # NetworkAccessManager + rep = None # current NetworkReply. + song = None # current song + + # SIGNALS + finished = Signal([Song, object]) + + #### private #### + def __init__(self, plugin): + QtCore.QObject.__init__(self, plugin) + + self.nam = QtNetwork.QNetworkAccessManager() + self.logger = plugin.logger + + def fetch2(self, song, url): + """A private convenience function to initiate fetch process.""" + # abort any existing connections + self.abort() + self.song = song + + self.logger.info('Searching %s: %s.'%(self. name, url.toString())) + self.rep = self.nam.get(QtNetwork.QNetworkRequest(url)) + self.rep.error.connect(self.handle_error) + + def finish(self, metadata = None): + """A private convenience function to clean up and emit finished(). + Feel free to reimplement/not use it.""" + self.rep = None + self.finished.emit(self.song, metadata) + self.song = None + + def handle_error(self): + """Print the error and abort.""" + self.logger.error(self.rep.errorString()) + self.abort() + self.finish() + + #### public #### + def fetch(self, song): + """Reimplement this in subclasses.""" + pass + + def abort(self): + """Abort all downloads currently in progress.""" + if self.rep: + self.rep.blockSignals(True) + self.rep.abort() + self.rep = None -- cgit v1.2.3