Fix for goodreads blocking "requests"

This commit is contained in:
Ozzie Isaacs
2024-05-10 09:42:44 +02:00
parent 506f0a33cf
commit 25a875b628
5 changed files with 40 additions and 25 deletions
+39 -8
View File
@@ -18,11 +18,11 @@
import time
from functools import reduce
import requests
import xmltodict
try:
from goodreads.client import GoodreadsClient
except ImportError:
from betterreads.client import GoodreadsClient
from goodreads.client import GoodreadsClient
from goodreads.request import GoodreadsRequest
try: import Levenshtein
except ImportError: Levenshtein = False
@@ -30,6 +30,37 @@ except ImportError: Levenshtein = False
from .. import logger
class my_GoodreadsClient(GoodreadsClient):
def request(self, *args, **kwargs):
"""Create a GoodreadsRequest object and make that request"""
req = my_GoodreadsRequest(self, *args, **kwargs)
return req.request()
class GoodreadsRequestException(Exception):
def __init__(self, error_msg, url):
self.error_msg = error_msg
self.url = url
def __str__(self):
return self.url, ':', self.error_msg
class my_GoodreadsRequest(GoodreadsRequest):
def request(self):
resp = requests.get(self.host+self.path, params=self.params,
headers={"User-agent":"Mozilla/5.0 (X11; Linux x86_64; rv:125.0) "
"Gecko/20100101 Firefox/125.0"})
if resp.status_code != 200:
raise GoodreadsRequestException(resp.reason, self.path)
if self.req_format == 'xml':
data_dict = xmltodict.parse(resp.content)
return data_dict['GoodreadsResponse']
else:
raise Exception("Invalid format")
log = logger.create()
_client = None # type: GoodreadsClient
@@ -38,20 +69,20 @@ _CACHE_TIMEOUT = 23 * 60 * 60 # 23 hours (in seconds)
_AUTHORS_CACHE = {}
def connect(key=None, secret=None, enabled=True):
def connect(key=None, enabled=True):
global _client
if not enabled or not key or not secret:
if not enabled or not key:
_client = None
return
if _client:
# make sure the configuration has not changed since last we used the client
if _client.client_key != key or _client.client_secret != secret:
if _client.client_key != key:
_client = None
if not _client:
_client = GoodreadsClient(key, secret)
_client = GoodreadsClient(key, None)
def get_author_info(author_name):