Skip to content

Instantly share code, notes, and snippets.

@danmichaelo
Last active July 10, 2023 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danmichaelo/fea2a222efd00fc47531 to your computer and use it in GitHub Desktop.
Save danmichaelo/fea2a222efd00fc47531 to your computer and use it in GitHub Desktop.
import logging
import json
from requests import Session
# Variables
api_key = 'SECRET' # API key for prod read/write
scan_in_params = {
'op': 'scan',
'external_id': False,
'library': '1030310', # Replace with your library code
'circ_desk': 'DEFAULT_CIRC_DESK' # Replace with your circ desk
}
base_url = 'https://api-eu.hosted.exlibrisgroup.com/almaws/v1'
barcode_api = base_url + '/items?item_barcode={barcode}'
item_api = base_url + '/bibs/{mms_id}/holdings/{holding_id}/items/{item_id}'
# Setup logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Setup requests session
session = Session()
session.headers.update({
'accept': 'application/json',
'authorization': 'apikey {}'.format(api_key)
})
with open('barcodes.txt', 'r') as fp:
# For each line in the file
for line in fp:
# Strip away any extra whitespace
barcode = line.strip()
# Make a barcode lookup request and parse the response as JSON
res = session.get(barcode_api.format(barcode=barcode))
doc = json.loads(res.text)
# Check if we got an error response
if 'errorList' in doc:
status = doc['errorList']['error'][0]['errorMessage']
logging.error('%s %s', barcode, status)
continue # jump to next item
# Extract MMS id, holding id and item id from the response
mms_id = doc['bib_data']['mms_id']
holding_id = doc['holding_data']['holding_id']
item_id = doc['item_data']['pid']
# Do the scan-in POST request:
res = session.post(item_api.format(mms_id=mms_id,
holding_id=holding_id,
item_id=item_id), params=scan_in_params)
doc = json.loads(res.text)
# Check if we got an error response
if 'errorList' in doc:
status = doc['errorList']['error'][0]['errorMessage']
logging.error('%s %s', barcode, status)
continue # jump to next item
status = doc['additional_info']
logging.info('%s %s', barcode, status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment