env/bin/fetch.sh

70 lines
1.4 KiB
Bash
Raw Normal View History

2019-09-11 04:11:40 +02:00
#!/bin/sh
2019-12-08 19:14:46 +01:00
# fetch
# Written in 2019-2021 by Lucas
2019-09-11 04:11:40 +02:00
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
2019-12-08 19:14:46 +01:00
usage()
{
printf "Usage: %s [-T] URL [URL ...]\n" "${0##*/}" >&2
2019-09-11 04:11:40 +02:00
exit 1
}
fetch_cmd()
{
if tty=$(tty) && [ "$tty" != "not a tty" ]; then
$torsocks ftp "$@" >"$tty"
else
$torsocks ftp "$@"
fi
}
2019-09-11 04:11:40 +02:00
torsocks=torsocks
while getopts T flag; do
case $flag in
T) torsocks=
;;
*) usage
;;
esac
done
2020-05-03 16:47:27 +02:00
shift $((OPTIND - 1))
2019-12-08 19:14:46 +01:00
[ $# -gt 0 ] || usage
2019-09-11 04:11:40 +02:00
: ${ARCHIVE_BASEDIR:=~/tmp/archive}
mkdir -p "$ARCHIVE_BASEDIR"
everything=$ARCHIVE_BASEDIR/everything
2020-05-03 16:47:27 +02:00
touch "$everything"
2019-09-11 04:11:40 +02:00
dir=$ARCHIVE_BASEDIR/$(date +%Y/%m/%d)
mkdir -p "$dir" || exit 1
rc=0
for url; do
sha=$(sha256 -qs "$url")
2019-09-11 04:11:40 +02:00
outdir=$dir/$sha
if grep -q "^$url\$" "$everything"; then
2020-05-03 16:47:27 +02:00
printf "%s: already fetched %s\n" "${0##*/}" "$url" >&2
printf "%s\n" "$ARCHIVE_BASEDIR"/*/*/*/"$sha"/file
2019-09-11 04:11:40 +02:00
continue
fi
mkdir -p "$outdir"
{
2020-08-22 02:01:38 +02:00
fetch_cmd -o "$outdir/file" "$url" &&
printf "%s\n" "$url" >"$outdir/url" &&
printf "%s\n" "$url" >>"$everything" &&
printf "%s\n" "$outdir/file"
2019-09-11 04:11:40 +02:00
} || rc=1
done
exit $rc