<commit_before># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
from os import environ
from urllib.request import urlopen

from .uri import URI, APIQuerier
from .marc import get_marc_data_from_xml
from .worldcat import get_worldcat_data_from_json
from .hathi import get_oclc_counts_from_json
from .common import ReadOnlyDataStructure

AlephURI = URI("http://mirlyn-aleph.lib.umich.edu/cgi-bin/bc2meta")
WorldCatURI = URI("http://www.worldcat.org/webservices/catalog"
                  "/content/libraries/{oclc}")
HathiURI = URI("http://catalog.hathitrust.org/api/volumes/brief"
               "/oclc/{oclc}.json")

aleph_api = APIQuerier(AlephURI, url_opener=urlopen)
worldcat_api = APIQuerier(WorldCatURI, url_opener=urlopen)
hathi_api = APIQuerier(HathiURI, url_opener=urlopen)

wc_key = environ.get("MDP_REJECT_WC_KEY", "none")

class VolumeDataFromBarcode:

    def __init__ (self, barcode):
        self.barcode = barcode
        self.marc = get_marc_data_from_xml(aleph_api.get(
                        id=barcode,
                        type="bc",
                        schema="marcxml"))

        if self.marc.oclc is None:
            worldcat, hathi = None, None

        else:
            worldcat = worldcat_api.get(
                    oclc=self.marc.oclc,
                    wskey=wc_key,
                    format="json",
                    maximumLibraries="50")
            hathi = hathi_api.get(oclc=self.marc.oclc)

        self.worldcat = get_worldcat_data_from_json(worldcat)
        self.hathi = get_oclc_counts_from_json(hathi, "mdp." + barcode)<commit_msg>Add alternate MDP-ID aleph API if the first fails<commit_after># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
from os import environ
from urllib.request import urlopen

from .uri import URI, APIQuerier
from .marc import get_marc_data_from_xml
from .worldcat import get_worldcat_data_from_json
from .hathi import get_oclc_counts_from_json
from .common import ReadOnlyDataStructure

AlephURI = URI("http://mirlyn-aleph.lib.umich.edu/cgi-bin/bc2meta")
WorldCatURI = URI("http://www.worldcat.org/webservices/catalog"
                  "/content/libraries/{oclc}")
HathiURI = URI("http://catalog.hathitrust.org/api/volumes/brief"
               "/oclc/{oclc}.json")

aleph_api = APIQuerier(AlephURI, url_opener=urlopen)
worldcat_api = APIQuerier(WorldCatURI, url_opener=urlopen)
hathi_api = APIQuerier(HathiURI, url_opener=urlopen)

wc_key = environ.get("MDP_REJECT_WC_KEY", "none")

class VolumeDataFromBarcode:

    def __init__ (self, barcode):
        self.barcode = barcode
        self.marc = get_marc_data_from_xml(aleph_api.get(
                        id=barcode,
                        type="bc",
                        schema="marcxml"))

        if not self.marc:
            self.marc = get_marc_data_from_xml(aleph_api.get(
                            id="mdp." + barcode,
                            schema="marcxml"))

        if self.marc.oclc is None:
            worldcat, hathi = None, None

        else:
            worldcat = worldcat_api.get(
                    oclc=self.marc.oclc,
                    wskey=wc_key,
                    format="json",
                    maximumLibraries="50")
            hathi = hathi_api.get(oclc=self.marc.oclc)

        self.worldcat = get_worldcat_data_from_json(worldcat)
        self.hathi = get_oclc_counts_from_json(hathi, "mdp." + barcode)
{'fraction_non_alphanumeric': 0.09553791404325213, 'fraction_numerical': 0.0038324664659184233, 'mean_word_length': 2.5441319107662466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4790993', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1178, 'n_words': 241}