diff --git a/docker/configure.sh b/docker/configure.sh index a5050a9..9b87ef8 100755 --- a/docker/configure.sh +++ b/docker/configure.sh @@ -47,6 +47,7 @@ LENNY_LOG_LEVEL=$LENNY_LOG_LEVEL LENNY_RELOAD=$LENNY_RELOAD LENNY_SSL_CRT=$LENNY_SSL_CRT LENNY_SSL_KEY=$LENNY_SSL_KEY +LENNY_OLSYNC=1 # Service Ports READER_PORT=$READER_PORT diff --git a/lenny/configs/__init__.py b/lenny/configs/__init__.py index 328ace6..2cb27fc 100644 --- a/lenny/configs/__init__.py +++ b/lenny/configs/__init__.py @@ -20,11 +20,13 @@ PORT = int(os.environ.get('LENNY_PORT', 8080)) WORKERS = int(os.environ.get('LENNY_WORKERS', 1)) DEBUG = bool(int(os.environ.get('LENNY_DEBUG', 0))) +OLSYNC = bool(int(os.environ.get('LENNY_OLSYNC', 1))) LOG_LEVEL = os.environ.get('LENNY_LOG_LEVEL', 'info') SSL_CRT = os.environ.get('LENNY_SSL_CRT') SSL_KEY = os.environ.get('LENNY_SSL_KEY') LENNY_HTTP_HEADERS = {"User-Agent": "LennyImportBot/1.0"} + READER_PORT = int(os.environ.get('READER_PORT', 3000)) READIUM_PORT = int(os.environ.get('READIUM_PORT', 15080)) READIUM_BASE_URL = f"http://lenny_readium:{READIUM_PORT}" diff --git a/lenny/core/api.py b/lenny/core/api.py index 24b12e8..ce5008e 100644 --- a/lenny/core/api.py +++ b/lenny/core/api.py @@ -218,6 +218,7 @@ def add(cls, openlibrary_edition: int, files: list[UploadFile], uploader_ip:str, ) db.add(item) db.commit() + OpenLibrary.notify_changes(openlibrary_edition, uploader_ip) return item except Exception as e: db.rollback() diff --git a/lenny/core/openlibrary.py b/lenny/core/openlibrary.py index 4a55880..756a8ab 100644 --- a/lenny/core/openlibrary.py +++ b/lenny/core/openlibrary.py @@ -3,14 +3,15 @@ from urllib.parse import urlencode import logging -from lenny.configs import LENNY_HTTP_HEADERS +from lenny.configs import OLSYNC logger = logging.getLogger(__name__) class OpenLibrary: SEARCH_URL = "https://openlibrary.org/search.json" - HTTP_HEADERS = LENNY_HTTP_HEADERS + NOTIFY_URL = "https://openlibrary.org/notify.json" + HTTP_HEADERS = {"User-Agent": "LennyImportBot/1.0"} HTTP_TIMEOUT = 5 DEFAULT_FIELDS = [ 'key', 'title', 'author_key', 'author_name', 'editions', 'editions.*', @@ -70,6 +71,15 @@ def search_json(cls, query: str, fields: Optional[List[str]] = None, page: int = logger.error(f"Error searching Open Library: {e}") return {} + @classmethod + def notify_changes(cls, olid, host): + """Notifies openlibrary.org of changes to this instance; can be toggled with + OLSYNC config. + """ + if OLSYNC: + r = requests.post(cls.NOTIFY_URL, headers=cls.HTTP_HEADERS, timeout=cls.HTTP_TIMEOUT) + re.raise_for_status() + class OpenLibraryRecord(dict): def __init__(self, data=None, **kwargs):