Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion autoremovetorrents/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .exception.nosuchclient import NoSuchClient
from .strategy import Strategy
from autoremovetorrents.torrent import Torrent
from .util.discord_webhook_handler import DiscordWebhookHandler

class Task(object):
def __init__(self, name, conf, remove_torrents = True):
Expand Down Expand Up @@ -37,6 +38,11 @@ def __init__(self, name, conf, remove_torrents = True):
self._enabled_remove = remove_torrents
self._delete_data = conf['delete_data'] if 'delete_data' in conf else False
self._strategies = conf['strategies'] if 'strategies' in conf else []
self._webhook_url = conf['webhook'] if 'webhook' in conf else None

# Get webhook handler
if self._webhook_url is not None:
self._webhook_handler = DiscordWebhookHandler(name, self._webhook_url, self._client_name)

# Torrents
self._torrents = set()
Expand Down Expand Up @@ -120,18 +126,22 @@ def _remove_torrents(self):
delete_list[torrent.hash] = torrent.name
# Run deletion
success, failed = self._client.remove_torrents([hash_ for hash_ in delete_list], self._delete_data)
# Output logs
# Output logs and send webhooks
for hash_ in success:
self._logger.info(
'The torrent %s and its data have been removed.' if self._delete_data \
else 'The torrent %s has been removed.',
delete_list[hash_]
)
if self._webhook_url is not None:
self._webhook_handler.send_success(delete_list[hash_])
for torrent in failed:
self._logger.error('The torrent %s and its data cannot be removed. Reason: %s' if self._delete_data \
else 'The torrent %s cannot be removed. Reason: %s',
delete_list[torrent['hash']], torrent['reason']
)
if self._webhook_url is not None:
self._webhook_handler.send_failure(delete_list[torrent['hash']], torrent['reason'])

# Execute
def execute(self):
Expand Down
66 changes: 66 additions & 0 deletions autoremovetorrents/util/discord_webhook_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding:utf-8 -*-
import requests
from .. import logger

class DiscordWebhookHandler:
def __init__(self, name, webhook_url, client_name):
# Logger
self._logger = logger.Logger.register(__name__)

# Settings
self._task_name = name
self._webhook = webhook_url
self._client_name = client_name

# Log creation
self._logger.debug('Webhook handler for task %s created with url %s', self._task_name, self._webhook)

# Send a webhook when torrent deletion is successful
def send_success(self, torrent):
self._logger.info("Sending success webhook for torrent: %s", torrent)
fields = [
{
"name": "Torrent",
"value": torrent
}
]
self.send_webhook("Torrent Deleted Successfully", 5091684, fields)

# Send a webhook when torrent deletion fails
def send_failure(self, torrent, reason):
self._logger.info("Sending failure webhook for torrent %s with reason: %s", torrent, reason)
fields = [
{
"name": "Torrent",
"value": torrent
},
{
"name": "Reason",
"value": reason
}
]
self.send_webhook("Torrent Deletion Failed", 11619684, fields)

# Send a webhook with discord embed format
def send_webhook(self, title, color, fields):
payload = {
"username": "autoremove-torrents",
"embeds": [
{
"author": {
"name": self._client_name,
},
"title": title,
"color": color,
"fields":
fields
}
]
}
result = requests.post(self._webhook, json=payload, headers={"Content-Type": "application/json"}, timeout=10)
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
self._logger.error("Failed to deliver webhook: %s", err)
else:
self._logger.info("Webhook delivered successfully with status code %s.", result.status_code)
12 changes: 10 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Just name your task.
No spaces are allowed before the task name.


Part 2: Login Information
Part 2: Login Information and Webhook URL
-------------------------

This part is your login inforamtion.
This part is your login information and optionally a Discord Webhook URL.

For qBittorrent, Transmission or μTorrent
++++++++++++++++++++++++++++++++++++++++++
Expand All @@ -80,6 +80,13 @@ This program accesses Deluge via its RPC protocol.
* ``username``: The username of the Deluge Daemon.
* ``password``: The password of the Deluge Daemon.

Discord Webhook (Optional)
+++++++++++

If you want to receive notifications via Discord Webhook, you can add the following fields:

* ``webhook``: The URL of your Discord Webhook. For example, ``https://discord.com/api/webhooks/123456789/abcdefghijklmnopqrstuvwxyz``.

Example:

.. code-block:: yaml
Expand All @@ -89,6 +96,7 @@ Example:
host: 127.0.0.1:58846
username: localclient
password: 357a0d23f09b9f303f58846e41986b36fef2ac88
webhook: "https://discord.com/api/webhooks/123456789/abcdefghijklmnopqrstuvwxyz" # Optional

.. note::

Expand Down