-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from Baconing/pr-discord_webhook
[discord_webhook] 1.0.0
- Loading branch information
Showing
6 changed files
with
802 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/__pycache__ | ||
*.py[cod] | ||
**/site-packages | ||
settings.json |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**1.0.0** | ||
- initial release |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"author": "Baconing", | ||
"compatibility": [ | ||
2 | ||
], | ||
"description": "Send a Discord webhook notification when a worker starts and finishes processing a file.", | ||
"icon": "https://cdn.prod.website-files.com/6257adef93867e50d84d30e2/636e0a6a49cf127bf92de1e2_icon_clyde_blurple_RGB.png", | ||
"id": "discord_webhook", | ||
"name": "Discord Webhook", | ||
"platform": [ | ||
"all" | ||
], | ||
"priorities": { | ||
"on_worker_process": "1", | ||
"on_postprocessor_task_results": "99" | ||
}, | ||
"tags": "discord,wekbook,notification", | ||
"version": "1.0.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Written by: Brenden "Baconing" Freier <[email protected]> | ||
Date: 14 Jan 2025 | ||
Copyright: | ||
Copyright (C) 2025 Brenden "Baconing" Freier | ||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General | ||
Public License as published by the Free Software Foundation, version 3. | ||
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
for more details. | ||
You should have received a copy of the GNU General Public License along with this program. | ||
If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
import logging | ||
from discord_webhook import DiscordEmbed, DiscordWebhook | ||
from unmanic.libs.unplugins.settings import PluginSettings | ||
|
||
logger = logging.getLogger("Unmanic.Plugin.discord_webhook") | ||
|
||
class Settings(PluginSettings): | ||
settings = { | ||
"Webhook URL": "", | ||
"Webhook Username": "", | ||
"Webhook Avatar URL": "", | ||
"Display Absolute Paths?": False, | ||
"Ping Everyone?": False | ||
} | ||
|
||
def on_worker_process(data): | ||
logger.info("Sending notification to webhook for a started task.") | ||
settings = Settings(library_id=data['library_id']) | ||
|
||
webhookUrl = settings.get_setting("Webhook URL") | ||
absolutePaths = settings.get_setting("Display Absolute Paths?") | ||
pingEveryone = settings.get_setting("Ping Everyone?") | ||
username = settings.get_setting("Webhook Username") | ||
avatarUrl = settings.get_setting("Webhook Avatar URL") | ||
|
||
file = data["file_in"] if absolutePaths else data["file_in"].split("/")[-1] | ||
|
||
msg = DiscordWebhook(webhookUrl) | ||
msg.content = "@everyone" if pingEveryone else None | ||
msg.username = username if len(username) > 0 else None | ||
msg.avatar_url = avatarUrl if len(avatarUrl) > 0 else None | ||
|
||
embed = DiscordEmbed("Task Started", "A file processing task has begun.") | ||
embed.set_color(0xFFFF00) | ||
embed.add_embed_field("File", "```{}```".format(file), False) | ||
|
||
msg.add_embed(embed) | ||
|
||
resp = msg.execute() | ||
if not resp.ok: | ||
logger.error("Got failed status code %d from Discord: %s", resp.status_code, resp.json()) | ||
resp.raise_for_status() | ||
|
||
|
||
return data | ||
|
||
def on_postprocessor_task_results(data): | ||
settings = Settings(library_id=data['library_id']) | ||
|
||
webhookUrl = settings.get_setting("Webhook URL") | ||
absolutePaths = settings.get_setting("Display Absolute Paths?") | ||
pingEveryone = settings.get_setting("Ping Everyone?") | ||
username = settings.get_setting("Webhook Username") | ||
avatarUrl = settings.get_setting("Webhook Avatar URL") | ||
|
||
destination_files = ''.join(file if absolutePaths else file.split("/")[-1] for file in data["destination_files"]) | ||
source_file = data["source_data"]["abspath"] if absolutePaths else data["source_data"]["basename"] | ||
|
||
msg = DiscordWebhook(webhookUrl) | ||
msg.content = "@everyone" if pingEveryone else None | ||
msg.username = username if len(username) > 0 else None | ||
msg.avatar_url = avatarUrl if len(avatarUrl) > 0 else None | ||
|
||
if (data["task_processing_success"] and data["file_move_processes_success"]): | ||
embed = DiscordEmbed("Task Completed", "A file processing task successfully completed.") | ||
embed.set_color(0x00FF00) | ||
else: | ||
embed = DiscordEmbed("Task Failed", "A file processing task failed during {}.".format("processing" if data["task_processing_success"] else "file movement")) | ||
embed.set_color(0xFF0000) | ||
|
||
embed.add_embed_field("File(s) Created", "```{}```".format(destination_files if len(destination_files) > 0 else "None"), False) | ||
embed.add_embed_field("Original (Source) File", "```{}```".format(source_file), False) | ||
|
||
msg.add_embed(embed) | ||
|
||
resp = msg.execute() | ||
if not resp.ok: | ||
logger.error("Got failed status code %d from Discord: %s", resp.status_code, resp.json()) | ||
resp.raise_for_status() | ||
|
||
return data |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
discord-webhook>=1.3.1 |