mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-05 14:28:40 +01:00
Use python logging in the google-covers script, handle Google API errors properly
This commit is contained in:
parent
9e6fea9d27
commit
93e119c202
@ -4,18 +4,19 @@ from PythonQt.QtCore import QUrl
|
|||||||
from PythonQt.QtNetwork import QNetworkRequest
|
from PythonQt.QtNetwork import QNetworkRequest
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger("google_images")
|
||||||
|
|
||||||
|
|
||||||
class GoogleImagesCoversScript():
|
class GoogleImagesCoversScript():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# create and register our factory
|
# create and register our factory
|
||||||
self.factory = GoogleImagesCoverProviderFactory()
|
self.factory = GoogleImagesCoverProviderFactory()
|
||||||
|
|
||||||
|
|
||||||
class GoogleImagesCoverProviderFactory(clementine.CoverProviderFactory):
|
class GoogleImagesCoverProviderFactory(clementine.CoverProviderFactory):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
clementine.CoverProviderFactory.__init__(self)
|
clementine.CoverProviderFactory.__init__(self)
|
||||||
# register in the repository of factories
|
# register in the repository of factories
|
||||||
@ -26,11 +27,11 @@ class GoogleImagesCoverProviderFactory(clementine.CoverProviderFactory):
|
|||||||
|
|
||||||
|
|
||||||
class GoogleImagesCoverProvider(clementine.CoverProvider):
|
class GoogleImagesCoverProvider(clementine.CoverProvider):
|
||||||
|
API_URL = 'https://ajax.googleapis.com/ajax/services/search/images?{0}'
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
clementine.CoverProvider.__init__(self, "Google Images", parent)
|
clementine.CoverProvider.__init__(self, "Google Images", parent)
|
||||||
|
|
||||||
self.api_url = 'https://ajax.googleapis.com/ajax/services/search/images?{0}'
|
|
||||||
self.api_args = {
|
self.api_args = {
|
||||||
'v' : '1.0',
|
'v' : '1.0',
|
||||||
# at most five results
|
# at most five results
|
||||||
@ -38,13 +39,12 @@ class GoogleImagesCoverProvider(clementine.CoverProvider):
|
|||||||
# only larger sizes
|
# only larger sizes
|
||||||
'imgsz' : 'large|xlarge'
|
'imgsz' : 'large|xlarge'
|
||||||
}
|
}
|
||||||
self.network = clementine.NetworkAccessManager(self)
|
self.network = clementine.NetworkAccessManager(self)
|
||||||
self.queries = {}
|
self.queries = {}
|
||||||
|
|
||||||
def SendRequest(self, query):
|
def SendRequest(self, query):
|
||||||
print 'sending request'
|
|
||||||
|
|
||||||
url = self.GetQueryURL(query)
|
url = self.GetQueryURL(query)
|
||||||
|
LOGGER.info("Sending request to '%s'", url)
|
||||||
|
|
||||||
reply = self.network.get(QNetworkRequest(url))
|
reply = self.network.get(QNetworkRequest(url))
|
||||||
self.queries[reply] = query
|
self.queries[reply] = query
|
||||||
@ -52,16 +52,16 @@ class GoogleImagesCoverProvider(clementine.CoverProvider):
|
|||||||
return reply
|
return reply
|
||||||
|
|
||||||
def ParseReply(self, reply):
|
def ParseReply(self, reply):
|
||||||
print 'parsing reply'
|
|
||||||
|
|
||||||
results = json.loads(str(reply.readAll()))
|
results = json.loads(str(reply.readAll()))
|
||||||
|
|
||||||
parsed = []
|
parsed = []
|
||||||
|
|
||||||
if 'Error' in results:
|
if "responseStatus" not in results or results["responseStatus"] != 200:
|
||||||
|
LOGGER.warning("Error parsing reply: %s", results["responseDetails"])
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
query = self.queries.pop(reply)
|
query = self.queries[reply]
|
||||||
|
LOGGER.info("Parsing reply for query '%s'", query)
|
||||||
for result in results['responseData']['results']:
|
for result in results['responseData']['results']:
|
||||||
current = clementine.CoverSearchResult()
|
current = clementine.CoverSearchResult()
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ class GoogleImagesCoverProvider(clementine.CoverProvider):
|
|||||||
current_args = self.api_args.copy()
|
current_args = self.api_args.copy()
|
||||||
current_args['q'] = query
|
current_args['q'] = query
|
||||||
|
|
||||||
return QUrl(self.api_url.format(urllib.urlencode(current_args)))
|
return QUrl(self.API_URL.format(urllib.urlencode(current_args)))
|
||||||
|
|
||||||
|
|
||||||
script = GoogleImagesCoversScript()
|
script = GoogleImagesCoversScript()
|
||||||
|
Loading…
Reference in New Issue
Block a user