#!/bin/bash # rdf.sh - Set of Bash functions to work with wdef files. # Copyright (C) 2022 Pierre Choffet # # This program is free software: you can redistribute it and/or modify # it under the terms of version 3 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . set -euo pipefail readonly RDFS_CACHE_DIR=${CACHE_DIR:-"${HOME}/.cache/wdef_tools/rdfs/"} # Get RDF and return a path to the result into cache dir # Parameter: # $1: Element QID # Output: # Path to the file containing the RDF, in cache dir function cacheRDF() { local -r element_qid="${1}" local -r rdf_url="https://www.wikidata.org/wiki/Special:EntityData/${element_qid}.rdf" local -r rdf_path="${RDFS_CACHE_DIR}${element_qid}.xml" # Create cache dir mkdir -p "${RDFS_CACHE_DIR}" curl "${rdf_url}" > "${rdf_path}" echo "${rdf_path}" } # Ensure RDF cache is not older than given age # Parameter: # $1: Element QID # $2: Max age (in minutes) # Output: # Path to the file containing the RDF, in cache dir function cacheRDFMaxAge() { local -r element_qid="${1}" local -r max_age="${2}" local -r rdf_path="${RDFS_CACHE_DIR}${element_qid}.xml" if [ ! -f "${rdf_path}" ]||[[ $(find "${rdf_path}" -mmin "+${max_age}") ]] then cacheRDF "${element_qid}" else echo "${rdf_path}" fi }