2022-05-10 03:32:59 +00:00
# To do:
# - Get API keys from somewhere else
# - Change installation warnings
2022-01-12 01:29:11 +00:00
# Filenames
input = " $1 "
root = " $( echo " $input " | sed 's/.md//g' ) "
links = " $root .links.txt "
2022-05-10 03:32:59 +00:00
archivedLinks = "captures.log" ##"$root.links.archived.txt"
errors = "error-json.log"
2022-01-12 01:29:11 +00:00
output = " $root .longnow.md "
2021-06-28 14:38:06 +00:00
2022-01-12 01:29:11 +00:00
## Directories
initialDir = " $( pwd ) "
workdir = " longnow- $root "
## Move to work dir
function moveToWorkDir( ) {
mkdir -p " $workdir "
2022-03-05 15:50:30 +00:00
cp " $input " " $workdir / $input "
2022-01-12 01:29:11 +00:00
cd " $workdir "
}
## Extract markdown links
function extractMarkdownLinks( ) { # Use: Takes a markdown file file.md, extracts all links, finds the unique ones and saves them to file.md.links
links2 = " $root .links2.txt "
2021-06-28 10:27:08 +00:00
echo ""
echo "Extracting links..."
2022-01-12 01:29:11 +00:00
rm -f " $links "
grep -Eoi '\]\((.*)\)' " $input " | grep -Eo '(http|https)://[^)]+' >> " $links "
awk '!seen[$0]++' " $links " > " $links2 " && mv " $links2 " " $links "
echo "Done extracting links"
2021-06-28 10:27:08 +00:00
}
2022-01-12 01:29:11 +00:00
## Push to Archive
2021-06-28 10:27:08 +00:00
function pushToArchive( ) {
# Use: Takes a txt file with one link on each line and pushes all the links to the internet archive. Saves those links to a textfile
# References:
# https://unix.stackexchange.com/questions/181254/how-to-use-grep-and-cut-in-script-to-obtain-website-urls-from-an-html-file
# https://github.com/oduwsdl/archivenow
2021-06-30 20:58:58 +00:00
# For the double underscore, see: https://stackoverflow.com/questions/13797087/bash-why-double-underline-for-private-functions-why-for-bash-complet/15181999
2022-01-12 01:29:11 +00:00
echo ""
echo "Pushing to archive.org..."
numLinesLinkFile = $( wc -l " $links " | awk '{ print $1 }' )
totalTimeInMinutes = $( echo " scale=0; ( $numLinesLinkFile *7.5 + 60* $numLinesLinkFile /15)/60 " | bc)
echo " Expected to take ~ $totalTimeInMinutes mins. "
2022-03-05 15:50:30 +00:00
echo ""
2022-05-10 03:32:59 +00:00
/home/loki/.bash/src/longnow/spn/wayback-machine-spn-scripts/spn.sh -a [ my private key] -f . -p 3 " $links "
2021-06-30 20:58:58 +00:00
2022-01-12 01:29:11 +00:00
echo "Done pushing links to archive.org"
2021-06-28 10:27:08 +00:00
echo ""
}
2022-01-12 01:29:11 +00:00
## Add archive links to file
2021-06-28 10:27:08 +00:00
function addArchiveLinksToFile( ) {
2022-01-12 01:29:11 +00:00
echo " Creating longnow file at $output "
2021-06-28 10:27:08 +00:00
2022-01-12 01:29:11 +00:00
rm -f " $output "
cp " $input " " $output "
2021-06-28 10:27:08 +00:00
while IFS = read -r url
2021-06-30 20:58:58 +00:00
do
wait
2022-01-12 01:29:11 +00:00
archivedUrl = $( ( grep " $url $" " $archivedLinks " ; grep " $url / $" " $archivedLinks " ) | tail -1)
2021-06-30 20:58:58 +00:00
if [ " $archivedUrl " != "" ] ; then
## echo "Url: $url"
## echo "ArchivedUrl: $archivedUrl"
urlForSed = " ${ url // \/ / \\ / } "
archiveUrlForSed = " ${ archivedUrl // \/ / \\ / } "
2022-05-10 03:32:59 +00:00
sed -i " s/ $urlForSed )/ $urlForSed ) ([a](https:\/\/web.archive.org $archiveUrlForSed ))/g " " $output "
2021-06-30 20:58:58 +00:00
##else
##echo "There was an error for $url; see the $errorsFile"
fi
2022-01-12 01:29:11 +00:00
done < " $links "
2021-06-30 20:58:58 +00:00
echo "Done."
2022-01-12 01:29:11 +00:00
}
2021-06-28 10:27:08 +00:00
2022-01-12 01:29:11 +00:00
## Explain installation
2022-03-05 15:50:30 +00:00
function explainArchiveNowInstallation( ) {
2022-01-12 01:29:11 +00:00
echo "Required archivenow utility not found in path."
echo "Install with \$ pip install archivenow"
echo "(resp. \$ pip3 install archivenow)"
echo "Or follow instructions on https://github.com/oduwsdl/archivenow"
2021-06-28 10:27:08 +00:00
}
2022-03-05 15:50:30 +00:00
function explainJqInstallation( ) {
echo "Required jq utility not found in path."
echo "Install with your package manager, e.g., \$ sudo apt install jq"
echo "Or follow instructions on https://stedolan.github.io/jq/download/"
}
2022-01-12 01:29:11 +00:00
## Report errors
function reportErrors( ) {
2022-05-10 03:32:59 +00:00
if test -f " $errors " ; then
2022-01-12 01:29:11 +00:00
echo " It seems that there are errors. To view and deal with them, see the $errors file "
fi
}
## Clean up
function cleanup( ) {
cp " $output " " ../ $output "
cd " $initialDir "
}
## Main
function main( ) {
doesArchiveNowExist = " $( whereis "archivenow" ) "
2022-03-05 15:50:30 +00:00
doesJqExist = " $( whereis "jq" ) "
if [ " $doesArchiveNowExist " = = "archivenow:" ] ; then
explainArchiveNowInstallation
elif [ " $doesJqExist " = = "jq:" ] ; then
explainJqInstallation
2021-06-28 10:27:08 +00:00
else
2022-03-05 15:50:30 +00:00
moveToWorkDir
extractMarkdownLinks
pushToArchive
addArchiveLinksToFile
reportErrors
cleanup
2021-06-28 10:27:08 +00:00
fi
}
2022-01-12 01:29:11 +00:00
main
2021-06-28 10:27:08 +00:00