Skip to content

Instantly share code, notes, and snippets.

@jermnelson
Created August 4, 2020 16:51
Show Gist options
  • Save jermnelson/3b4afd7d8957a5525bfa3c383777f9e3 to your computer and use it in GitHub Desktop.
Save jermnelson/3b4afd7d8957a5525bfa3c383777f9e3 to your computer and use it in GitHub Desktop.
Trellis Interactions for University of Washington
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import rdflib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# University of Washington - Trellis Operations\n",
"## 04 August 2020\n",
"\n",
"Documentation:\n",
"- https://github.com/LD4P/sinopia_server/wiki/Draft-Notes-for-Sinopia-Server-API-Spec\n",
"- https://github.com/trellis-ldp/trellis/wiki/Resource-Management"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"resource_one = requests.get('https://trellis.development.sinopia.io/repository/washington/030e18ca-fd7a-48a3-8297-a00619cb2c53')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"@prefix schema: <http://schema.org/> .\n",
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
"@prefix memento: <http://mementoweb.org/ns#> .\n",
"@prefix skos: <http://www.w3.org/2004/02/skos/core#> .\n",
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
"@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n",
"@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .\n",
"@prefix dc11: <http://purl.org/dc/elements/1.1/> .\n",
"@prefix as: <https://www.w3.org/ns/activitystreams#> .\n",
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
"@prefix ldp: <http://www.w3.org/ns/ldp#> .\n",
"@prefix time: <http://www.w3.org/2006/time#> .\n",
"@prefix prov: <http://www.w3.org/ns/prov#> .\n",
"@prefix dc: <http://purl.org/dc/terms/> .\n",
"\n",
"_:b0 rdf:type <http://id.loc.gov/ontologies/bibframe/Status> ;\n",
" <http://id.loc.gov/ontologies/bibframe/code> \"n\"@en .\n",
"\n",
"_:b1 rdf:type <http://id.loc.gov/ontologies/bibframe/AdminMetadata> ;\n",
" <http://id.loc.gov/ontologies/bibframe/status> _:b0 ;\n",
" <http://id.loc.gov/ontologies/bibframe/source> <https://id.loc.gov/vocabulary/organizations/wau> ;\n",
" <http://id.loc.gov/ontologies/bibframe/descriptionLanguage> <https://id.loc.gov/vocabulary/languages/eng> ;\n",
" <http://id.loc.gov/ontologies/bibframe/descriptionConventions> <https://id.loc.gov/vocabulary/descriptionConventions/rda> ;\n",
" <http://id.loc.gov/ontologies/bibframe/creationDate> \"2019-10-29\"@en ;\n",
" <http://id.loc.gov/ontologies/bflc/encodingLevel> <https://id.loc.gov/vocabulary/menclvl/f> ;\n",
" <http://id.loc.gov/ontologies/bflc/catalogerID> \"cec23\"@en .\n",
"\n",
"<> <http://sinopia.io/vocabulary/hasResourceTemplate> \"WAU:RT:RDA:Item:monograph\" ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#hasLcClassificationPartB> \".A1 2012\"@en ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#heldBy> <https://id.loc.gov/vocabulary/organizations/wau> ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#hasPhysicalLocation> \"Yragui bookshelf\"@en ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#hasPhysicalSubLocation> \"American fiction\"@en ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#hasLcClassificationPartA> \"PS1305\"@en ;\n",
" <http://rdaregistry.info/Elements/i/P40047> \"Also snacks for Mortimer the dog.\"@en ;\n",
" <http://id.loc.gov/ontologies/bibframe/adminMetadata> _:b1 ;\n",
" <http://rdaregistry.info/Elements/i/P40050> \"Goodwill\"@en ;\n",
" <https://doi.org/10.6069/uwlib.55.d.4#hasBarcode> \"123456789\"@en ;\n",
" <http://rdaregistry.info/Elements/i/P40049> <https://trellis.development.sinopia.io/repository/washington/b784eb4c-d373-420e-a236-5e9f4cef63f6> ;\n",
" <http://rdaregistry.info/Elements/i/P40047> \"Keys to Yragui house required\"@en ;\n",
" rdf:type <http://rdaregistry.info/Elements/c/C10003> .\n",
"\n"
]
}
],
"source": [
"print(resource_one.text)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"resource_one = requests.post('https://trellis.development.sinopia.io/repository/washington/')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"200"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"resource_one.status_code"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"@prefix schema: <http://schema.org/> .\n",
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
"@prefix memento: <http://mementoweb.org/ns#> .\n",
"@prefix skos: <http://www.w3.org/2004/02/skos/core#> .\n",
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
"@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n",
"@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .\n",
"@prefix dc11: <http://purl.org/dc/elements/1.1/> .\n",
"@prefix as: <https://www.w3.org/ns/activitystreams#> .\n",
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
"@prefix ldp: <http://www.w3.org/ns/ldp#> .\n",
"@prefix time: <http://www.w3.org/2006/time#> .\n",
"@prefix prov: <http://www.w3.org/ns/prov#> .\n",
"@prefix dc: <http://purl.org/dc/terms/> .\n",
"\n",
"<https://trellis.development.sinopia.io/repository/washington>\n",
" rdfs:label \"Sinopia University of Washington Group Container\" ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/92b81b19-fd98-4395-8c96-2fb2f9657cc2> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/5d3de424-4ed0-4d4e-b13e-e72cc548e6b6> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/b4f8451d-069d-4294-80aa-277dc7bcd006> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/0dc02840-7679-43e6-b912-3973a7cbcb01> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/a20ab8a5-397d-48db-a0a2-6a7bfe04d8f6> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/7438f178-80ba-4d45-842e-2eb20b259c8e> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/9931fe52-32d7-4ee9-bcfe-a845c6a40d5d> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/1fbaa398-ea35-42e8-9d76-3f81b80375d0> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/37ec8045-aa1a-46c2-9068-db7579b0c784> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/133cae2e-93e9-4f0d-9369-225514eff557> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/a6934df9-7158-46d3-a50f-993135ace180> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/4c10775c-4659-4cd3-ba95-16dff4277571> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/b05edb8e-6513-403d-bf37-e5e5d2ddf71d> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/911457e8-bdbd-46a1-bbf7-9d2c0986b6b0> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/06ee1b9e-f8ab-444b-974a-2fa193e3f6e9> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/9c5bd9f5-1804-45bd-99ed-b6e3774c896e> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/b784eb4c-d373-420e-a236-5e9f4cef63f6> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/12f50756-8643-465c-9be4-e18eb00da8c6> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/0c851197-feda-49da-b015-b6bbb0901897> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/2d32e2a1-1b6d-4ea8-b37f-0833eae18b8d> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/52e1fc26-021c-4053-8665-312917965c7f> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/aa81dfcc-0bc3-4291-84af-073999a31129> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/03bd4a2b-a130-4adb-855f-aff0b316c946> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/373bb373-6e3d-41a6-988e-937ac254df3a> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/8a9825ca-649c-49e7-9b8d-cb18fbf76974> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/763039b8-a7f8-410e-ac1d-e3282b9f40da> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/2a0e6ddc-7223-41a2-a4ec-aa270d977590> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/e09c00ef-852c-44b8-ae3d-eaa30a2d6e9b> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/093b60df-de48-44f0-920f-fdd3415abc52> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/12324719-29d4-4a1a-a36f-d20d0323448c> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/67887da9-185d-4a5e-889b-340135c34b6c> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/de8abfb9-1ef6-4d00-af64-14500684d6c5> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/03c8c0bb-c0d4-413a-a6eb-2dda81de34ed> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/1cab2747-a655-4bf0-aa79-93c153f0cef5> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/1e812fdd-bca8-4e94-8846-e9405b8196cc> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/8f386cea-666e-4f75-8447-d503e8a2aa15> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/014ba9ff-c2a0-4648-9a94-9a4cc8b5a857> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/14088f23-620e-45d9-95f9-ec767b2a9565> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/d9c2d78d-1303-4abe-a81f-4852c53aa291> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/c7412a5b-d204-4027-98bb-2776bf3c1fd1> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/83c3f1cd-eb47-4f32-8a5e-16649e9011dc> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/aef297f3-41c1-45e6-a751-396f77f6d01a> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/1e8c033b-a877-4866-9b68-27f474c19e32> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/12587dc2-9b3b-48d9-944f-2638b603932f> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/b454f5d8-d7b6-4e1a-bbcd-291d2bc304f6> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/aa30f09f-c622-4240-ba03-f9d0192c015e> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/4442f8dd-68ab-4c60-9934-9139d4d41f4a> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/3f82acb1-a002-42ca-9435-568ae35e3420> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/85c75c2e-3f05-4902-a21c-e57e43ee0edf> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/22e3629e-6f64-4a25-b2ae-869bd1247db2> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/cc0f7aea-dc8a-41e2-a9a1-63b4776b0a7f> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/7c02e581-dfce-461d-9a2c-968ff0aed075> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/cfd50c70-1945-4519-95d9-81a2e68a8765> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/b663903e-f3a9-42d5-813c-45292bd80124> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/a3fecf34-b58f-4cf8-806f-b9c303748c1c> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/3b59fc74-2013-4bc4-9ebb-c81b9446c8b0> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/650cacb0-9235-497b-a00b-fbef84f257e5> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/e7292ff1-9780-423a-8d52-9d3a9132d832> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/a3f7dc74-1306-4521-a90f-2ff3a12a7307> ;\n",
" ldp:contains <https://trellis.development.sinopia.io/repository/washington/030e18ca-fd7a-48a3-8297-a00619cb2c53> .\n",
"\n"
]
}
],
"source": [
"print(resource_one.text)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=Nbde65c5078504aebb5295d69f72b6caf (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"headers = {\n",
" \"Authorization\": f\"Bearer {jwt_token}\",\n",
" \"Content-Type\": \"application/ld+json\",\n",
" \"Link\": '<http://www.w3.org/ns/ldp#RDFSource>; rel=\"type\"'\n",
"}\n",
"\n",
"result2 = requests.get('https://trellis.development.sinopia.io/repository/washington/a3f7dc74-1306-4521-a90f-2ff3a12a7307')\n",
"graph = rdflib.Graph()\n",
"graph.parse(data=result2.text, format='turtle')\n",
"graph.add((rdflib.URIRef(\"http://example.com/\"), rdflib.RDF.type, rdflib.URIRef(\"http://schema.org/Book\")))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(graph)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"@prefix ns1: <http://id.loc.gov/ontologies/bibframe/> .\n",
"@prefix ns2: <http://id.loc.gov/ontologies/bflc/> .\n",
"@prefix ns3: <http://sinopia.io/vocabulary/> .\n",
"@prefix schema: <http://schema.org/> .\n",
"\n",
"<file:///Users/jpnelson/2020/ld4p/sinopia_acl/> a <http://rdaregistry.info/Elements/c/C10001> ;\n",
" ns1:adminMetadata [ a ns1:AdminMetadata ;\n",
" ns2:catalogerID \"123456\"@en ;\n",
" ns2:encodingLevel <https://id.loc.gov/vocabulary/menclvl/f> ;\n",
" ns1:creationDate \"123456\"@en ;\n",
" ns1:descriptionConventions <https://id.loc.gov/vocabulary/descriptionConventions/rda> ;\n",
" ns1:descriptionLanguage <https://id.loc.gov/vocabulary/languages/eng> ;\n",
" ns1:source <https://id.loc.gov/vocabulary/organizations/wau> ;\n",
" ns1:status [ a ns1:Status ;\n",
" ns1:code \"n\"@en ] ] ;\n",
" ns3:hasResourceTemplate \"WAU:RT:RDA:Work:monograph\" .\n",
"\n",
"<http://example.com/> a schema:Book .\n",
"\n",
"\n"
]
}
],
"source": [
"print(graph.serialize(format='turtle').decode())"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"# Updates existing Trellis URI\n",
"result = requests.put('https://trellis.development.sinopia.io/repository/washington/a3f7dc74-1306-4521-a90f-2ff3a12a7307',\n",
" data=graph.serialize(format='json-ld'),\n",
" headers=headers)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"add_result = requests.post('https://trellis.development.sinopia.io/repository/washington',\n",
" data=graph.serialize(format='json-ld'),\n",
" headers=headers)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"201"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_result.status_code"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment