Astroport.ONE/tools/TUBE.copy.sh

118 lines
4.1 KiB
Bash
Raw Normal View History

2022-08-25 17:21:39 +02:00
#!/bin/bash
########################################################################
# Version: 0.3
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
########################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
ME="${0##*/}"
# Need TW index.html path + IPNS publication Key (available in IPFS keystore)
# Search for "tube" tagged tiddlers to get URL
# Download video, add to ipfs and import new tiddler
# Publish !!
## BEWARE, DO NOT MODIFY TW DURING THIS PROCESS !!
# TODO use crontab to run regularly
2022-08-25 17:21:39 +02:00
INDEX="$1"
[[ ! $INDEX ]] && echo "Please provide path to source TW index.html" && exit 1
[[ ! -f $INDEX ]] && echo "Fichier TW absent. $INDEX" && exit 1
WISHKEY="$2"
[[ ! $WISHKEY ]] && echo "Please provide IPFS publish key" && exit 1
WNS=$(ipfs key list -l | grep -w $WISHKEY | cut -d ' ' -f1)
# Extract tag=tube from TW
rm -f ~/.zen/tmp/tiddlers.json
2022-08-25 17:21:39 +02:00
tiddlywiki --verbose --load ${INDEX} --output ~/.zen/tmp --render '.' 'tiddlers.json' 'text/plain' '$:/core/templates/exporters/JsonFile' 'exportFilter' '[tag[tube]]'
## Extract URL from text field
for yurl in $(cat ~/.zen/tmp/tiddlers.json | jq -r '.[].text' | grep 'http'); do
2022-08-25 17:21:39 +02:00
echo "Detected $yurl"
echo "Start Downloading"
mkdir -p ~/.zen/tmp/tube
2022-09-15 21:46:37 +02:00
TITLE=$(yt-dlp --print title ${yurl})
2022-09-15 21:51:51 +02:00
TITLE=${TITLE//[^a-zA-Z0-9 ]/}
2022-09-15 21:46:37 +02:00
2022-09-09 18:20:18 +02:00
# https://github.com/yt-dlp/yt-dlp#format-selection-examples
2022-09-15 13:56:34 +02:00
# SUBS ? --write-subs --write-auto-subs --sub-langs "en, en-orig" --embed-subs
# TODO : DELAY COPY OPERATION... Astro can download quicker at 03:00 AM
2022-09-15 21:51:51 +02:00
echo "yt-dlp -f \"bv*[ext=mp4][height<=480]+ba/b[height<=480] / bv*[ext=mp4][height<=720]+ba/b[height<=720]\" --no-mtime --embed-thumbnail --add-metadata -o \"$HOME/.zen/tmp/tube/$TITLE.%(ext)s\" ${yurl}"
2022-09-15 21:46:37 +02:00
yt-dlp -f "bv*[ext=mp4][height<=480]+ba/b[height<=480] / bv*[ext=mp4][height<=720]+ba/b[height<=720]" -S "filesize:500M" --no-mtime --embed-thumbnail --add-metadata -o "$HOME/.zen/tmp/tube/$TITLE.%(ext)s" ${yurl}
2022-09-15 14:23:29 +02:00
echo
2022-09-15 14:05:38 +02:00
# Get last writen file... TODO: Could we do better ?
2022-09-15 14:17:43 +02:00
# ZFILE=$(ls -t ~/.zen/tmp/tube/*.mp4 | head -n 1)
2022-09-15 21:46:37 +02:00
2022-09-15 21:17:48 +02:00
ZFILE="$TITLE.mp4"
2022-09-15 14:55:11 +02:00
echo "$ZFILE"
2022-09-15 14:39:48 +02:00
2022-09-15 14:23:29 +02:00
[[ ! -f "$HOME/.zen/tmp/tube/$ZFILE" ]] && echo "No FILE -- EXIT --" && exit 1
echo
2022-08-25 17:21:39 +02:00
2022-09-15 14:39:48 +02:00
echo "FOUND : ~/.zen/tmp/tube/$ZFILE"
2022-09-15 12:35:42 +02:00
2022-08-25 17:21:39 +02:00
echo "Adding to IPFS"
2022-09-15 13:45:52 +02:00
ILINK=$(ipfs add -q "$HOME/.zen/tmp/tube/$ZFILE" | tail -n 1)
2022-08-25 17:21:39 +02:00
echo "/ipfs/$ILINK ready"
2022-09-15 13:45:52 +02:00
MIME=$(file --mime-type "$HOME/.zen/tmp/tube/$ZFILE" | rev | cut -d ' ' -f 1 | rev)
2022-08-25 17:21:39 +02:00
2022-09-15 13:45:52 +02:00
TEXT="<video controls width=360><source src='/ipfs/"${ILINK}"' type='"${MIME}"'></video><h1>"${ZFILE}"</h1>"
2022-08-25 17:21:39 +02:00
echo "Creating Youtube tiddler"
2022-09-15 13:41:53 +02:00
echo $TEXT
2022-08-25 17:21:39 +02:00
echo '[
{
2022-09-15 13:45:52 +02:00
"title": "'$ZFILE'",
"type": "'text/vnd.tiddlywiki'",
"text": "'$TEXT'",
2022-08-29 16:10:21 +02:00
"ipfs": "'${ILINK}'",
"tags": "'ipfs youtube ${MIME}'"
2022-08-25 17:21:39 +02:00
}
]
2022-09-15 13:45:52 +02:00
' > "$HOME/.zen/tmp/tube/$ZFILE.TW.json"
2022-08-25 17:21:39 +02:00
echo "=========================="
2022-08-25 17:21:39 +02:00
echo "Adding tiddler to TW"
rm -f ~/.zen/tmp/newindex.html
2022-09-15 13:45:52 +02:00
echo "importing $HOME/.zen/tmp/tube/$ZFILE.TW.json"
2022-09-15 13:41:53 +02:00
tiddlywiki --load $INDEX \
2022-09-15 13:45:52 +02:00
--import "$HOME/.zen/tmp/tube/$ZFILE.TW.json" "application/json" \
--deletetiddlers '[tag[tube]]' \
2022-08-25 17:21:39 +02:00
--output ~/.zen/tmp --render "$:/core/save/all" "newindex.html" "text/plain"
if [[ -s ~/.zen/tmp/newindex.html ]]; then
echo "Updating $INDEX"
cp ~/.zen/tmp/newindex.html $INDEX
echo "ipfs name publish -k $WISHKEY"
ILINK=$(ipfs add -q $INDEX | tail -n 1)
ipfs name publish -k $WISHKEY /ipfs/$ILINK
echo "/ipfs/$ILINK"
else
2022-08-25 17:21:39 +02:00
echo "Problem with tiddlywiki command. Missing ~/.zen/tmp/newindex.html"
echo "XXXXXXXXXXXXXXXXXXXXXXX"
2022-08-25 17:21:39 +02:00
fi
2022-08-25 17:21:39 +02:00
done
myIP=$(hostname -I | awk '{print $1}' | head -n 1)
echo "=========================="
echo "Nouveau TW"
echo "http://$myIP:8080/ipns/$WNS"
2022-08-25 17:21:39 +02:00
# Removing tag=tube
# --deletetiddlers '[tag[tube]]'