Clementine-audio-player-Mac.../scripts/google-covers/google_covers.py

73 lines
1.9 KiB
Python
Raw Normal View History

2011-04-02 15:47:51 +02:00
import clementine
from PythonQt.QtCore import QUrl
from PythonQt.QtNetwork import QNetworkRequest
2011-04-02 15:47:51 +02:00
import json
import logging
2011-04-02 15:47:51 +02:00
import urllib
LOGGER = logging.getLogger("google_images")
2011-04-02 15:47:51 +02:00
class GoogleImagesCoverProvider(clementine.CoverProvider):
API_URL = 'https://ajax.googleapis.com/ajax/services/search/images?{0}'
def __init__(self, parent=None):
clementine.CoverProvider.__init__(self, "Google Images", parent)
2011-04-02 15:47:51 +02:00
self.api_args = {
'v' : '1.0',
2011-04-02 15:47:51 +02:00
# at most five results
'rsz' : '5',
# only larger sizes
'imgsz' : 'large|xlarge'
2011-04-02 15:47:51 +02:00
}
self.network = clementine.NetworkAccessManager()
2011-04-02 15:47:51 +02:00
def StartSearch(self, artist, album, id):
url = self.GetQueryURL(artist + " " + album)
LOGGER.info("Id %d - sending request to '%s'" % (id, url))
2011-04-02 15:47:51 +02:00
reply = self.network.get(QNetworkRequest(url))
def QueryFinished():
LOGGER.debug("Id %d - finished" % id)
self.SearchFinished(id, self.ParseReply(artist, album, reply))
reply.connect("finished()", QueryFinished)
return True
2011-04-02 15:47:51 +02:00
def ParseReply(self, artist, album, reply):
results = json.loads(str(reply.readAll()))
2011-04-02 15:47:51 +02:00
parsed = []
if "responseStatus" not in results or results["responseStatus"] != 200:
LOGGER.warning("Error parsing reply: %s", results["responseDetails"])
2011-04-02 15:47:51 +02:00
return parsed
query = "%s - %s" % (artist, album)
LOGGER.info("Parsing reply for query '%s'" % query)
2011-04-02 15:47:51 +02:00
for result in results['responseData']['results']:
current = clementine.CoverSearchResult()
2011-04-02 15:47:51 +02:00
current.description = query
current.image_url = result['url']
2011-04-02 15:47:51 +02:00
parsed.append(current)
return parsed
def GetQueryURL(self, query):
current_args = self.api_args.copy()
current_args['q'] = query
return QUrl.fromEncoded(self.API_URL.format(urllib.urlencode(current_args)))
2011-04-02 15:47:51 +02:00
provider = GoogleImagesCoverProvider()
clementine.cover_providers.AddProvider(provider)