2011-04-02 15:47:51 +02:00
|
|
|
import clementine
|
|
|
|
|
2011-05-22 14:27:16 +02:00
|
|
|
from PythonQt.QtCore import QUrl
|
|
|
|
from PythonQt.QtNetwork import QNetworkRequest
|
2011-04-02 15:47:51 +02:00
|
|
|
|
2011-05-22 14:27:16 +02:00
|
|
|
import json
|
2011-05-28 18:00:53 +02:00
|
|
|
import logging
|
2011-04-02 15:47:51 +02:00
|
|
|
import urllib
|
|
|
|
|
2011-05-28 18:00:53 +02:00
|
|
|
LOGGER = logging.getLogger("google_images")
|
2011-05-22 14:27:16 +02:00
|
|
|
|
2011-04-02 15:47:51 +02:00
|
|
|
|
2011-05-22 14:27:16 +02:00
|
|
|
class GoogleImagesCoverProvider(clementine.CoverProvider):
|
2011-05-28 18:00:53 +02:00
|
|
|
API_URL = 'https://ajax.googleapis.com/ajax/services/search/images?{0}'
|
2011-05-22 14:27:16 +02:00
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
def __init__(self, parent=None):
|
2011-05-22 14:27:16 +02:00
|
|
|
clementine.CoverProvider.__init__(self, "Google Images", parent)
|
2011-04-02 15:47:51 +02:00
|
|
|
|
|
|
|
self.api_args = {
|
2011-04-06 17:24:56 +02:00
|
|
|
'v' : '1.0',
|
2011-04-02 15:47:51 +02:00
|
|
|
# at most five results
|
2011-04-06 17:24:56 +02:00
|
|
|
'rsz' : '5',
|
|
|
|
# only larger sizes
|
|
|
|
'imgsz' : 'large|xlarge'
|
2011-04-02 15:47:51 +02:00
|
|
|
}
|
2011-06-20 01:15:51 +02:00
|
|
|
self.network = clementine.NetworkAccessManager()
|
2011-04-02 15:47:51 +02:00
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
def StartSearch(self, artist, album, id):
|
|
|
|
url = self.GetQueryURL(artist + " " + album)
|
2011-06-20 01:15:51 +02:00
|
|
|
LOGGER.info("Id %d - sending request to '%s'" % (id, url))
|
2011-04-02 15:47:51 +02:00
|
|
|
|
|
|
|
reply = self.network.get(QNetworkRequest(url))
|
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
def QueryFinished():
|
|
|
|
LOGGER.debug("Id %d - finished" % id)
|
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
self.SearchFinished(id, self.ParseReply(artist, album, reply))
|
2011-06-20 01:15:51 +02:00
|
|
|
|
|
|
|
reply.connect("finished()", QueryFinished)
|
|
|
|
return True
|
2011-04-02 15:47:51 +02:00
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
def ParseReply(self, artist, album, reply):
|
2011-05-22 14:27:16 +02:00
|
|
|
results = json.loads(str(reply.readAll()))
|
2011-04-02 15:47:51 +02:00
|
|
|
|
|
|
|
parsed = []
|
|
|
|
|
2011-05-28 18:00:53 +02:00
|
|
|
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
|
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
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-05-22 14:27:16 +02:00
|
|
|
|
2011-04-02 15:47:51 +02:00
|
|
|
current.description = query
|
2011-05-22 14:27:16 +02:00
|
|
|
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
|
|
|
|
|
2011-06-22 21:11:58 +02:00
|
|
|
return QUrl.fromEncoded(self.API_URL.format(urllib.urlencode(current_args)))
|
2011-04-02 15:47:51 +02:00
|
|
|
|
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
provider = GoogleImagesCoverProvider()
|
|
|
|
clementine.cover_providers.AddProvider(provider)
|