Created
April 17, 2020 15:12
-
-
Save jum-s/4598c9a740e8cf5a534a43a7d5ddce57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Update series of some works | |
| # Find works serie parts and replace results serie claim value with a new serie uri | |
| # | |
| set -o errexit | |
| set -o nounset | |
| host="http://localhost:3006" | |
| username="adamsberg" | |
| password="12345678" | |
| works=$1 | |
| currentSerie=$2 | |
| newSerie=$3 | |
| usage() { | |
| echo -e "Usage: $0 current new" | |
| echo -e "\tcurrent:\t inventaire uri of the current serie (inv:71b4f804c097431886e9ea2d343e3759 or wd:Q718449 for example)" | |
| echo -e "\tnew:\t inventaire uri of the new serie (inv:71b4f804c097431886e9ea2d343e3759 or wd:Q718449 for example)" | |
| exit 1 | |
| } | |
| auth() { | |
| result=$(curl --silent -L --request POST ${host}/api/auth?action=login \ | |
| --cookie-jar cookie.txt \ | |
| --header 'Content-Type: application/json' \ | |
| --data "{ | |
| \"username\": \"${username}\", | |
| \"password\": \"${password}\" | |
| }") | |
| [ "${result}" = "{\"ok\":true}" ] || (echo "[ERROR] Authentication failed" && exit 2) | |
| } | |
| getWorkOfSerie() { | |
| works=$(curl --silent ${host}/api/entities?action=serie-parts\&uri=${currentSerie} \ | |
| --cookie cookie.txt \ | |
| --header 'Content-Type: application/json') | |
| works=$(echo ${works} | jq .parts[].uri) | |
| [ -n "${works}" ] || (echo "No work were found for this serie" && exit 3) | |
| } | |
| updateClaim() { | |
| for work in ${works} | |
| do | |
| data='{ | |
| "uri":%s, | |
| "property":"wdt:P179", | |
| "old-value":"%s", | |
| "new-value":"%s" | |
| }' | |
| res=$(curl -s -XPUT "${host}/api/entities?action=update-claim" \ | |
| --cookie cookie.txt \ | |
| --header 'Content-Type: application/json' \ | |
| -d "$(printf "$data" "$work" "$currentSerie" "$newSerie")") | |
| echo $res | |
| done | |
| } | |
| main() { | |
| auth | |
| getWorkOfSerie | |
| updateClaim | |
| rm cookie.txt | |
| exit 0 | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment