2024-07-10 23:20:06 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
"""
|
2024-09-05 16:40:47 +01:00
|
|
|
Loads a PDF document into TrustGraph processing.
|
2024-07-12 15:06:51 +01:00
|
|
|
"""
|
|
|
|
|
|
2024-07-10 23:20:06 +01:00
|
|
|
import pulsar
|
2024-07-25 22:24:14 +01:00
|
|
|
from pulsar.schema import JsonSchema
|
2024-10-02 18:14:29 +01:00
|
|
|
from trustgraph.schema import Document, document_ingest_queue, Metadata
|
2024-07-10 23:20:06 +01:00
|
|
|
import base64
|
|
|
|
|
import hashlib
|
2024-07-12 15:06:51 +01:00
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import time
|
2024-10-22 16:28:54 +01:00
|
|
|
import uuid
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-09-30 16:16:20 +01:00
|
|
|
from trustgraph.log_level import LogLevel
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-10-02 18:14:29 +01:00
|
|
|
default_user = 'trustgraph'
|
|
|
|
|
default_collection = 'default'
|
|
|
|
|
|
2024-10-22 16:28:54 +01:00
|
|
|
class MetadataSource:
|
|
|
|
|
def __init__(self, args):
|
|
|
|
|
self.source = args
|
|
|
|
|
|
|
|
|
|
def id_hash(self, data):
|
|
|
|
|
|
|
|
|
|
# Create a SHA256 hash from the data
|
|
|
|
|
id = hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
|
|
|
|
# Convert into a UUID, 64-byte hash becomes 32-byte UUID
|
|
|
|
|
id = str(uuid.UUID(id[::2]))
|
|
|
|
|
|
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
|
def add(self, id, add):
|
|
|
|
|
|
|
|
|
|
source = self.source
|
|
|
|
|
|
|
|
|
|
add(id, IS_A, DIGITAL_DOCUMENT)
|
|
|
|
|
|
|
|
|
|
if source.name:
|
|
|
|
|
add(id, LABEL, source.name)
|
|
|
|
|
add(id, NAME, source.name)
|
|
|
|
|
|
|
|
|
|
if source.description:
|
|
|
|
|
add(id, DESCRIPTION, source.description)
|
|
|
|
|
|
|
|
|
|
if source.copyright_notice:
|
|
|
|
|
add(id, COPYRIGHT_NOTICE, source.copyright_notice)
|
|
|
|
|
|
|
|
|
|
if source.copyright_holder:
|
|
|
|
|
add(id, COPYRIGHT_HOLDER, source.copyright_holder)
|
|
|
|
|
|
|
|
|
|
if source.copyright_year:
|
|
|
|
|
add(id, COPYRIGHT_YEAR, source.copyright_year)
|
|
|
|
|
|
|
|
|
|
if source.license:
|
|
|
|
|
add(id, LICENSE, source.license)
|
|
|
|
|
|
|
|
|
|
if source.publication_organization:
|
|
|
|
|
|
|
|
|
|
pub_id = self.id_hash(
|
|
|
|
|
source.publication_organization.encode("utf-8")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
add(id, IS_A, PUBLICATION_EVENT)
|
|
|
|
|
|
|
|
|
|
add(id, PUBLICATION, pub_id)
|
|
|
|
|
|
|
|
|
|
if source.publication_organization:
|
|
|
|
|
add(pub_id, PUBLISHED_BY,
|
|
|
|
|
source.publication_organization)
|
|
|
|
|
|
|
|
|
|
if source.publication_description:
|
|
|
|
|
add(pub_id, DESCRIPTION, source.publication_description)
|
|
|
|
|
|
|
|
|
|
if source.publication_date:
|
|
|
|
|
add(pub_id, START_DATE, source.publication_date)
|
|
|
|
|
|
|
|
|
|
if source.publication_date:
|
|
|
|
|
add(pub_id, END_DATE, source.publication_date)
|
|
|
|
|
|
|
|
|
|
if source.url:
|
|
|
|
|
add(id, URL, source.url)
|
|
|
|
|
|
|
|
|
|
if source.identifier:
|
|
|
|
|
add(id, IDENTIFIER, source.identifier)
|
|
|
|
|
|
|
|
|
|
if source.keyword:
|
|
|
|
|
for k in source.keyword:
|
|
|
|
|
add(id, KEYWORD, k)
|
|
|
|
|
|
|
|
|
|
IS_A = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
|
|
|
|
|
LABEL = 'http://www.w3.org/2000/01/rdf-schema#label'
|
|
|
|
|
|
|
|
|
|
DIGITAL_DOCUMENT = 'https://schema.org/DigitalDocument'
|
|
|
|
|
PUBLICATION_EVENT = 'https://schema.org/PublicationEvent'
|
|
|
|
|
|
|
|
|
|
NAME = 'https://schema.org/name'
|
|
|
|
|
DESCRIPTION = 'https://schema.org/description'
|
|
|
|
|
COPYRIGHT_NOTICE = 'https://schema.org/copyrightNotice'
|
|
|
|
|
COPYRIGHT_HOLDER = 'https://schema.org/copyrightHolder'
|
|
|
|
|
COPYRIGHT_YEAR = 'https://schema.org/copyrightYear'
|
|
|
|
|
LICENSE = 'https://schema.org/license'
|
|
|
|
|
PUBLICATION = 'https://schema.org/publication'
|
|
|
|
|
START_DATE = 'https://schema.org/startDate'
|
|
|
|
|
END_DATE = 'https://schema.org/endDate'
|
|
|
|
|
PUBLISHED_BY = 'https://schema.org/publishedBy'
|
|
|
|
|
DATE_PUBLISHED = 'https://schema.org/datePublished'
|
|
|
|
|
PUBLICATION = 'https://schema.org/publication'
|
|
|
|
|
DATE_PUBLISHED = 'https://schema.org/datePublished'
|
|
|
|
|
URL = 'https://schema.org/url'
|
|
|
|
|
IDENTIFIER = 'https://schema.org/identifier'
|
|
|
|
|
KEYWORD = 'https://schema.org/keywords'
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
class Loader:
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
pulsar_host,
|
|
|
|
|
output_queue,
|
2024-10-02 18:14:29 +01:00
|
|
|
user,
|
|
|
|
|
collection,
|
2024-07-12 15:06:51 +01:00
|
|
|
log_level,
|
2024-10-22 16:28:54 +01:00
|
|
|
metadata,
|
2024-07-12 15:06:51 +01:00
|
|
|
):
|
|
|
|
|
|
2024-10-22 16:28:54 +01:00
|
|
|
# self.client = pulsar.Client(
|
|
|
|
|
# pulsar_host,
|
|
|
|
|
# logger=pulsar.ConsoleLogger(log_level.to_pulsar())
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# self.producer = self.client.create_producer(
|
|
|
|
|
# topic=output_queue,
|
|
|
|
|
# schema=JsonSchema(Document),
|
|
|
|
|
# chunking_enabled=True,
|
|
|
|
|
# )
|
|
|
|
|
class Bunch:
|
|
|
|
|
def close(self):
|
|
|
|
|
print("CLOSE")
|
|
|
|
|
self.client = Bunch()
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-10-02 18:14:29 +01:00
|
|
|
self.user = user
|
|
|
|
|
self.collection = collection
|
2024-10-22 16:28:54 +01:00
|
|
|
self.metadata = metadata
|
2024-10-02 18:14:29 +01:00
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
def load(self, files):
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
for file in files:
|
|
|
|
|
self.load_file(file)
|
|
|
|
|
|
|
|
|
|
def load_file(self, file):
|
2024-07-12 15:06:51 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
path = file
|
2024-07-12 15:06:51 +01:00
|
|
|
data = open(path, "rb").read()
|
|
|
|
|
|
2024-10-16 23:30:51 +01:00
|
|
|
# Create a SHA256 hash from the data
|
|
|
|
|
id = hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
|
|
|
|
# Convert into a UUID, 64-byte hash becomes 32-byte UUID
|
|
|
|
|
id = str(uuid.UUID(id[::2]))
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-10-22 16:28:54 +01:00
|
|
|
triples = []
|
|
|
|
|
|
|
|
|
|
def add(s, p, o):
|
|
|
|
|
triples.append((s, p, o))
|
|
|
|
|
|
|
|
|
|
self.metadata.add(id, add)
|
|
|
|
|
|
|
|
|
|
for t in triples:
|
|
|
|
|
print(t)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
r = Document(
|
2024-10-02 18:14:29 +01:00
|
|
|
metadata=Metadata(
|
2024-07-12 15:06:51 +01:00
|
|
|
source=path,
|
|
|
|
|
title=path,
|
|
|
|
|
id=id,
|
2024-10-02 18:14:29 +01:00
|
|
|
user=self.user,
|
|
|
|
|
collection=self.collection,
|
2024-07-12 15:06:51 +01:00
|
|
|
),
|
|
|
|
|
data=base64.b64encode(data),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.producer.send(r)
|
|
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
print(f"{file}: Loaded successfully.")
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
except Exception as e:
|
2024-10-01 19:34:35 +01:00
|
|
|
print(f"{file}: Failed: {str(e)}", flush=True)
|
2024-07-12 15:06:51 +01:00
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
self.client.close()
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
prog='loader',
|
|
|
|
|
description=__doc__,
|
2024-07-10 23:20:06 +01:00
|
|
|
)
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
default_pulsar_host = os.getenv("PULSAR_HOST", 'pulsar://localhost:6650')
|
2024-07-25 21:43:55 +01:00
|
|
|
default_output_queue = document_ingest_queue
|
2024-07-12 15:06:51 +01:00
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-p', '--pulsar-host',
|
|
|
|
|
default=default_pulsar_host,
|
|
|
|
|
help=f'Pulsar host (default: {default_pulsar_host})',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-o', '--output-queue',
|
|
|
|
|
default=default_output_queue,
|
|
|
|
|
help=f'Output queue (default: {default_output_queue})'
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-02 18:14:29 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-u', '--user',
|
|
|
|
|
default=default_user,
|
|
|
|
|
help=f'User ID (default: {default_user})'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-c', '--collection',
|
|
|
|
|
default=default_collection,
|
|
|
|
|
help=f'Collection ID (default: {default_collection})'
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-16 23:30:51 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--name', help=f'Document name'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--description', help=f'Document description'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--copyright-notice', help=f'Copyright notice'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--copyright-holder', help=f'Copyright holder'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--copyright-year', help=f'Copyright year'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2024-10-22 16:28:54 +01:00
|
|
|
'--license', help=f'Copyright license'
|
2024-10-16 23:30:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--publication-organization', help=f'Publication organization'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--publication-description', help=f'Publication description'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--publication-date', help=f'Publication date'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--url', help=f'Document URL'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2024-10-22 16:28:54 +01:00
|
|
|
'--keyword', nargs='+', help=f'Keyword'
|
2024-10-16 23:30:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2024-10-22 16:28:54 +01:00
|
|
|
'--identifier', '--id', help=f'Document ID'
|
2024-10-16 23:30:51 +01:00
|
|
|
)
|
|
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-l', '--log-level',
|
|
|
|
|
type=LogLevel,
|
|
|
|
|
default=LogLevel.ERROR,
|
|
|
|
|
choices=list(LogLevel),
|
|
|
|
|
help=f'Output queue (default: info)'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2024-10-01 19:34:35 +01:00
|
|
|
'files', nargs='+',
|
2024-07-12 15:06:51 +01:00
|
|
|
help=f'File to load'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
|
|
try:
|
2024-10-01 19:34:35 +01:00
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
p = Loader(
|
|
|
|
|
pulsar_host=args.pulsar_host,
|
|
|
|
|
output_queue=args.output_queue,
|
2024-10-02 18:14:29 +01:00
|
|
|
user=args.user,
|
|
|
|
|
collection=args.collection,
|
2024-07-12 15:06:51 +01:00
|
|
|
log_level=args.log_level,
|
2024-10-22 16:28:54 +01:00
|
|
|
metadata=MetadataSource(args),
|
2024-07-12 15:06:51 +01:00
|
|
|
)
|
|
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
p.load(args.files)
|
2024-07-12 15:06:51 +01:00
|
|
|
|
2024-10-01 19:34:35 +01:00
|
|
|
print("All done.")
|
2024-07-12 15:06:51 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print("Exception:", e, flush=True)
|
|
|
|
|
print("Will retry...", flush=True)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
time.sleep(10)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-12 15:06:51 +01:00
|
|
|
main()
|
2024-07-10 23:20:06 +01:00
|
|
|
|