-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
coding recipes
Any time you want to import and test Open Library code that needs access to web.ctx, you'll need to run a magic incantation.
First, exec into the docker container and launch python:
docker compose exec -it web pythonNext, use the following code to load Open Library and launch a minimal headless app:
import web
import infogami
import os
from openlibrary.config import load_config
path = '/olsystem/etc/openlibrary.yml' # prod
if not os.path.exists('/olsystem/etc/openlibrary.yml'):
path = 'config/openlibrary.yml' # fallback to dev
load_config(path)
infogami._setup()
from infogami import configMost privileged actions/requests Open Library takes on behalf of a patron will require their s3 keys:
from openlibrary import accounts
from openlibrary.accounts.model import OpenLibraryAccount
logged_in_user = accounts.get_current_user()
username = logged_in_user and logged_in_user.key.split('/')[-1]
account = username and OpenLibraryAccount.get(username=username)
s3_keys = web.ctx.site.store.get(account._key).get('s3_keys')keys = ["/works/OL5285479W", "/works/OL257943W", "/works/OL27448W"]
docs = web.ctx.site.get_many(keys)How to determine, given a set of patron usernames, which have public reading logs / can be followed?
usernames = ['mekBot', 'ScarTissue', 'seabelis', 'brewster', 'jachamp']
user_prefs = web.ctx.site.get_many([f'/people/{username}/preferences' for username in usernames])
followable = dict((user.key.split('/')[2], user.notifications.public_readlog == 'yes') for user in user_prefs)The following must be called within a controller that has access to web.ctx:
user = accounts.get_current_user()See: https://openlibrary.org/dev/docs/api/search
See: https://github.com/internetarchive/openlibrary/pull/7928
You might want to set a cookie when a patron logs in or registers to persist information, such as a setting, across page views. For instance, if a patron logs in and they have print disabled access (something we'd need to check on every page view) we can save this using a session cookie:
web.setcookie('sfw', 'yes', expires="")
Setting cookies example: https://github.com/internetarchive/openlibrary/pull/7935/files#diff-ef23faa0d3112e7eef1f574a58b0710f9604f911f5a57e795e45c13c4b91fa19R342
Clearing cookies example: https://github.com/internetarchive/openlibrary/pull/8490/files#diff-884f3a712a7e51cce625ca19060335684272f33a967702b19e66e48dbdd1be69R131-R133
import web
import infogami
from openlibrary.config import load_config
load_config('/olsystem/etc/openlibrary.yml')
infogami._setup()
from infogami import config
from openlibrary.core.vendors import AmazonAPI
web.amazon_api = AmazonAPI(*[config.amazon_api.get('key'), config.amazon_api.get('secret'),config.amazon_api.get('id')], throttling=0.9)
book = web.amazon_api.get_products(["1666568287"], serialize=True)- Log into the host
ol-home0 docker exec -it openlibrary-affiliate-server-1 bashcurl localhost:31337/status
Or to monitor repeatedly during debugging:
% cat > ~/affiliate_status.sh
#!/bin/bash
while true
do
curl --silent localhost:31337/status | cut -c 1-90
sleep 1
done- Fetching patron's lists