Skip to content

Instantly share code, notes, and snippets.

@noaione
Created June 12, 2019 04:24
Show Gist options
  • Save noaione/2393f4a0f646d6afec2e8718c4b68f0d to your computer and use it in GitHub Desktop.
Save noaione/2393f4a0f646d6afec2e8718c4b68f0d to your computer and use it in GitHub Desktop.
QBittorrent Discord Webhook Notifier and Rclone Command Wrapper
import argparse
import subprocess
from discord_webhook import DiscordWebhook, DiscordEmbed
"""
QBittorrent Discord Webhook and Rclone command wrapper
Version: 1.0
This script will send a notification to Discord via webhook, that your download are finished.
After that, it execute a "copy" command from rclone to back it up to cloud storage
This script might or not might works with other torrent client.
:param name: The torrent name that will be sended
:param size: Torrent sizes in bytes.
:param hash: Torrent hash
:param path: Full path to the downloaded file or folder
If you're in linux, chown this file to the user that run torrent client and
don't forget to put the rclone.conf file to `~/.config/rclone/rclone.conf` then
chown the `rclone` folder and the config file in `~/.config`
"""
# Python Version: 3.6+
# Requirements: `pip3 install discord-webhook``
# Help: `python3.6 qbitWebhook.py -h``
#################################################
WEBHOOK_URL = 'DISCORD WEBHOOK URL'
RCLONE_UPLOAD_DESTINATION = 'UPLOAD DESTINATION WITHOUT QUOTATION MARK'
DISCORD_ID = 'YOUR DISCORD USER ID'
#################################################
def sizeof_fmt(num, suffix='B'):
num = int(num)
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)
parser = argparse.ArgumentParser(description='QBittorrent Discord Notifier and Rclone wrapper')
parser.add_argument('--name', required=True, help="Torrent name")
parser.add_argument('--size', required=True, help="Torrent size")
parser.add_argument('--hash', required=True, help="Torrent hash")
parser.add_argument('--path', required=True, help="Torrent fullpath")
args = parser.parse_args()
print('@@ Sending First Webhook Notifier')
hook = DiscordWebhook(url=WEBHOOK_URL, content="File will be uploaded\n<@!{}>".format(DISCORD_ID))
embed = DiscordEmbed(title="Torrent Downloaded", color=0xf8c1b8)
embed.add_embed_field(name='Torrent', value=args.name)
embed.add_embed_field(name='Ukuran', value=sizeof_fmt(args.size))
embed.set_footer(text=args.hash)
embed.set_timestamp()
hook.add_embed(embed)
hook.execute()
dummy_args = ['rclone', 'copy', args.path, RCLONE_UPLOAD_DESTINATION]
print('@@ Executing rclone command')
try:
with subprocess.Popen(dummy_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
print('@@ Communicating resutls')
out, err = proc.communicate()
if int(proc.returncode) == 0:
print('Return Code Zero: Sending success message')
hook = DiscordWebhook(WEBHOOK_URL, content="Torrent `{}` uploaded to your cloud storage\n<@!{}>".format(args.name, DISCORD_ID))
hook.execute()
print('@@ All process executed, exiting...')
exit(0)
print('@@ Non-Zero Code Returned: Sending failed message')
hook = DiscordWebhook(WEBHOOK_URL, content="Failed when trying to upload `{}` torrent to cloud storage!\n<@!{}>".format(args.name, DISCORD_ID))
hook.execute()
except Exception as e:
print('An Exception Occured: ' + e)
print('Sending failed message')
hook = DiscordWebhook(WEBHOOK_URL, content="Failed when trying to upload `{}` torrent to cloud storage!\n<@!{}>".format(args.name, DISCORD_ID))
hook.execute()
@AKASGaming
Copy link

AKASGaming commented Nov 13, 2023

  1. Does this still work with qBittorrent 4.6.0?
  2. Where do I put this for the case of qBittorrent in a docker container?

@noaione
Copy link
Author

noaione commented Nov 27, 2023

@AKASGaming

  1. Don't know, if qbit does not change anything with their hook system it should work fine. (This was made for qBit 4.1.6)
  2. You would need to adjust your docker setup process to add Python (+ discord_webhook lib) and add a folder where you can mount this file to your docker volume.

I haven't use this script in a while since I do not need to mirror my torrent download anymore, so I'm only using the webhook part

Simplified:

from discord_webhook import DiscordEmbed, DiscordWebhook
from sys import argv

tid, tn, tpath = argv[1:]

embed = DiscordEmbed(title="Torrent Completed", description=tn, color=0x64b65d)
embed.set_author(name="Seedbox", url="REDACTED", icon_url="REDACTED")
embed.set_footer(text='ID: {} | {}'.format(tid, tpath))

hook = DiscordWebhook(url="REDACTED")

hook.add_embed(embed)
hook.execute()

Then I wrap it to .sh file for easier access

#!/bin/bash
torrentid=$1
torrentname=$2
torrentpath=$3
echo "Finished Torrent Details: " "$torrentname" "$torrentpath" "$torrentid"  >> ~/qbit_exec.log

python ~/scripts/torrentdone.py "$torrentid" "$torrentname" "$torrentpath"

And on the qBit WebUI on running external program I just use this:

~/scripts/done.sh "%I" "%N" "%D | MizoreSB qBT"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment