Commit 77034c64 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

premier jet import nakala. max.sh --nakala-feed dbname collection id (clé...

premier jet import nakala. max.sh --nakala-feed dbname collection id (clé d'api à renseigner dans configuration>nakala>api_key
parent 9141ff1b
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ display_usage(){
    echo "   --list-plugins: show plugins and status."
    echo "   --enable-plugin [plugin_name]: Enable [plugin_name] plugin."
    echo "   --disable-plugin [plugin_name]: Disable [plugin_name] plugin."
    echo "   --nakala-feed [project_id] [nakala_collection_id]: Feeds a Nakala collection with project sources"
    echo ""
}

@@ -136,6 +137,74 @@ disable_plugin(){
}





nakala_feed(){
  apikey=$($BASEX_BIN "doc('$DIRECTORY/../configuration/configuration.xml')//nakala/api_key/text()")
  collection_id=$2
  #removes old export if exists
  if [ -d /tmp/$1 ]
  then rm -rf /tmp/$1
  fi

  #creates export path
  mkdir /tmp/$1

  #export with basex cli
  $BASEX_BIN "-c<commands><open name='$1'/><export path='/tmp/$1'/></commands>"

  echo -e 'will export /tmp/'$1' in nakala collection with id '$2

  for f in `ls /tmp/$1/*.xml`
  do
    nakala_upload_file $f $apikey $collection_id
  done

}

nakala_upload_file(){
  file=$1
  apikey=$2
  collection_id=$3
  response=$(curl -w "\n%{http_code}" -s \
  -X POST "https://apitest.nakala.fr/datas/uploads" \
  -H  "accept: application/json" \
  -H "X-API-KEY: $apikey" \
  -H  "Content-Type: multipart/form-data" \
  -F "file=@$file;type=application/xml")
  response=(${response[@]}) # convert to array
  code=${response[-1]} # get last element (last line)
  body=${response[@]::${#response[@]}-1} # get all elements except last
  sha1=$(echo $body | sed -E 's/.*"sha1":"?([^,"]*)"?.*/\1/')
  echo -e 'code = '$code
  case $code in
    '401')  echo 'Upload impossible du fichier '$f', vérifiez votre clé API';return 1;;
    '201') nakala_add_file_to_collection $f $sha1 $collection_id $apikey;return 0;;
  esac
}

nakala_add_file_to_collection(){
  file=$1
  sha1=$2
  collection_id=$3
  apikey=$4
  title=$($BASEX_BIN "string(doc('$file')//*:teiHeader//*:titleStmt/*:title)")
  echo -e 'Ajout du fichier '$file' à la collection '$collection_id
  response=$(curl -s -X POST "https://apitest.nakala.fr/datas" \
  -H  "accept: application/json" \
  -H  "X-API-KEY: $apikey" \
  -H  "Content-Type: application/json" \
  -d "{  \"collectionsIds\": [ \"$collection_id\"],\
  \"files\": [{\"sha1\": \"$sha1\",\"embargoed\": \"2025-03-27\",\"description\": \"desc todo\"}],\
  \"relations\": [],  \"status\": \"pending\",\
  \"metas\":[{\"value\": \"0BSD\",\"lang\": \"ndk\",\"typeUri\": \"http://www.w3.org/2001/XMLSchema#string\",\"propertyUri\": \"http://nakala.fr/terms#license\" },\
  {\"value\": \"$title\",\"lang\": \"ndk\",\"typeUri\": \"http://www.w3.org/2001/XMLSchema#string\",\"propertyUri\": \"http://nakala.fr/terms#title\" },\
  {\"value\":\"http://purl.org/coar/resource_type/c_18cf\",\"lang\": \"ndk\",\"typeUri\": \"http://www.w3.org/2001/XMLSchema#anyURI\",\"propertyUri\": \"http://nakala.fr/terms#type\" }],\
  \"rights\": []}")
  echo $response
}

# 1 argument required
if [  $# -lt 1 ]
then
@@ -159,6 +228,7 @@ while [[ "$#" -gt 0 ]]; do
        --list-plugins) list_plugins;exit 0;;
        --enable-plugin) enable_plugin $2 $3;exit 0;;
        --disable-plugin) disable_plugin $2 $3;exit 0;;
        --nakala-feed) nakala_feed $2 $3; exit 0;;
         *) echo "Unknown parameter passed: $1"; exit 1 ;;
    esac
    shift
@@ -285,6 +355,7 @@ init_dev_max(){
}



#creates config file if not exists (.dist copy)
if [ ! -f $DIRECTORY/../configuration/configuration.xml ]
then