-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·104 lines (92 loc) · 2.79 KB
/
script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# filename: script.sh
# author: @hugolpz
showHelp() {
# `cat << EOF` This means that cat should stop reading when EOF is detected
cat << EOF
Usage: script.sh [-q,--sparql <arg1>] [-s,--service <arg2>] [f,--format <arg3>]#
Optional arguments:
-q, --query, --sparql
Path to a file with a valid SPARQL query, i.e './query.sparql'
-s, --service
Query service among 'wikidata', 'lingualibre', 'commons'. Default: 'wikidata'.
-f, --format
Output format among 'xml', 'json', 'csv', 'tsv'.
Example: script.sh ./sparql/LL-demo.sparql -s lingualibre -f json
EOF
# EOF is found above and hence cat command stops reading. This is equivalent to echo but much neater when printing out.
}
# Set default values for variables
sparql="./sparql/WD-demo.sparql"
service="wikidata"
format="json"
output="WD-demo.json"
# Parse command line options
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-q|--query|--sparql)
sparql="$2"
shift
;;
-s|--service)
service="$2"
shift
;;
-f|--format)
format="$2"
shift
;;
-h|--help)
showHelp
exit 0
;;
\?) echo "Invalid option. Please use `bash ./script.sh -h`" >&2
;;
esac
shift
done
# ASSIGN OUTPUT FILENAME
output=$(basename "${sparql}" .sparql).${format}
# ASSIGN SERVICE
serviceURL=""
if [ "$service" == "lingualibre" ]; then
serviceURL="https://lingualibre.org/bigdata/sparql"
elif [ "$service" == "commons" ]; then
serviceURL="https://commons-query.wikimedia.org/sparql"
else
serviceURL="https://query.wikidata.org/sparql"
fi
# DEV COMMENTS TO REMOVE
echo "Parameter 'q' is: $sparql"
echo "Parameter 'f' is: $format"
echo "└─Output name is: ${output}"
echo "Parameter 's' is: $service"
echo "└─Service URL is: ${serviceURL}"
# Sparql query
query=$(cat ${sparql})
echo "QUERY= ${query}" | head -n 5
# CURL SPARQL query on Wikidata
response=$(curl -G --data-urlencode query="${query}" ${serviceURL}?format=${format})
echo "RESPONSE: ${response}" | head -n 20
# CLEAN BY FORMAT
if [ "$format" == "json" ]; then
clean=$(echo "${response}" | jq '.results.bindings' | jq 'map(map_values(.value))' | sed -E "s/https?:\/\/.*\/entity\///g" )
echo "CLEANED (JSON): ${clean}" | head -n 20
else
clean=${response}
fi
## IF valid response, THEN print to local file, ELSE error message.
echo "Check if response is valid."
firstline=$(echo "${clean}" | head -n 1)
if [[ ${format} == "json" && ${firstline:0:1} == "[" ]]; then
echo "JSON response is valid, print."
echo "${clean}" > "./data/${output}";
elif [ ${format} != "json" ]; then
echo "${format} response received, print."
echo "${clean}" > "./data/${output}";
else
echo "Response appears invalid, was NOT printed to ./data/${output} ."
fi
echo "* ********************************************* *"
echo " "