Created
July 16, 2019 09:39
-
-
Save clochix/3f03ae72ff3e249cf804a4f01fe81d0d to your computer and use it in GitHub Desktop.
CozyExport: sample script to export data from Cozy
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
#!/usr/bin/env bash | |
######### | |
# | |
# Export every documents from a datatype | |
# | |
# Syntax: cozyexport claude.mycozy.cloud io.cozy.contacts | |
# | |
######## | |
cozyexport () | |
( | |
server='' | |
token='' | |
verbose='-s' | |
# Step 1: register app and get token | |
server=$(echo "$1" | sed 's/https:\/\///g') | |
if [ "$server" = "" ]; then | |
logerr "Usage: ${FUNCNAME[0]} URL [datatype]" | |
logerr " default datatype: io.cozy.files" | |
return 1 | |
fi | |
if [ "$#" -eq 1 ]; then | |
datatype="io.cozy.files" | |
else | |
datatype="${2}" | |
fi | |
scope="${datatype}:GET" | |
echo "Getting token for $server with scope $scope" | |
registration=$(curl -s -X POST -H "Host: ${server}" -H "Content-Type: application/json" -H "Accept: application/json" -d '{"redirect_uris": ["http://localhost:8080"],"client_name": "cozycli","software_id": "cozycli"}' https://${server}/auth/register | jq ".server=\"${server}\"") | |
state="$(cat /proc/sys/kernel/random/uuid)" | |
clientid="$(echo "$registration" | jq -r '.client_id')" | |
clientsecret="$(echo "$registration" | jq -r '.client_secret')" | |
registrationtoken="$(echo "$registration" | jq -r '.registration_access_token')" | |
url="$(curl -s -L -w "%{url_effective}" -o /dev/null "https://${server}/auth/authorize?client_id={$clientid}&response_type=code&scope=${scope}&state=${state}&redirect_uri=http%3A%2F%2Flocalhost:8080")" | |
echo "Open this URL in your browser" | |
echo "$url" | |
answer=$(mktemp) | |
chmod 600 "$answer" | |
nc -l -p 8080 -q 1 -c 'while read -r request remaining;do echo $request $remaining >> '${answer}';if [ "$remaining" = "" ];then break;fi;done;echo "HTTP/1.1 200 OK\n\nOK"'; | |
IFS=";" read -r code state2<<< $(sed -E "s/^.*&code=([^&]*)&state=(.*) .*$/\1;\2/" "$answer") | |
rm -f $answer | |
if [ "$state2" != "$state" ]; then | |
echo "Wrong state! Expected ${state}, got ${state2}" | |
fi | |
token=$(curl -s -X POST -H "Host: ${server}" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "grant_type=authorization_code&code=${code}&client_id=${clientid}&client_secret=${clientsecret}" https://${server}/auth/access_token | jq -r '.access_token') | |
# Step 2: export the data | |
res=$(curl $verbose -H "Origin: https://${server}" -H "Authorization: Bearer ${token}" -H "Accept: application/json" "https://${server}/data/${datatype}/_all_docs?include_docs=true") | |
echo "$res" | |
# Step 3: revoke the tocken | |
res=$(curl $verbose -H "Origin: https://${server}" -H "Authorization: Bearer ${registrationtoken}" -X DELETE "https://${server}/auth/register/${clientid}") | |
echo "$res" | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
J'ai utilisé ce script pour récupéré les opérations Cozy-bank qui ce trouve sur
io.cozy.bank-operations
. Mon but étant d'importé tout mes opérations dans un logiciel de comptabilité personnelle (Skrooge dans mon cas) et ce en gardant la catégorisation faite sur cozy-bankC'est pas encore fini mais j'ai un fichier csv avec toute les info et qui est lu par Skooge. Il va maintenant falloir voir comment géré la catégorisation...
Pour jq
J'ai fait un fichier 'Cozy-Bank-cleaner.jq' :
La deuxièmement ligne me permet de sélectionner ce que je veux et de renommer les keys
et la 3eme ligne modifie le format des dates (uniquement YYYY-MM-DD)
Sortie en CSV
Pour la sorti csv j'ai préféré utiliser
in2csv
(qui est danscsvkit
et présent dans les dépôt Debian depuis Buster)Commande final
jq -f Cozy-Bank-cleaner.jq data.json | in2csv -f json > data-clean.csv