{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Studiando la documentazione di Virtuoso, si vede che ci sono potenzialmente un sacco di metodi, più o meno complicati, per caricare file RDF o singole triple, sia da interfaccia che da codice; vedi [qui](http://vos.openlinksw.com/owiki/wiki/VOS/VirtRDFInsert).\n", "Quello che mi pareva più accessibile e su cui mi sono concentrato per ora usa l'endpoint SPARQL. In [questa pagina](http://vos.openlinksw.com/owiki/wiki/VOS/VirtGraphProtocolCURLExamples) sono riportati esempi per farlo tramite il comando *curl*, ad esempio:\n", "\n", "**> curl --digest --user USER:PWD (--verbose) --url \"http://VIRTUOSO_URL:PORT/sparql-graph-crud-auth?graph-uri=FULL_PATH_TO_THE_GRAPH\" -T FILE_TO_UPLOAD**\n", "\n", "Una volta verificato che funzionava, si è trattato di replicare il comando in Python. Per farlo ho usato il modulo *requests*. L'unico punto delicato è stato riprodurre l'**autenticazione**, che a quanto pare richiede il metodo **\"Digest\"** - ho dovuto cercare un po' sul web per trovare l'opzione per riprodurla." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Segue il codice per fare l'upload del file di prova che mi ha passato Federica." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# SETUP\n", "\n", "import requests\n", "\n", "file_path = '/home/kora/Desktop/OVI_Data_local/temp/' # CAMBIARE QUESTO!\n", "file_name = 'event_exchange_letters.ttl' # Cambiare se si desidera" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Created\n" ] } ], "source": [ "# UPLOAD\n", "\n", "file_object = open(file_path + file_name, 'rb')\n", "user = 'dba'\n", "pwd = 'restore2021'\n", "virtuoso_url = 'http://dev.restore.ovi.cnr.it:8890'\n", "graph_uri = 'http://dev.restore.ovi.cnr.it:8890/PROVE_IMPORT/pr10' # Cambiare se si desidera\n", "\n", "resp = requests.post(virtuoso_url + '/sparql-graph-crud-auth?graph-uri=' + graph_uri,\n", " data = file_object,\n", " auth = requests.auth.HTTPDigestAuth(user, pwd))\n", "\n", "print(resp)\n", "print(resp.reason)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Per riferimento" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '7163', 'Authorization': 'Digest username=\"dba\", realm=\"SPARQL\", nonce=\"d64d97f8b4666b5bc6e591370c9a2e10\", uri=\"/sparql-graph-crud-auth?graph-uri=http://dev.restore.ovi.cnr.it:8890/PROVE_IMPORT/pr10\", response=\"032a4bc7088a6b3e7c015f76b12635d0\", opaque=\"5ebe2294ecd0e0f08eab7690d2a6ee69\", algorithm=\"MD5\", qop=\"auth\", nc=00000001, cnonce=\"e2f7a32a9a83fd67\"'}\n" ] } ], "source": [ "print(resp.request.headers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Per completezza: prova di ri-download delle info uploadate. Notare che l'url da chiamare è *leggermente diverso*: la substring **crud-auth** diventa **crud**." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "@prefix ns0:\t .\n", "@prefix ns1:\t .\n", "@prefix ns2:\t .\n", "ns1:ASPO00000102\tns0:P25i_moved_by\tns2:EL1 .\n", "@prefix rdf:\t .\n", "ns2:EL1\trdf:type\tns0:EL1_Exchange_Letters .\n", "@prefix rdfs:\t .\n", "ns2:EL1\trdfs:label\t\"Carteggio: CONSIGLI DUCCIO E GIAME a DATINI FRANCESCO DI MARCO E COMP.\" .\n", "ns2:EL2\trdf:type\tns0:EL2_Send_Letter ;\n", "\trdfs:label\t\"Invio\" .\n", "@prefix ns5:\t .\n", "ns2:EL2\tns0:P4_has_time-span\tns5:E52 ;\n", "\tns0:P27_moved_from\tns5:E53 ;\n", "\trdf:subClassOf\tns2:EL1 ;\n", "\tns0:P01_has_domain\tns5:PC14 .\n", "ns5:E53\trdf:type\tns0:E53_Place ;\n", "\trdfs:label\t\"AIGUES-MORTES\" .\n", "ns2:EL3\trdf:type\tns0:EL3_Receive_Letter ;\n", "\trdfs:label\t\"Ricezione\" .\n", "@prefix ns6:\t .\n", "ns2:EL3\tns0:P4_has_time-span\tns6:E52 ;\n", "\tns0:P26_moved_to\tns6:E53 ;\n", "\trdf:subClassOf\tns2:EL1 ;\n", "\tns0:P01_has_domain\tns6:PC14 .\n", "ns6:E53\trdf:type\tns0:E53_Place ;\n", "\trdfs:label\t\"AVIGNONE\" .\n", "@prefix ns7:\t .\n", "ns7:E21\trdf:type\tns0:E21_Person ;\n", "\trdfs:label\t\"CONSIGLI DUCCIO E GIAME\" .\n", "ns5:E52\trdf:type\tns0:E52_Time-Span ;\n", "\trdfs:label\t\"data di partenza: 09/09/1384\" .\n", "ns5:E55\trdfs:label\t\"Mittente\" .\n", "ns5:PC14\trdf:type\tns0:PC14_carried_out_by ;\n", "\trdfs:label\t\"CONSIGLI DUCCIO E GIAME nel ruolo di Mittente\" ;\n", "\tns0:P02_has_range\tns7:E21 ;\n", "\t\tns5:E55 .\n", "ns6:E52\trdf:type\tns0:E52_Time-Span ;\n", "\trdfs:label\t\"data di arrivo: 13/09/1384\" .\n", "ns6:PC14\trdf:type\tns0:PC14_carried_out_by ;\n", "\trdfs:label\t\"DATINI FRANCESCO DI MARCO E COMP. nel ruolo di Mittente\" .\n", "@prefix ns8:\t .\n", "ns6:PC14\tns0:P02_has_range\tns8:E21 ;\n", "\t\tns6:E55 .\n", "ns8:E21\trdf:type\tns0:E21_Person ;\n", "\trdfs:label\t\"DATINI FRANCESCO DI MARCO E COMP.\" .\n", "ns6:E55\trdfs:label\t\"Destinatario\" .\n", "ns0:EL1_Exchange_Letters\trdfs:label\t\"Exchange_Letters\" .\n" ] } ], "source": [ "resp2 = requests.get(virtuoso_url + '/sparql-graph-crud?graph-uri=' + graph_uri)\n", "\n", "print(resp2.text)" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }