Skip to content

Commit

Permalink
Update blackhole.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Pukabyte authored Jul 15, 2024
1 parent 0b17d64 commit 46d7804
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions blackhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def print(*values: object):

import signal

async def processTorrent(torrent: TorrentBase, file: TorrentFileInfo, arr: Arr, message_id, parsed_title, file_info, is_movie) -> bool:
async def processTorrent(torrent: TorrentBase, file: TorrentFileInfo, arr: Arr, message_id, parsed_title, file_info, is_movie, debrid_provider, discord_user_tag, tag_on_success) -> bool:
_print = globals()['print']

def print(*values: object):
Expand All @@ -147,9 +147,9 @@ def print(*values: object):

update_message = lambda status: discordUpdate(
title=f"Processing {'Movie' if is_movie else 'Series'}: {parsed_title}",
message=f"{file_info}\n**STATUS**:\nπŸ“ Torrent Cached: {status['cached']}\nπŸ“₯ Added to Debrid: {status['added']}\nπŸ”Ž Found on Mount: {status['mounted']}\nπŸ”— Symlinked: {status['symlinked']}",
message=f"**Debrid Provider:** {debrid_provider}\n{file_info}\n**STATUS:**\nπŸ“ Torrent Cached: {status['cached']}\nπŸ“₯ Added to Debrid: {status['added']}\nπŸ”Ž Found on Mount: {status['mounted']}\nπŸ”— Symlinked: {status['symlinked']}",
color=color,
message_id=message_id
message_id=message_id,
)

status = {
Expand Down Expand Up @@ -246,20 +246,33 @@ def print(*values: object):
await refreshArr(arr, count=3, delay=10)

# Check for the existence of the symlink every 5 seconds
symlink_exists = False
check_count = 0
while check_count < 6:
check_count += 1
arr.refreshMonitoredDownloads()
if os.path.exists(os.path.join(file.fileInfo.folderPathCompleted, relRoot, filename)):
symlink_exists = True
if not os.path.exists(os.path.join(file.fileInfo.folderPathCompleted, relRoot, filename)):
break
await asyncio.sleep(5)

if symlink_exists:
discordUpdate(f"Successfully processed", f"{file.fileInfo.filenameWithoutExt}")
timestamp = datetime.utcnow().isoformat() + "Z"
if check_count < 6:
discordUpdate(
content=discord_user_tag if tag_on_success else None,
title=f"Successfully Processed: {parsed_title}",
message=f"**Debrid Provider:** {debrid_provider}\n{file_info}\n**STATUS:**\nπŸ“ Torrent Cached: {status['cached']}\nπŸ“₯ Added to Debrid: {status['added']}\nπŸ”Ž Found on Mount: {status['mounted']}\nπŸ”— Symlinked: {status['symlinked']}\n✨ Successfully Processed: βœ…",
color=65280, # Green for success
message_id=message_id,
timestamp=timestamp
)
else:
discordError("Symlink check failed", f"{file.fileInfo.filenameWithoutExt}")
discordUpdate(
content=discord_user_tag if tag_on_success else None,
title=f"Successfully Processed: {parsed_title}",
message=f"**Debrid Provider:** {debrid_provider}\n{file_info}\n**STATUS:**\nπŸ“ Torrent Cached: {status['cached']}\nπŸ“₯ Added to Debrid: {status['added']}\nπŸ”Ž Found on Mount: {status['mounted']}\nπŸ”— Symlinked: {status['symlinked']}\n✨ Successfully Processed: βœ…",
color=65280, # Green for success
message_id=message_id,
timestamp=timestamp
)

return True

Expand Down Expand Up @@ -333,34 +346,40 @@ async def is_accessible(path, timeout=10):
torrentConstructors = []
if realdebrid['enabled']:
torrentConstructors.append(RealDebridTorrent if file.torrentInfo.isDotTorrentFile else RealDebridMagnet)
debrid_provider = "Real-Debrid"
if torbox['enabled']:
torrentConstructors.append(TorboxTorrent if file.torrentInfo.isDotTorrentFile else TorboxMagnet)
debrid_provider = "Torbox"

onlyLargestFile = isRadarr or bool(re.search(r'S[\d]{2}E[\d]{2}', file.fileInfo.filename))

# Send initial notification
discord_user = os.getenv('DISCORD_USER_TAG')
discord_user_tag = f"<@{discord_user}>"
tag_on_success = os.getenv('TAG_ON_SUCCESS', 'true').lower() == 'true'
tag_on_failure = os.getenv('TAG_ON_FAILURE', 'true').lower() == 'true'
message_id = discordUpdate(
title=f"Processing Torrent: {parsed_title}",
message=f"{file_info}\n**STATUS**:\nπŸ“ Torrent Cached: β›”\nπŸ“₯ Added to Debrid: β›”\nπŸ”Ž Found on Mount: β›”\nπŸ”— Symlinked: β›”",
color=color
message=f"**Debrid Provider:** {debrid_provider}\n{file_info}\n**STATUS:**\nπŸ“ Torrent Cached: β›”\nπŸ“₯ Added to Debrid: β›”\nπŸ”Ž Found on Mount: β›”\nπŸ”— Symlinked: β›”",
color=color,
)

if not blackhole['failIfNotCached']:
torrents = [constructor(f, fileData, file, blackhole['failIfNotCached'], onlyLargestFile) for constructor in torrentConstructors]
results = await asyncio.gather(*(processTorrent(torrent, file, arr, message_id, parsed_title, file_info, is_movie) for torrent in torrents))
results = await asyncio.gather(*(processTorrent(torrent, file, arr, message_id, parsed_title, file_info, is_movie, debrid_provider, discord_user_tag, tag_on_success) for torrent in torrents))

if not any(results):
for torrent in torrents:
fail(torrent, arr, message_id, file_info, parsed_title, is_movie, color)
fail(torrent, arr, message_id, file_info, parsed_title, is_movie, color, discord_user_tag, debrid_provider, tag_on_failure)
else:
for i, constructor in enumerate(torrentConstructors):
isLast = (i == len(torrentConstructors) - 1)
torrent = constructor(f, fileData, file, blackhole['failIfNotCached'], onlyLargestFile)

if await processTorrent(torrent, file, arr, message_id, parsed_title, file_info, is_movie):
if await processTorrent(torrent, file, arr, message_id, parsed_title, file_info, is_movie, debrid_provider, discord_user_tag, tag_on_success):
break
elif isLast:
fail(torrent, arr, message_id, file_info, parsed_title, is_movie, color)
fail(torrent, arr, message_id, file_info, parsed_title, is_movie, color, discord_user_tag, debrid_provider, tag_on_failure)

os.remove(file.fileInfo.filePathProcessing)
except:
Expand All @@ -371,7 +390,7 @@ async def is_accessible(path, timeout=10):

discordError(f"Error processing {file.fileInfo.filenameWithoutExt}", f"Error:\n```{e}```")

def fail(torrent: TorrentBase, arr: Arr, message_id, file_info, parsed_title, is_movie, color):
def fail(torrent: TorrentBase, arr: Arr, message_id, file_info, parsed_title, is_movie, color, discord_user_tag, debrid_provider, tag_on_failure):
_print = globals()['print']

def print(*values: object):
Expand All @@ -391,11 +410,14 @@ def print(*values: object):
arr.failHistoryItem(item['id'])
print(f"Failed")

timestamp = datetime.utcnow().isoformat() + "Z"
discordUpdate(
title=f"Processing {'Movie' if is_movie else 'Series'}: {parsed_title}",
message=f"{file_info}\n**STATUS**:\nβ›” Failed",
color=color,
message_id=message_id
content=discord_user_tag if tag_on_failure else None,
title=f"Failed To Process: {parsed_title}",
message=f"**Debrid Provider:** {debrid_provider}\n{file_info}\n**STATUS:**\nβ›” Failed",
color=16711680, # Red for failure
message_id=message_id,
timestamp=timestamp
)

def getFiles(isRadarr):
Expand Down

0 comments on commit 46d7804

Please sign in to comment.