|
| 1 | +import os |
| 2 | +import re |
| 3 | +import requests |
| 4 | +from requests.exceptions import JSONDecodeError, RequestException |
| 5 | + |
| 6 | +CACHE = {} |
| 7 | + |
| 8 | + |
| 9 | +def parse_research_catalog_identifier(identifier: str): |
| 10 | + """ |
| 11 | + Given a RC identifier (e.g. "b1234", "pb9876", "pi4567"), returns a dict |
| 12 | + defining: |
| 13 | + - nyplSource: One of sierra-nypl, recap-pul, recap-cul, or recap-hl (at |
| 14 | + writing) |
| 15 | + - nyplType: One of bib, holding, or item |
| 16 | + - id: The numeric string id |
| 17 | + """ |
| 18 | + if not isinstance(identifier, str): |
| 19 | + raise ResearchCatalogIdentifierError( |
| 20 | + f'Invalid RC identifier: {identifier}') |
| 21 | + |
| 22 | + # Extract prefix from the identifier: |
| 23 | + match = re.match(r'^([a-z]+)', identifier) |
| 24 | + if match is None: |
| 25 | + raise ResearchCatalogIdentifierError( |
| 26 | + f'Invalid RC identifier: {identifier}') |
| 27 | + prefix = match[0] |
| 28 | + |
| 29 | + # The id is the identifier without the prefix: |
| 30 | + id = identifier.replace(prefix, '') |
| 31 | + nyplType = None |
| 32 | + nyplSource = None |
| 33 | + |
| 34 | + # Look up nyplType and nyplSource in nypl-core based on the prefix: |
| 35 | + for _nyplSource, mapping in nypl_core_source_mapping().items(): |
| 36 | + if mapping.get('bibPrefix') == prefix: |
| 37 | + nyplType = 'bib' |
| 38 | + elif mapping.get('itemPrefix') == prefix: |
| 39 | + nyplType = 'item' |
| 40 | + elif mapping.get('holdingPrefix') == prefix: |
| 41 | + nyplType = 'holding' |
| 42 | + if nyplType is not None: |
| 43 | + nyplSource = _nyplSource |
| 44 | + break |
| 45 | + |
| 46 | + if nyplSource is None: |
| 47 | + raise ResearchCatalogIdentifierError( |
| 48 | + f'Invalid RC identifier: {identifier}') |
| 49 | + |
| 50 | + return { |
| 51 | + 'nyplSource': nyplSource, |
| 52 | + 'nyplType': nyplType, |
| 53 | + 'id': id |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def research_catalog_id_prefix(nyplSource: str, nyplType='bib'): |
| 58 | + """ |
| 59 | + Given a nyplSource (e.g. 'sierra-nypl') and nyplType (e.g. 'item'), returns |
| 60 | + the relevant prefix used in the RC identifier (e.g. 'i') |
| 61 | + """ |
| 62 | + if nypl_core_source_mapping().get(nyplSource) is None: |
| 63 | + raise ResearchCatalogIdentifierError( |
| 64 | + f'Invalid nyplSource: {nyplSource}') |
| 65 | + |
| 66 | + if not isinstance(nyplType, str): |
| 67 | + raise ResearchCatalogIdentifierError( |
| 68 | + f'Invalid nyplType: {nyplType}') |
| 69 | + |
| 70 | + prefixKey = f'{nyplType}Prefix' |
| 71 | + if nypl_core_source_mapping()[nyplSource].get(prefixKey) is None: |
| 72 | + raise ResearchCatalogIdentifierError(f'Invalid nyplType: {nyplType}') |
| 73 | + |
| 74 | + return nypl_core_source_mapping()[nyplSource][prefixKey] |
| 75 | + |
| 76 | + |
| 77 | +def nypl_core_source_mapping(): |
| 78 | + """ |
| 79 | + Builds a nypl-source-mapping by retrieving the mapping from NYPL-Core |
| 80 | + """ |
| 81 | + name = 'nypl-core-source-mapping' |
| 82 | + if not CACHE.get(name) is None: |
| 83 | + return CACHE[name] |
| 84 | + |
| 85 | + url = os.environ.get('NYPL_CORE_SOURCE_MAPPING_URL', |
| 86 | + 'https://raw.githubusercontent.com/NYPL/nypl-core/master/mappings/recap-discovery/nypl-source-mapping.json') # noqa |
| 87 | + try: |
| 88 | + response = requests.get(url) |
| 89 | + response.raise_for_status() |
| 90 | + except RequestException as e: |
| 91 | + raise ResearchCatalogIdentifierError( |
| 92 | + 'Failed to retrieve nypl-core source-mapping file from {url}:' |
| 93 | + ' {errorType} {errorMessage}' |
| 94 | + .format(url=url, errorType=type(e), errorMessage=e)) from None |
| 95 | + |
| 96 | + try: |
| 97 | + CACHE[name] = response.json() |
| 98 | + return CACHE[name] |
| 99 | + except (JSONDecodeError, KeyError) as e: |
| 100 | + raise ResearchCatalogIdentifierError( |
| 101 | + 'Failed to parse nypl-core source-mapping file: {errorType}' |
| 102 | + ' {errorMessage}' |
| 103 | + .format(errorType=type(e), errorMessage=e)) from None |
| 104 | + |
| 105 | + |
| 106 | +class ResearchCatalogIdentifierError(Exception): |
| 107 | + def __init__(self, message=None): |
| 108 | + self.message = message |
0 commit comments