Last active
April 9, 2021 21:08
-
-
Save lifehome/6040b902119dd1bc2cff68a2930c82d5 to your computer and use it in GitHub Desktop.
aria2 auto add torrent script for inotify - bash - licensed on AGPL-3.0
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
#!/bin/bash | |
aria2PROTOCOL="http" | |
aria2HOST="127.0.0.1" | |
aria2PORT="6800" | |
aria2TOKEN="1234567890abcdef" | |
aria2ENDPOINT="/jsonrpc" | |
discordNotification=true | |
discordActorUsername="Aria2 Torrent Automator" | |
discordActorAvatarURL="https://i.imgur.com/9S4duHx.png" | |
discordWebhookURL="https://discord.com/api/webhooks/..." | |
invokeDiscordWebhook() { | |
curl -i -s -o /dev/null -X POST -H "Content-Type: application/json" -d '{"username": "'"$2"'", "avatar_url": "'"$3"'", "content": "'"$4"'"}' $1 | |
} | |
endpoint_handler() { | |
if [[ $2 == *"Bencode decoding failed"* ]]; then | |
echo "ERROR - Corrupted torrent file found, disabling it now: $1" | |
mv "$1" "$1.corrupted" | |
if [ "$discordNotification" = true ]; then | |
invokeDiscordWebhook "$discordWebhookURL" "$discordActorUsername" "$discordActorAvatarURL" "Oops! The following torrent is corrupted, it has been renamed to prevent further accidents:\n> $1" | |
fi | |
return 2 | |
elif [[ $2 == *"Unauthorized"* ]]; then | |
echo "ERROR - Invalid aria2 JSONRPC secret, please fix it before executing again!" | |
if [ "$discordNotification" = true ]; then | |
invokeDiscordWebhook "$discordWebhookURL" "$discordActorUsername" "$discordActorAvatarURL" "Hmm... Your Aria2 JSONRPC secret is incorrect, please fix it before executing another iteration.\nThe following file is skipped:\n> $1" | |
fi | |
return 999 | |
elif [[ $2 == *"result"* ]]; then | |
echo "OK - Torrent added, renaming it as a backup: $1" | |
mv "$1" "$1.bak" | |
if [ "$discordNotification" = true ]; then | |
invokeDiscordWebhook "$discordWebhookURL" "$discordActorUsername" "$discordActorAvatarURL" "Job added:\n> $1" | |
fi | |
return 0 | |
else | |
echo "FATAL - Unexpected error occured while handling: $1" | |
if [ "$discordNotification" = true ]; then | |
invokeDiscordWebhook "$discordWebhookURL" "$discordActorUsername" "$discordActorAvatarURL" "Unhandled Exception: Cannot process the following torrent while communicating with Aria2 Endpoint:\n> $1" | |
fi | |
return 1 | |
fi | |
} | |
handle_magnet_file() { | |
magnetUri="$(cat "$1")" | |
uid="INOTIFYDOCKER_$(date +%s)_$(echo "$1" | base64 | tr -d '\n')" | |
status_code=$(echo '--data-binary "{\"jsonrpc\":\"2.0\",\"method\":\"aria2.addUri\",\"id\":\"'"$uid"'\",\"params\":[\"token:'"$aria2TOKEN"'\",[\"'"$magnetUri"'\"]]}"' | curl -K- -i -s "$aria2PROTOCOL://$aria2HOST:$aria2PORT$aria2ENDPOINT" -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache') | |
endpoint_handler "$1" "$status_code" | |
} | |
handle_torrent_file() { | |
b64torrent="$(base64 "$1" | tr -d '\n')" | |
uid="INOTIFYDOCKER_$(date +%s)_$(echo "$1" | base64 | tr -d '\n')" | |
status_code=$(echo '--data-binary "{\"jsonrpc\":\"2.0\",\"method\":\"aria2.addTorrent\",\"id\":\"'"$uid"'\",\"params\":[\"token:'"$aria2TOKEN"'\",\"'"$b64torrent"'\"]}"' | curl -K- -i -s "$aria2PROTOCOL://$aria2HOST:$aria2PORT$aria2ENDPOINT" -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache') | |
endpoint_handler "$1" "$status_code" | |
} | |
scan_directory() { | |
dir=$1 | |
if [ -d "$dir" ]; then | |
cd "$dir" | |
for tf in *.torrent; do | |
if [ "$tf" != "*.torrent" ]; then | |
handle_torrent_file "$tf" | |
fi | |
done | |
for mf in *.magnet; do | |
if [ "$mf" != "*.magnet" ]; then | |
handle_magnet_file "$mf" | |
fi | |
done | |
else | |
echo 'ERROR - This is not a directory: '"$1" | |
fi | |
} | |
scan_directory $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment