Last active
March 29, 2022 03:08
-
-
Save FiskFan1999/66daa3063f63418cb6957123d7f8955d to your computer and use it in GitHub Desktop.
Upload video to peertube in single request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# +--------------------------+ | |
# | PEERTUBE UPLOAD SCRIPT | | |
# | by William Rehwinkel | | |
# | (implements example in | | |
# | REST API documentation) | | |
# +--------------------------+ | |
# | Note that some param- | | |
# | eters, such as "comments | | |
# | enabled" or "nsfw", are | | |
# | hard-coded. These can be | | |
# | modified according to | | |
# | your use case. | | |
# +--------------------------+ | |
## DEPENDENCIES: jq | |
read -p "website [$PEERTUBEDEF_WEBSITE]: " WEBSITE | |
if [[ "$WEBSITE" == "" ]] | |
then | |
WEBSITE="$PEERTUBEDEF_WEBSITE" | |
fi | |
API="$WEBSITE/api/v1" | |
read -p "username [$PEERTUBEDEF_USERNAME]: " USERNAME | |
if [[ "$USERNAME" == "" ]] | |
then | |
USERNAME="$PEERTUBEDEF_USERNAME" | |
fi | |
if [[ "$1" == "listchannels" ]] | |
then | |
url="$WEBSITE/api/v1/accounts/$USERNAME/video-channels" | |
resp=$(curl -s "$url") | |
printf "%s" "$resp" | jq .data[] | jq '.name,.id' | |
exit 0 | |
fi | |
# no default password, | |
# continually loop | |
if [[ "$PEERTUBEDEF_PASSWORD" == "" ]] | |
then | |
printf "password: " | |
else | |
printf "password [default stored]: " | |
fi | |
read -s PASSWORD | |
echo | |
if [[ "$PASSWORD" == "" ]] | |
then | |
PASSWORD="$PEERTUBEDEF_PASSWORD" | |
fi | |
## AUTH | |
client_id=$(curl -s "$API/oauth-clients/local" | jq -r ".client_id") | |
client_secret=$(curl -s "$API/oauth-clients/local" | jq -r ".client_secret") | |
TOKENTEMP=$(mktemp) || exit 1 | |
httpresponse=$(curl -w "%{http_code}" --http1.1 -s "$API/users/token" \ | |
--data client_id="$client_id" \ | |
--data client_secret="$client_secret" \ | |
--data grant_type=password \ | |
--data username="$USERNAME" \ | |
--data password="$PASSWORD" \ | |
-o "$TOKENTEMP") | |
if [[ "$httpresponse" == "429" ]] | |
then | |
echo "api/v1/users/token rate limited. Try again later" | |
exit 1 | |
elif [[ "$httpresponse" != "200" ]] | |
then | |
echo "api/v1/users/token failed. Try again later" | |
exit 1 | |
else | |
echo "api/v1/users/token success" | |
fi | |
PEERTUBETOKEN=$(cat "$TOKENTEMP" | jq -r ".access_token") | |
#token=$(curl --http1.1 -s "$API/users/token" \ | |
#--data client_id="$client_id" \ | |
#--data client_secret="$client_secret" \ | |
#--data grant_type=password \ | |
#--data username="$USERNAME" \ | |
#--data password="$PASSWORD" \ | |
#| jq -r ".access_token") | |
read -p "channel ID [$PEERTUBEDEF_CHANNELID]: " CHANNEL_ID | |
if [[ "$CHANNEL_ID" == "" ]] | |
then | |
CHANNEL_ID="$PEERTUBEDEF_CHANNELID" | |
fi | |
# no default | |
while [[ "$FILE_PATH" == "" ]] | |
do | |
read -p "video file path: " FILE_PATH | |
done | |
# no default | |
while [[ "$NAME" == "" ]] | |
do | |
read -p "video name: " NAME | |
done | |
TEMPFILEDESC=$(mktemp) || exit 1 | |
trap 'rm -f "$TEMPFILEDESC"' EXIT | |
echo Write the description of the video here, then save and quit this document. > "$TEMPFILEDESC" | |
vi "$TEMPFILEDESC" | |
available_privacy=$(curl "$API/videos/privacies") | |
echo $available_privacy | |
while [[ "$PRIVACY" == "" ]] | |
do | |
read -p "privacy (int): " PRIVACY | |
done | |
while [[ "$TAGSA" == "" || $(echo -n "$TAGSA" | wc -c ) -ge 30 ]] | |
do | |
read -p "tag 1: " TAGSA | |
done | |
while [[ "$TAGSB" == "" || $(echo -n "$TAGSB" | wc -c ) -ge 30 ]] | |
do | |
read -p "tag 2: " TAGSB | |
done | |
while [[ "$TAGSC" == "" || $(echo -n "$TAGSC" | wc -c ) -ge 30 ]] | |
do | |
read -p "tag 3: " TAGSC | |
done | |
while [[ "$TAGSD" == "" || $(echo -n "$TAGSD" | wc -c ) -ge 30 ]] | |
do | |
read -p "tag 4: " TAGSD | |
done | |
while [[ "$TAGSE" == "" || $(echo -n "$TAGSE" | wc -c ) -ge 30 ]] | |
do | |
read -p "tag 5: " TAGSE | |
done | |
curl "$API/videos/categories" | |
echo | |
read -p "category [$PEERTUBEDEF_CATEGORY]: " CATEGORY | |
if [[ "$CATEGORY" == "" ]] | |
then | |
CATEGORY="$PEERTUBEDEF_CATEGORY" | |
fi | |
curl "$API/videos/licences" | jq . | |
read -p "license [$PEERTUBEDEF_LICENSE]: " LICENSE | |
if [[ "$LICENSE" == "" ]] | |
then | |
LICENSE="$PEERTUBEDEF_LICENSE" | |
fi | |
curl "$API/videos/languages" | jq . | |
read -p "language [$PEERTUBEDEF_LANGUAGE]: " LANGUAGE | |
if [[ "$LANGUAGE" == "" ]] | |
then | |
LANGUAGE="$PEERTUBEDEF_LANGUAGE" | |
fi | |
if [[ "$1" == "test" ]] | |
then | |
exit 2 | |
fi | |
OUTPUTFILE="$(mktemp)" | |
## VIDEO UPLOAD | |
curl --http1.1 "$API/videos/upload" \ | |
-H "Authorization: Bearer $PEERTUBETOKEN" \ | |
--max-time 600 \ | |
--form videofile=@"$FILE_PATH" \ | |
--form channelId=$CHANNEL_ID \ | |
--form privacy=$PRIVACY \ | |
--form category=$CATEGORY \ | |
--form licence=$LICENSE \ | |
--form language="$LANGUAGE" \ | |
--form description="<$TEMPFILEDESC" \ | |
--form tags="$TAGSA" \ | |
--form tags="$TAGSB" \ | |
--form tags="$TAGSC" \ | |
--form tags="$TAGSD" \ | |
--form tags="$TAGSE" \ | |
--form waitTranscoding="true" \ | |
--form name="$NAME" \ | |
--form commentsEnabled="true" \ | |
--form downloadEnabled="true" \ | |
--form nsfw="false" \ | |
-o "$OUTPUTFILE" | |
# forms not used: originallyPublishedAt, | |
# previewfile, scheduleUpdate (refer to REST api reference), | |
# support, thumbnailfile | |
cat "$OUTPUTFILE" | jq . | |
rm "$OUTPUTFILE" "$TOKENTEMP" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment