]> Pierre Choffet | Git repositories - wdef_tools.git/blob - scripts/rdf.sh
Add script to compare with Wikidata’s RDF
[wdef_tools.git] / scripts / rdf.sh
1 #!/bin/bash
2
3 # rdf.sh - Set of Bash functions to work with wdef files.
4 # Copyright (C) 2022 Pierre Choffet
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of version 3 of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 set -euo pipefail
19
20 readonly RDFS_CACHE_DIR=${CACHE_DIR:-"${HOME}/.cache/wdef_tools/rdfs/"}
21
22 # Get RDF and return a path to the result into cache dir
23 # Parameter:
24 # $1: Element QID
25 # Output:
26 # Path to the file containing the RDF, in cache dir
27 function cacheRDF() {
28 local -r element_qid="${1}"
29
30 local -r rdf_url="https://www.wikidata.org/wiki/Special:EntityData/${element_qid}.rdf"
31 local -r rdf_path="${RDFS_CACHE_DIR}${element_qid}.xml"
32
33 # Create cache dir
34 mkdir -p "${RDFS_CACHE_DIR}"
35
36 curl "${rdf_url}" > "${rdf_path}"
37
38 echo "${rdf_path}"
39 }
40
41 # Ensure RDF cache is not older than given age
42 # Parameter:
43 # $1: Element QID
44 # $2: Max age (in minutes)
45 # Output:
46 # Path to the file containing the RDF, in cache dir
47 function cacheRDFMaxAge() {
48 local -r element_qid="${1}"
49 local -r max_age="${2}"
50
51 local -r rdf_path="${RDFS_CACHE_DIR}${element_qid}.xml"
52
53 if [ ! -f "${rdf_path}" ]||[[ $(find "${rdf_path}" -mmin "+${max_age}") ]]
54 then
55 cacheRDF "${element_qid}"
56 else
57 echo "${rdf_path}"
58 fi
59 }