Skip to content

Instantly share code, notes, and snippets.

@nsapa
Last active November 29, 2020 16:48
Show Gist options
  • Save nsapa/ea71c2ec16bc363a3da8613b19dfa3ba to your computer and use it in GitHub Desktop.
Save nsapa/ea71c2ec16bc363a3da8613b19dfa3ba to your computer and use it in GitHub Desktop.
Fix some thumbnails upload by tubeup on IA
#!/bin/bash
set -e
function parse_file() {
if [ $( (echo $1 | grep "Web/P image") | wc -l) -eq 1 ]; then
echo "Web/P image"
return
fi
echo "unknow file format"
return
}
#stdin content:
#youtube-zvicyGuszJ4.json: "fail-reasons": "JPEGThumb:filetype doesn't match name:zvicyGuszJ4.jpg"
Fixer_Root_Dir=$(pwd)
while read ligne; do
JSON_file=$(echo $ligne | cut -f1 -d:)
IA_id=$( (jq .metadata.\"identifier\" <${JSON_file}) | cut -f2 -d\")
Fail_reasons=$( (jq .metadata.\"fail-reasons\" <${JSON_file}) | cut -f2 -d\")
echo "Working on ${IA_id}"
if [ $( (echo $Fail_reasons | grep JPEGThumb) | wc -l) -eq 1 ]; then
if [ -f ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.done ]; then
echo "Already fixed thumbnails on ${IA_id}, skipping."
continue
fi
TEMP_FOLDER=$(mktemp -d)
pushd ${TEMP_FOLDER}
if [ ! -f ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.deletefix ]; then
echo "Fixing a wrong format for thumbnails"
WrongImage_file=$(echo $Fail_reasons | cut -f3 -d:)
ia download ${IA_id} ${WrongImage_file}
WrongImage_format=$(file ${IA_id}/${WrongImage_file} | cut -f2 -d:)
echo "${WrongImage_file} was supposed to be a JPEG but it is really a $(parse_file "${WrongImage_format}") "
echo "Converting ${WrongImage_file} to a real JPEG (${IA_id}.jpg)"
convert ${IA_id}/${WrongImage_file} ${IA_id}.jpg
if [ ! -f ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.uploadfix ]; then
echo "Uploading fixed thumbnail"
ia upload ${IA_id} ${IA_id}.jpg --retries=5 -H x-archive-keep-old-version:0
touch ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.uploadfix
fi
echo "Removing old thumbnails"
ia delete ${IA_id} ${WrongImage_file} __ia_thumb.jpg -n --retries=5
echo "Removing tempory files"
rm ${IA_id}/${WrongImage_file} ${IA_id}.jpg
rmdir ${IA_id}
touch ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.deletefix
fi
if [ ! -f ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.metadatafix ]; then
echo "Removing fail-reasons in the metadata"
ia metadata ${IA_id} --remove="fail-reasons:${Fail_reasons}"
touch ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.metadatafix
fi
popd
rmdir ${TEMP_FOLDER}
touch ${Fixer_Root_Dir}/${IA_id}.JPEGThumb.done
fi
echo "[DONE] for ${IA_id} (https://archive.org/details/${IA_id})"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment