summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2011-03-27 19:55:50 +0200
committerAnton Khirnov <anton@khirnov.net>2011-04-05 14:19:15 +0200
commit9d30595e703e95cd0ecebe650576471a1cb1b0ab (patch)
tree2a5a2ee08e4a4eff5f4dcb6ebe9eded72fa24789
parent82fb2128be98f04ab403f192d5a42c7e2075d8f5 (diff)
common: split MetadataFetcher into its own filemaster
also fix finished() signal signature
-rw-r--r--nephilim/common.py61
-rw-r--r--nephilim/metadata_fetcher.py79
-rw-r--r--nephilim/plugins/AlbumCover.py7
-rw-r--r--nephilim/plugins/Lyrics.py6
4 files changed, 87 insertions, 66 deletions
diff --git a/nephilim/common.py b/nephilim/common.py
index 35e041f..ddbb96b 100644
--- a/nephilim/common.py
+++ b/nephilim/common.py
@@ -16,8 +16,7 @@
# along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
#
-from PyQt4 import QtCore, QtGui, QtNetwork
-from PyQt4.QtCore import pyqtSignal as Signal
+from PyQt4 import QtCore, QtGui
import socket
import logging
import os
@@ -106,64 +105,6 @@ def decode_htmlentities(string):
entity_re = re.compile(r'&(#?)(x?)(\w+);')
return entity_re.subn(substitute_entity, string)[0]
-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', 'metadata'])
-
- #### 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
-
class SongsMimeData(QtCore.QMimeData):
# private
__songs = None
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 <anton@khirnov.net>
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+
+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
diff --git a/nephilim/plugins/AlbumCover.py b/nephilim/plugins/AlbumCover.py
index 8a26bc8..6de4519 100644
--- a/nephilim/plugins/AlbumCover.py
+++ b/nephilim/plugins/AlbumCover.py
@@ -21,7 +21,7 @@ from PyQt4.QtCore import pyqtSignal as Signal
import os
from ..plugin import Plugin
-from .. import common
+from .. import common, metadata_fetcher, song
from .. import icons
class AlbumCoverWidget(QtGui.QLabel):
@@ -316,10 +316,11 @@ class AlbumCover(Plugin):
def get_settings_widget(self):
return self.SettingsWidgetAlbumCover(self)
-class FetcherLastfm(common.MetadataFetcher):
+class FetcherLastfm(metadata_fetcher.MetadataFetcher):
name = 'Last.fm'
def fetch(self, song):
+ self.song = song
if not 'artist' in song or not 'album' in song:
return self.finish()
url = QtCore.QUrl('http://ws.audioscrobbler.com/2.0/')
@@ -371,7 +372,7 @@ class FetcherLocal(QtCore.QObject):
settings = None
# SIGNALS
- finished = Signal('song', 'metadata')
+ finished = Signal([song.Song, object])
def __init__(self, plugin):
QtCore.QObject.__init__(self, plugin)
diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py
index 4413b8e..30e18fa 100644
--- a/nephilim/plugins/Lyrics.py
+++ b/nephilim/plugins/Lyrics.py
@@ -22,7 +22,7 @@ import re
from lxml import etree
from ..plugin import Plugin
-from .. import common
+from .. import common, metadata_fetcher
from .. import icons
class LyricsWidget(QtGui.QWidget):
@@ -300,7 +300,7 @@ class Lyrics(Plugin):
self.__fetchers.append(fetcher(self))
self.__fetchers[-1].finished.connect(self.__new_lyrics_fetched)
-class FetchLyricwiki(common.MetadataFetcher):
+class FetchLyricwiki(metadata_fetcher.MetadataFetcher):
name = 'Lyricwiki'
__apiaddress = 'http://lyrics.wikia.com/api.php'
@@ -364,7 +364,7 @@ class FetchLyricwiki(common.MetadataFetcher):
lyrics += gr + '\n'
self.finish(common.decode_htmlentities(lyrics))
-class FetchAnimelyrics(common.MetadataFetcher):
+class FetchAnimelyrics(metadata_fetcher.MetadataFetcher):
name = 'Animelyrics'
def fetch(self, song):