Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user-provided Google Books API key to fix "429 Client Error: Too Many Requests" #3281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cps/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,9 @@ def _configuration_update_helper():
services.goodreads_support.connect(config.config_goodreads_api_key,
config.config_use_goodreads)

# Google Books API configuration
_config_string(to_save, "config_googlebooks_api_key")

_config_int(to_save, "config_updatechannel")

# Reverse proxy login configuration
Expand Down
1 change: 1 addition & 0 deletions cps/config_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class _Settings(_Base):

config_use_goodreads = Column(Boolean, default=False)
config_goodreads_api_key = Column(String)
config_googlebooks_api_key = Column(String, default='')
config_register_email = Column(Boolean, default=False)
config_login_type = Column(Integer, default=0)

Expand Down
5 changes: 3 additions & 2 deletions cps/metadata_provider/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import requests

from cps import logger
from cps import logger, config
from cps.isoLanguages import get_lang3, get_language_name
from cps.services.Metadata import MetaRecord, MetaSourceInfo, Metadata

Expand All @@ -38,6 +38,7 @@ class Google(Metadata):
BOOK_URL = "https://books.google.com/books?id="
SEARCH_URL = "https://www.googleapis.com/books/v1/volumes?q="
ISBN_TYPE = "ISBN_13"
API_KEY = "&key=" + config.config_googlebooks_api_key

def search(
self, query: str, generic_cover: str = "", locale: str = "en"
Expand All @@ -50,7 +51,7 @@ def search(
tokens = [quote(t.encode("utf-8")) for t in title_tokens]
query = "+".join(tokens)
try:
results = requests.get(Google.SEARCH_URL + query)
results = requests.get(Google.SEARCH_URL + query + Google.API_KEY)
results.raise_for_status()
except Exception as e:
log.warning(e)
Expand Down
4 changes: 4 additions & 0 deletions cps/templates/config_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ <h4 class="panel-title">
</div>
</div>
{% endif %}
<div class="form-group">
<label for="config_googlebooks_api_key">{{_('Google Books API Key')}}</label>
<input type="text" class="form-control" id="config_googlebooks_api_key" name="config_googlebooks_api_key" value="{% if config.config_googlebooks_api_key != None %}{{ config.config_googlebooks_api_key }}{% endif %}" autocomplete="off">
</div>
<div class="form-group">
<input type="checkbox" id="config_allow_reverse_proxy_header_login" name="config_allow_reverse_proxy_header_login" data-control="reverse-proxy-login-settings" {% if config.config_allow_reverse_proxy_header_login %}checked{% endif %}>
<label for="config_allow_reverse_proxy_header_login">{{_('Allow Reverse Proxy Authentication')}}</label>
Expand Down