Skip to content
Draft
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
1 change: 1 addition & 0 deletions docker/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lenny/configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
1 change: 1 addition & 0 deletions lenny/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions lenny/core/openlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*',
Expand Down Expand Up @@ -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):
Expand Down