{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "aa88188f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "try:\n", " import IPython\n", "except:\n", " %pip install IPython\n", " import IPython \n", "from IPython.display import display, IFrame, HTML, Javascript\n", "from IPython.core.display import display, HTML\n", "\n", "HTML(\"\"\"\"\"\")" ] }, { "cell_type": "markdown", "id": "547477a2", "metadata": {}, "source": [ "# Rijksmuseum\n", "\n", "The Rijksmuseum provides an API to query data. \n", "\n", " - To start using the data and images, you first need to obtain an API key by registering for a Rijksstudio account. \n", " - You will be given a key instantly upon request, which you can find at the advanced settings of your Rijksstudio account.\n", " \n", " #### Further Reading\n", " - [Rijksmuseum](https://www.rijksmuseum.nl/nl)\n", " - [Rijksmuseum API](https://data.rijksmuseum.nl/object-metadata/api/)" ] }, { "cell_type": "code", "execution_count": 6, "id": "3969a153", "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import os\n", "try:\n", " import pandas as pd\n", "except:\n", " %pip install pandas\n", " import pandas as pd\n", " \n", "fileNGA = 'data/nga/input/nga_ruskin.csv'\n", "keyfile = \"data/ruskin/input/keyrijks.txt\"\n", "key = open(keyfile, mode='r', encoding='utf-8-sig').read()\n", "url = \"https://www.rijksmuseum.nl/api/nl/collection?key=\" + key + \"&involvedMaker=John+Ruskin\"\n", "rijksdir = \"data/rijks/json/\"\n", "outputdir = \"data/ruskin/output/json/\"\n", "baseURI = \"http://www.rijksmuseum.nl/nl/collectie/\"\n", "\n", "\n", "response = requests.get(url)\n", "data = response.json()\n", "\n", "for artwork in data[\"artObjects\"]:\n", "\n", " if artwork[\"hasImage\"] == False:\n", " continue\n", " if artwork[\"principalOrFirstMaker\"] != \"John Ruskin\":\n", " continue\n", " text_file = open( rijksdir + artwork[\"id\"] + \".json\", \"wt\")\n", " text_file.write(json.dumps(artwork, indent=2))\n", " text_file.close()\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 7, "id": "5709fa66", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0
idid
accession_numberobjectNumber
classification
titletitle
alt_titlelongTitle
notes
date_created
date_created_earliest
date_created_latest
created_period
created_dynasty
created_inscriptions
created_notes
creatorprincipalOrFirstMaker
physical_medium
physical_style
physical_technique
physical_description
physical_dimensions
created_provenance
credit_line
collection
current_status
current_owner
image_url
homepage
\n", "
" ], "text/plain": [ " 0\n", "id id\n", "accession_number objectNumber\n", "classification \n", "title title\n", "alt_title longTitle\n", "notes \n", "date_created \n", "date_created_earliest \n", "date_created_latest \n", "created_period \n", "created_dynasty \n", "created_inscriptions \n", "created_notes \n", "creator principalOrFirstMaker\n", "physical_medium \n", "physical_style \n", "physical_technique \n", "physical_description \n", "physical_dimensions \n", "created_provenance \n", "credit_line \n", "collection \n", "current_status \n", "current_owner \n", "image_url \n", "homepage " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mapp = {\n", " \n", " \"id\":\"id\",\n", " \"accession_number\":\"objectNumber\",\n", " \"classification\" : \"\",\n", " \"title\": \"title\",\n", " \"alt_title\": \"longTitle\",\n", " \"notes\": \"\",\n", " \"date_created\":\"\",\n", " \"date_created_earliest\": \"\",\n", " \"date_created_latest\": \"\",\n", " \"created_period\":\"\",\n", " \"created_dynasty\":\"\",\n", " \"created_inscriptions\":\"\",\n", " \"created_notes\": \"\",\n", " \"creator\":\"principalOrFirstMaker\",\n", " \"physical_medium\": \"\",\n", " \"physical_style\": \"\",\n", " \"physical_technique\": \"\",\n", " \"physical_description\": \"\",\n", " \"physical_dimensions\": \"\",\n", " \"created_provenance\": \"\" ,\n", " \"credit_line\": \"\",\n", " \"collection\" : \"\",\n", " \"classification\": \"\",\n", " \"current_status\" : \"\",\n", " \"current_owner\": \"\",\n", " \"image_url\":\"\",\n", " \"homepage\" : \"\"\n", "}\n", "\n", "display(pd.DataFrame(mapp, index=[0]).T)\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "ab9c4d02", "metadata": {}, "outputs": [], "source": [ "import re\n", "try:\n", " import cromulent \n", "except:\n", " %pip install cromulent\n", " import cromulent\n", "from cromulent.model import factory\n", "\n", "from lib import linkedart as la\n", "\n", "# list to hold file names for use with jsonld visualisation dropdown\n", "selectOptions = []\n", "selectOptions = [('Please select an artwork', '')]\n", "\n", "\n", "def createObjProp(obj,mapp):\n", " objProp = {}\n", " csv_keys = list(obj.keys())\n", " for key in csv_keys:\n", " for prop in mapp:\n", " if key == mapp[prop]:\n", " \n", " if prop == \"alt_title\":\n", " alt_title = obj[key]\n", " # parse alt_title for dates\n", " \n", " years = re.findall('(\\d{4})', alt_title)\n", " if years[0]:\n", " objProp[\"date_created_earliest\"] = years[0]\n", " if years[1]:\n", " objProp[\"date_created_latest\"] = years[1]\n", " objProp[\"date_created\"] = years[0] + \"-\" + years[1]\n", " else:\n", " objProp[\"date_created\"] = years[0]\n", "\n", " else:\n", " objProp[prop] = obj[key]\n", " \n", " objProp[\"current_owner\"] = {\"name\":\"Rijksmuseum\",\n", " \"location\":\"Amsterdam, Netherlands\",\n", " \"type\": \"http://vocab.getty.edu/aat/300312281\" ,\n", " \"type_label\": \"Museum\"}\n", " return objProp \n", "\n", "\n", "file_list=os.listdir(rijksdir)\n", "for file in file_list:\n", " l\n", " objProp=createObjProp(obj,mapp)\n", " \n", " objProp[\"image_url\"] = obj[\"webImage\"][\"url\"]\n", " objProp[\"creator\"] = [{\"id\": baseURI + \"creator/\" + obj[\"id\"], \"name\": obj[\"principalOrFirstMaker\"], \"role\": \"Artist\"}]\n", " \n", " objProp[\"classification\"] = \"Painting\"\n", " objProp[\"homepage\"] = obj[\"links\"][\"web\"]\n", " \n", " if objProp[\"creator\"] != \"\":\n", " \n", " id = str(objProp[\"id\"])\n", " object_uri = baseURI + id\n", " \n", " # create obj description\n", " objLA = la.createObjDesc(objProp,la.objTypes,object_uri)\n", " filename = id + \".json\"\n", " selectOptions.append( ( objProp[\"title\"] + \" (\" + filename + \")\" , filename))\n", " # write to file \n", " text_file = open(outputdir + filename, \"wt\")\n", " n = text_file.write(factory.toString(objLA, compact=False))\n", " text_file.close()\n", " " ] }, { "cell_type": "code", "execution_count": 9, "id": "150aad72", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4375682b008f43bdaba58574981ca630", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(options=(('Please select an artwork', ''), ('Bergkam aan het Meer van Genève (nl-RP-T-1987-19.json)',…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "try:\n", " import ipywidgets\n", "except:\n", " %pip install ipywidgets\n", " import ipywidgets\n", "\n", "from ipywidgets import Layout, FileUpload \n", "from IPython.display import display, IFrame, HTML, Image\n", "\n", "import os\n", "\n", "try:\n", " import json\n", "except:\n", " %pip install json\n", " import json \n", " \n", "from IPython.core.display import Javascript \n", " \n", "def dropdown_eventhandler(change):\n", " with open('./src/js/visld.js', 'r') as _jscript:\n", " code = _jscript.read() + \"var file = '\" + outputdir + change.new + \"';var selector = '#visnga';visjsonld(file, selector); \"\n", " display(Javascript(code))\n", " \n", " with open( outputdir + \"/\" + change.new) as json_file:\n", " \n", " artwork = json.load(json_file)\n", " if (\"representation\" in artwork):\n", " image = artwork[\"representation\"][0][\"id\"]\n", " display(Javascript(\"document.getElementById('artworknga').src = '\" + image + \"';\"))\n", " else:\n", " display(Javascript(\"document.getElementById('artworknga').src = '';\"))\n", " \n", "\n", "selectObject = ipywidgets.Dropdown(options=selectOptions)\n", "selectObject.observe(dropdown_eventhandler, names='value')\n", "\n", "display(selectObject)" ] }, { "cell_type": "markdown", "id": "92462f9e", "metadata": {}, "source": [ "
\n", "\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }