Skip to content
Travelogues.ipynb 12.9 KiB
Newer Older
Georg Petz's avatar
Georg Petz committed
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get the MMS ID for a given barcode, get the metadata via `https://obv-at-oenb.alma.exlibrisgroup.com/view/sru/43ACC_ONB?version=1.2&query=alma.barcode=<barcode>startRecord=0&maximumRecords=1&operation=searchRetrieve&recordSchema=dc` and extract the MMS ID with `/srw:searchRetrieveResponse/srw:records/srw:record/srw:recordIdentifier/text()`.\n",
    "\n",
    "Metadata in Dublin Core can be obtained via SRU.\n",
    "\n",
    "Linked Data from ALMA (library management system) can be retrieved in \n",
    "\n",
    "* BIBFRAME via `https://open-na.hosted.exlibrisgroup.com/alma/<institution code>/bf/entity/instance/<mms id>`\n",
    "* JSON-LD via `https://open-na.hosted.exlibrisgroup.com/alma/<institution code>/bibs/<mms_id>.jsonld`\n",
    "* RDA/RDF via `https://open-na.hosted.exlibrisgroup.com/alma/<institution code>/rda/entity/manifestation/<mms id>.rdf`\n",
    "\n",
    "For a Network Zone MMS ID the institution code is 43ACC_NETWORK and for the Institution MMS ID it is 43ACC_ONB.\n",
    "\n",
    "The following xpath `/rdf:RDF/bf:Instance/bf:hasItem/bf:Item/bf:electronicLocator/rdfs:Resource/bflc:locator/@rdf:resource` selects URLs of the Viewer. We use the + sign (URL encoded %2B) to spilt the URL in order to extract the Barcode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getMMS_ID(barcode):\n",
    "    cont=requests.get('https://obv-at-oenb.alma.exlibrisgroup.com/view/sru/43ACC_ONB?version=1.2&query=alma.barcode=%2BZ' + barcode + '&startRecord=0&maximumRecords=1&operation=searchRetrieve&recordSchema=dc').content\n",
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'srw': 'http://www.loc.gov/zing/srw/'\n",
    "    }\n",
    "    result = e.xpath('/srw:searchRetrieveResponse/srw:records/srw:record/srw:recordIdentifier/text()', namespaces=namespaces)\n",
    "    return result[0]\n",
    "\n",
Georg Petz's avatar
Georg Petz committed
    "def getYear(barcode):\n",
    "    cont=requests.get('https://obv-at-oenb.alma.exlibrisgroup.com/view/sru/43ACC_ONB?version=1.2&query=alma.barcode=%2BZ' + barcode + '&startRecord=0&maximumRecords=1&operation=searchRetrieve&recordSchema=marcxml').content\n",
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'srw': 'http://www.loc.gov/zing/srw/',\n",
    "        'marc21': 'http://www.loc.gov/MARC21/slim'\n",
    "    }    \n",
    "    xpath = '/srw:searchRetrieveResponse/srw:records/srw:record/srw:recordData/marc21:record/marc21:datafield[@tag=\\'264\\']/marc21:subfield[@code=\\'{}\\']/text()'\n",
    "    yearResult = e.xpath(xpath.format('c'), namespaces=namespaces)\n",
    "    year = \"; \".join(yearResult) if yearResult else ''\n",
    "    placeResult = e.xpath(xpath.format('a'), namespaces=namespaces)\n",
    "    place = \"; \".join(placeResult) if placeResult else ''\n",
    "    return[year, place]\n",
    "\n",
    "def getDCDataMMS(mms_id):\n",
    "    cont=requests.get('https://obv-at-oenb.alma.exlibrisgroup.com/view/sru/43ACC_ONB?version=1.2&query=alma.mms_id=' + mms_id + '&startRecord=0&maximumRecords=1&operation=searchRetrieve&recordSchema=dc').content\n",
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'srw': 'http://www.loc.gov/zing/srw/',\n",
    "        'srw_dc': 'info:srw/schema/1/dc-schema',\n",
    "        'dc': 'http://purl.org/dc/elements/1.1/'\n",
    "    }\n",
    "    xpath = '/srw:searchRetrieveResponse/srw:records/srw:record/srw:recordData/srw_dc:dc/dc:{}/text()'\n",
    "    \n",
    "    titleResult = e.xpath(xpath.format('title'), namespaces=namespaces)\n",
    "    title = \"; \".join(titleResult) if titleResult else ''\n",
    "    \n",
    "    contributorResult = e.xpath(xpath.format('contributor'), namespaces=namespaces)\n",
    "    contributor = \"; \".join(contributorResult) if contributorResult else ''\n",
    "    \n",
    "    dateResult = e.xpath(xpath.format('date'), namespaces=namespaces)\n",
    "    date = \"; \".join(dateResult) if dateResult else ''\n",
    "    print([title, contributor, date])\n",
    "    return [title, contributor, date]\n",
    "\n",
Georg Petz's avatar
Georg Petz committed
    "def getDCData(barcode):\n",
Georg Petz's avatar
Georg Petz committed
    "    cont=requests.get('https://obv-at-oenb.alma.exlibrisgroup.com/view/sru/43ACC_ONB?version=1.2&query=alma.barcode=' + barcode + '&startRecord=0&maximumRecords=1&operation=searchRetrieve&recordSchema=dc').content\n",
Georg Petz's avatar
Georg Petz committed
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'srw': 'http://www.loc.gov/zing/srw/',\n",
    "        'srw_dc': 'info:srw/schema/1/dc-schema',\n",
    "        'dc': 'http://purl.org/dc/elements/1.1/'\n",
    "    }\n",
    "    xpath = '/srw:searchRetrieveResponse/srw:records/srw:record/srw:recordData/srw_dc:dc/dc:{}/text()'\n",
    "    \n",
    "    titleResult = e.xpath(xpath.format('title'), namespaces=namespaces)\n",
    "    title = \"; \".join(titleResult) if titleResult else ''\n",
    "    \n",
    "    contributorResult = e.xpath(xpath.format('contributor'), namespaces=namespaces)\n",
    "    contributor = \"; \".join(contributorResult) if contributorResult else ''\n",
    "    \n",
    "    dateResult = e.xpath(xpath.format('date'), namespaces=namespaces)\n",
    "    date = \"; \".join(dateResult) if dateResult else ''\n",
    "    print([title, contributor, date])\n",
    "    return [title, contributor, date]\n",
    "\n",
    "def getLinksAndBarcodes(local_mms_id):\n",
    "    cont=requests.get('https://open-na.hosted.exlibrisgroup.com/alma/43ACC_ONB/bf/entity/instance/' + local_mms_id).content\n",
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',\n",
    "        'bf': 'http://id.loc.gov/ontologies/bibframe/',\n",
    "        'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',\n",
    "        'bflc': 'http://id.loc.gov/ontologies/bflc/'\n",
    "    }\n",
    "    result = e.xpath('/rdf:RDF/bf:Instance/bf:hasItem/bf:Item/bf:electronicLocator/rdfs:Resource/bflc:locator/@rdf:resource', namespaces=namespaces)\n",
    "    barcodes = []\n",
    "    for link in result:\n",
    "        splits = link.split('%2B')\n",
    "        if len(splits) >= 2:\n",
    "            barcodes.append('+' + link.split('%2B')[1])\n",
    "    print(local_mms_id + ': ' + \", \".join(barcodes))\n",
    "    linksJoined = \", \".join(result)\n",
    "    barcodesJoined = \", \".join(barcodes)\n",
    "    #returns a list with URLs and Barcodes\n",
Georg Petz's avatar
Georg Petz committed
    "    return [linksJoined, barcodesJoined]\n",
    "\n",
    "def getCatalogLink(local_mms_id):\n",
    "    print(local_mms_id)\n",
    "    cont=requests.get('https://open-na.hosted.exlibrisgroup.com/alma/43ACC_ONB/bf/entity/instance/' + local_mms_id).content\n",
    "    e = etree.XML(cont)\n",
    "    namespaces = {\n",
    "        'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',\n",
    "        'bf': 'http://id.loc.gov/ontologies/bibframe/',\n",
    "        'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',\n",
    "        'bflc': 'http://id.loc.gov/ontologies/bflc/'\n",
    "    }    \n",
    "    result = e.xpath('/rdf:RDF/bf:Work/bf:partOf/bf:Work/bf:identifiedBy/bf:Identifier/rdf:value/text()', namespaces=namespaces)\n",
    "    if not result:\n",
    "        result = e.xpath('/rdf:RDF/bf:Instance/bf:identifiedBy/bf:Local/rdf:value/text()', namespaces=namespaces)\n",
    "    return 'http://data.onb.ac.at/rec/' + result[0]"
Georg Petz's avatar
Georg Petz committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
Georg Petz's avatar
Georg Petz committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Sammlung der besten Reisebeschreibungen', '', '1784']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Sammlung der besten Reisebeschreibungen', '', '1784']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getDCDataMMS(str(990048102650603338))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
Georg Petz's avatar
Georg Petz committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Vorstellung der vornehmsten Völkerschaften der Welt nach ihrer Abstammung, Ausbreitung und Sprachen', 'Breitenbauch, Georg-August vonaut', '1786']\n",
      "['Vorstellung der vornehmsten Völkerschaften der Welt nach ihrer Abstammung, Ausbreitung und Sprachen', 'Breitenbauch, Georg-August vonaut', '1786']\n"
     ]
    }
   ],
   "source": [
    "dc = getDCData(str(178966306))\n",
    "print(dc)"
   ]
  },
  {
   "cell_type": "code",
Georg Petz's avatar
Georg Petz committed
   "execution_count": 5,
Georg Petz's avatar
Georg Petz committed
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Identifier</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
Georg Petz's avatar
Georg Petz committed
       "      <th>0</th>\n",
       "      <td>Z164418102</td>\n",
Georg Petz's avatar
Georg Petz committed
       "    </tr>\n",
       "    <tr>\n",
Georg Petz's avatar
Georg Petz committed
       "      <th>2</th>\n",
       "      <td>Z97787406</td>\n",
Georg Petz's avatar
Georg Petz committed
       "    </tr>\n",
       "    <tr>\n",
Georg Petz's avatar
Georg Petz committed
       "      <th>3</th>\n",
       "      <td>Z198357107</td>\n",
Georg Petz's avatar
Georg Petz committed
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
Georg Petz's avatar
Georg Petz committed
       "   Identifier\n",
       "0  Z164418102\n",
Georg Petz's avatar
Georg Petz committed
       "2   Z97787406\n",
       "3  Z198357107"
Georg Petz's avatar
Georg Petz committed
      ]
     },
Georg Petz's avatar
Georg Petz committed
     "execution_count": 5,
Georg Petz's avatar
Georg Petz committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel('exampleBarcodes.xlsx')\n",
    "df_sample = df.sample(3).copy()\n",
    "df_sample"
   ]
  },
  {
   "cell_type": "code",
Georg Petz's avatar
Georg Petz committed
   "execution_count": 6,
Georg Petz's avatar
Georg Petz committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Sammlung der besten Reisebeschreibungen', '', '1784']\n",
      "['Sammlung der besten Reisebeschreibungen', '', '1784']\n",
      "['Geschichte der Reisen die seit Cook an der Nordwest- und Nordost-Küste von Amerika und in dem nördlichsten Amerika selbst von Meares,  Dixon, Portlock, Coxe, Long u. a. m. unternommen worden sind Mit vielen Karten und Kupfern', 'Forster, Georg1754-1794(DE-588)118534416edtO:H; Dixon, George1755-1800(DE-588)130525294ctbO:H; Long, JohnctbO:H; Meares, John1756-1809(DE-588)121248275ctbO:H; Portlock, Nathaniel1748-1817(DE-588)133848531ctbO:H; Portlock, NathanielautO:800O:H; Forster, [Johann] GeorgautAdamO:806O:H; Vossische BuchhandlungBerlin(DE-588)64386-5pblO:H', '1792']\n",
      "['Sammlung der besten Reisebeschreibungen', 'Traßler, Joseph Georg1759-1816(DE-588)129262358prt', '1784']\n",
Georg Petz's avatar
Georg Petz committed
      "['', '', '']\n",
Georg Petz's avatar
Georg Petz committed
      "['Sammlung der besten Reisebeschreibungen', '', '1784']\n",
      "['Beschreibung der äussern und innern Merkwürdigkeiten der Königlichen Schlösser in Berlin, Charlottenburg, Schönhausen in und bey Potsdam', 'Rumpf, Friedrichaut', '1794']\n",
      "[\"Neues Elementarwerk für die niedern Klassen lateinischer Schulen und Gymnasien nach einem zusammenhängenden und auf die Lesung klassischer Autoren in den obern Klassen, wie auch auf die übrigen Vorerkenntnisse künftiger Studirenden gründlich vorbereitenden Plane; <<M. J. E. Fabri's>> Elementargeographie; M. J. E. Fabri's Elementargeographie; Fabri's Elementargeographie; M. J. E. Fabri's Elementargeographie; zweiten Zweiter\", 'Fabri, Johann Ernst1755-1825(DE-588)11536028Xaut; Gebauer, Johann Jakobpbl', '1790']\n",
Georg Petz's avatar
Georg Petz committed
      "['', '', '']\n"
Georg Petz's avatar
Georg Petz committed
     ]
    }
   ],
   "source": [
    "df[['Titel', 'Autor', 'Erscheinungsjahr']] = df.apply(lambda row: getDCData(str(row['Identifier'])[1:]), axis=1, result_type='expand')"
   ]
  },
  {
   "cell_type": "code",
Georg Petz's avatar
Georg Petz committed
   "execution_count": 7,
Georg Petz's avatar
Georg Petz committed
   "metadata": {},
Georg Petz's avatar
Georg Petz committed
   "outputs": [],
Georg Petz's avatar
Georg Petz committed
   "source": [
Georg Petz's avatar
Georg Petz committed
    "writer = pd.ExcelWriter(r'exampleBarcodes_extended.xlsx', engine='xlsxwriter',options={'strings_to_urls': False})\n",
Georg Petz's avatar
Georg Petz committed
    "df.to_excel(writer)\n",
    "writer.close()"
Georg Petz's avatar
Georg Petz committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
Georg Petz's avatar
Georg Petz committed
   "display_name": "labs",
Georg Petz's avatar
Georg Petz committed
   "language": "python",
Georg Petz's avatar
Georg Petz committed
   "name": "labs"
Georg Petz's avatar
Georg Petz committed
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}