128 lines
3.1 KiB
Bash
Executable File
128 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# SPDX-FileCopyrightText: 2023 Hugo Peixoto <hugo.peixoto@gmail.com>
|
|
|
|
set -ueo pipefail
|
|
|
|
test_command() {
|
|
command -v "$1" > /dev/null || (
|
|
echo "$1: comando não instalado"
|
|
exit 1;
|
|
)
|
|
}
|
|
|
|
test_command curl
|
|
test_command jq
|
|
test_command xargs
|
|
test_command sha256sum
|
|
test_command stat
|
|
|
|
PROCS=${PROCS:=4}
|
|
SERVER="https://sapo.pxto.pt/config.json"
|
|
export SAPO_EXEC="$0"
|
|
|
|
get_config() {
|
|
curl -s "$SERVER"
|
|
}
|
|
|
|
download_many() {
|
|
AMOUNT="$1"
|
|
export SAPO_CONTACT="$2"
|
|
|
|
CONFIG="$(get_config)"
|
|
export SAPO_CONFIG_VIDEOS_URL="$(echo "$CONFIG" | jq -r .videos_url)"
|
|
export SAPO_CONFIG_UPLOAD_URL="$(echo "$CONFIG" | jq -r .upload_url)"
|
|
export SAPO_CONFIG_MAX="$(echo "$CONFIG" | jq -r .max_amount)"
|
|
|
|
if [ "$AMOUNT" -gt "$SAPO_CONFIG_MAX" ]; then
|
|
echo "Não podes pedir mais do que $SAPO_CONFIG_MAX vídeos!" >&2
|
|
exit 1;
|
|
fi
|
|
|
|
mkdir -p videos
|
|
while [ "$AMOUNT" -gt 0 ]; do
|
|
if [ "$AMOUNT" -ge 1000 ]; then
|
|
N=1000
|
|
else
|
|
N=$AMOUNT
|
|
fi
|
|
|
|
echo "another part ($N)" >&2
|
|
curl -s "$SAPO_CONFIG_VIDEOS_URL?amount=$N" |
|
|
jq -r '.videos[]' |
|
|
xargs "-P$PROCS" -I {} "$SAPO_EXEC" single {}
|
|
|
|
AMOUNT=$(( $AMOUNT - $N ))
|
|
done
|
|
}
|
|
|
|
download_single() {
|
|
VIDEO_ID="$1"
|
|
VIDEO_URL="https://rd.videos.sapo.pt/$VIDEO_ID/mov/1"
|
|
FILENAME="videos/$VIDEO_ID.mp4"
|
|
|
|
if [ -f "$FILENAME" ]; then
|
|
SHA=$(sha256sum "$FILENAME" | awk '{print $1}')
|
|
SIZE=$(stat --format=%s "$FILENAME")
|
|
MB=$(( $SIZE / 1024 / 1024 ))
|
|
echo "repetido: $VIDEO_ID / sha256:$SHA / ${MB} MiB"
|
|
else
|
|
curl -Ls "$VIDEO_URL" -o "$FILENAME"
|
|
SHA=$(sha256sum "$FILENAME" | awk '{print $1}')
|
|
SIZE=$(stat --format=%s "$FILENAME")
|
|
MB=$(( $SIZE / 1024 / 1024 ))
|
|
|
|
PAYLOAD="$(jq -rc --null-input \
|
|
--arg video_id "$VIDEO_ID" \
|
|
--arg sha "$SHA" \
|
|
--arg email "$SAPO_CONTACT" \
|
|
--argjson size "$SIZE" \
|
|
'{video_id: $video_id, sha256: $sha, size: $size, email: $email}')"
|
|
curl -s "$SAPO_CONFIG_UPLOAD_URL" -H 'content-type: application/json' -d "$PAYLOAD" > /dev/null
|
|
|
|
echo "descarregado: $VIDEO_ID / sha256:$SHA / ${MB} MiB"
|
|
fi
|
|
}
|
|
|
|
unknown() {
|
|
CONFIG="$(get_config)"
|
|
SAPO_CHECK_VIDEOS_URL="$(echo "$CONFIG" | jq -r .check_url)"
|
|
|
|
find videos/ -name "*.mp4" |
|
|
sed -e 's|videos/\(.*\).mp4|\1|g' |
|
|
xargs -n1000 |
|
|
sed -e 's/ /","/g' -e 's/^/["/' -e 's/$/"]/' |
|
|
while read body; do
|
|
echo "$body" | curl --silent --json "@-" "$SAPO_CHECK_VIDEOS_URL"
|
|
done |
|
|
jq -rc '.videos[]|select(.sha256==null)|("videos/" + .randname + ".mp4")'
|
|
}
|
|
|
|
case "$1" in
|
|
"download")
|
|
download_many "$2" "$3"
|
|
;;
|
|
"single")
|
|
download_single "$2"
|
|
;;
|
|
"unknown")
|
|
unknown
|
|
;;
|
|
"stats")
|
|
BYTES="$(du -b videos/ | awk '{print $1}')"
|
|
NUM="$(ls -1 videos/ | wc -l)"
|
|
MB=$(( $BYTES / 1024 / 1024 ))
|
|
MB_PER_VIDEO=$(( $MB / $NUM ))
|
|
|
|
echo "Número de vídeos: $NUM"
|
|
echo "Tamanho total: $MB MiB"
|
|
echo "Tamanho médio: $MB_PER_VIDEO MiB/vídeo"
|
|
;;
|
|
"test")
|
|
get_config > /dev/null || (
|
|
echo "A comunicação com o servidor falhou: $SERVER"
|
|
exit 1;
|
|
)
|
|
echo "Tudo pronto!"
|
|
esac
|