|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
Download UD metadata for the different releases from the lindat clarin-dspace |
|
|
repository into `./templates/`: |
|
|
- BibTeX "Howto Cite" |
|
|
(use the item's own data from the webpage) |
|
|
- dc.description |
|
|
(use the clarin-dspace API to retrieve the item's metadata) |
|
|
|
|
|
We assume a clarin-dspace infrastructure with: |
|
|
- ".../repository/server/api/core/refbox/citations?type=bibtex&handle=" |
|
|
returning a bibtex entry for the given hdl |
|
|
- ".../repository/server/api/core/items/" as the API endpoint (core/items) |
|
|
""" |
|
|
|
|
|
import json |
|
|
import re |
|
|
import argparse |
|
|
import logging |
|
|
from pathlib import Path |
|
|
from urllib.error import URLError |
|
|
from urllib.request import urlopen |
|
|
from urllib.parse import urlparse |
|
|
|
|
|
|
|
|
citation_url_prefix = "https://lindat.mff.cuni.cz/repository/server/api/core/refbox/citations?type=bibtex&handle=" |
|
|
description_url_prefix = "https://lindat.mff.cuni.cz/repository/server/api/core/items/" |
|
|
handle_url_prefix = "http://hdl.handle.net/" |
|
|
handle_redirect_url_prefix = "https://lindat.mff.cuni.cz/repository/items/" |
|
|
|
|
|
|
|
|
outfile_pathprefix = "./etc/" |
|
|
citation_outfile_name = "citation-{rev}" |
|
|
description_outfile_name = "description-{rev}" |
|
|
|
|
|
|
|
|
url_postfixes = { |
|
|
|
|
|
|
|
|
"2.17": "11234/1-6036", |
|
|
"2.16": "11234/1-5901", |
|
|
"2.15": "11234/1-5787", |
|
|
"2.14": "11234/1-5502", |
|
|
"2.13": "11234/1-5287", |
|
|
"2.12": "11234/1-5150", |
|
|
"2.11": "11234/1-4923", |
|
|
"2.10": "11234/1-4758", |
|
|
"2.9": "11234/1-4611", |
|
|
"2.8": "11234/1-3687", |
|
|
"2.7": "11234/1-3424", |
|
|
} |
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__, |
|
|
formatter_class=argparse.RawDescriptionHelpFormatter) |
|
|
parser.add_argument('-o', '--override', action='store_true', |
|
|
help='override output file if it already exists') |
|
|
parser.add_argument('-v', '--verbose', action='count', default=0, |
|
|
help='increase verbosity level') |
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
|
level = max(logging.DEBUG, logging.INFO - args.verbose * 10), |
|
|
format='%(asctime)s [%(levelname)s] %(message)s', |
|
|
datefmt='%Y-%m-%d %H:%M:%S' |
|
|
) |
|
|
|
|
|
|
|
|
for rev,handle in url_postfixes.items(): |
|
|
|
|
|
try: |
|
|
|
|
|
citation_url = citation_url_prefix + handle |
|
|
logging.debug(f"Using Citation URL: {citation_url}") |
|
|
|
|
|
|
|
|
with urlopen(citation_url) as response: |
|
|
|
|
|
|
|
|
data = json.loads(response.read().decode()) |
|
|
|
|
|
|
|
|
metadata = data.get("metadata") |
|
|
|
|
|
|
|
|
if metadata: |
|
|
|
|
|
|
|
|
metadata = "\n".join([re.sub('^( )+', ' ', line) for line in |
|
|
metadata.split("\n")]) |
|
|
|
|
|
|
|
|
output_fn = f"{outfile_pathprefix}"+citation_outfile_name.format(rev=rev) |
|
|
if args.override or not Path(output_fn).exists(): |
|
|
with open(output_fn, "w") as fh: |
|
|
fh.write(metadata + "\n") |
|
|
logging.info(f"Successfully downloaded citation from {citation_url} and written to {output_fn}.") |
|
|
else: |
|
|
logging.info(f"Output {output_fn} already exists: Not overriding.") |
|
|
|
|
|
|
|
|
|
|
|
handle_url = handle_url_prefix + handle |
|
|
logging.debug(f"Using handle URL: {handle_url}") |
|
|
|
|
|
with urlopen(handle_url) as response: |
|
|
if response.url.startswith(handle_redirect_url_prefix): |
|
|
itemid = (urlparse(response.url)).path.rsplit("/", 1)[-1] |
|
|
|
|
|
description_url = description_url_prefix + itemid |
|
|
with urlopen(description_url) as response: |
|
|
data = json.loads(response.read().decode()) |
|
|
description = data["metadata"]["dc.description"][0]["value"] |
|
|
if description: |
|
|
|
|
|
|
|
|
output_fn = f"{outfile_pathprefix}"+description_outfile_name.format(rev=rev) |
|
|
if args.override or not Path(output_fn).exists(): |
|
|
with open(output_fn, "w") as fh: |
|
|
fh.write(description+ "\n") |
|
|
logging.info(f"Successfully downloaded description from {description_url} and written to {output_fn}.") |
|
|
else: |
|
|
logging.info(f"Output {output_fn} already exists: Not overriding.") |
|
|
|
|
|
except URLError as e: |
|
|
logging.error(f"Error downloading: {e}") |
|
|
except json.JSONDecodeError as e: |
|
|
logging.error(f"Error decoding JSON: {e}") |
|
|
|