Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Torbox NZB support #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
35 changes: 20 additions & 15 deletions blackhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from shared.discord import discordError, discordUpdate
from shared.shared import realdebrid, torbox, blackhole, plex, checkRequiredEnvs
from shared.arr import Arr, Radarr, Sonarr
from shared.debrid import TorrentBase, RealDebridTorrent, RealDebridMagnet, TorboxTorrent, TorboxMagnet
from shared.debrid import FileBase, RealDebridTorrent, RealDebridMagnet, TorboxTorrent, TorboxMagnet, TorboxNZB

_print = print

Expand Down Expand Up @@ -41,23 +41,25 @@ def __init__(self, filename, filenameWithoutExt, filePath, filePathProcessing, f
self.folderPathCompleted = folderPathCompleted

class TorrentInfo():
def __init__(self, isTorrentOrMagnet, isDotTorrentFile) -> None:
self.isTorrentOrMagnet = isTorrentOrMagnet
def __init__(self, isTorrentMagnetOrNZB, isDotTorrentFile, isDotNZBFile) -> None:
self.isTorrentMagnetOrNZB = isTorrentMagnetOrNZB
self.isDotTorrentFile = isDotTorrentFile
self.isDotNZBFile = isDotNZBFile

def __init__(self, filename, isRadarr) -> None:
print('filename:', filename)
baseBath = getPath(isRadarr)
uniqueId = str(uuid.uuid4())[:8] # Generate a unique identifier
isDotTorrentFile = filename.casefold().endswith('.torrent')
isTorrentOrMagnet = isDotTorrentFile or filename.casefold().endswith('.magnet')
isDotNZBFile = filename.casefold().endswith('.nzb')
isTorrentMagnetOrNZB = isDotNZBFile or isDotTorrentFile or filename.casefold().endswith('.magnet')
filenameWithoutExt, ext = os.path.splitext(filename)
filePath = os.path.join(baseBath, filename)
filePathProcessing = os.path.join(baseBath, 'processing', f"{filenameWithoutExt}_{uniqueId}{ext}")
folderPathCompleted = os.path.join(baseBath, 'completed', filenameWithoutExt)

self.fileInfo = self.FileInfo(filename, filenameWithoutExt, filePath, filePathProcessing, folderPathCompleted)
self.torrentInfo = self.TorrentInfo(isTorrentOrMagnet, isDotTorrentFile)
self.torrentInfo = self.TorrentInfo(isTorrentMagnetOrNZB, isDotTorrentFile, isDotNZBFile)

def getPath(isRadarr, create=False):
baseWatchPath = blackhole['baseWatchPath']
Expand Down Expand Up @@ -137,13 +139,13 @@ def print(*values: object):

import signal

async def processTorrent(torrent: TorrentBase, file: TorrentFileInfo, arr: Arr) -> bool:
async def processTorrent(torrent: FileBase, file: TorrentFileInfo, arr: Arr) -> bool:
_print = globals()['print']

def print(*values: object):
_print(f"[{torrent.__class__.__name__}] [{file.fileInfo.filenameWithoutExt}]", *values)

if not torrent.submitTorrent():
if not torrent.submitFile():
return False

count = 0
Expand Down Expand Up @@ -179,7 +181,7 @@ def print(*values: object):
while True:
existsCount += 1

folderPathMountTorrent = await torrent.getTorrentPath()
folderPathMountTorrent = await torrent.getFilePath()
if folderPathMountTorrent:
multiSeasonRegex1 = r'(?<=[\W_][Ss]eason[\W_])[\d][\W_][\d]{1,2}(?=[\W_])'
multiSeasonRegex2 = r'(?<=[\W_][Ss])[\d]{2}[\W_][Ss]?[\d]{2}(?=[\W_])'
Expand Down Expand Up @@ -253,7 +255,7 @@ def print(*values: object):
async def processFile(file: TorrentFileInfo, arr: Arr, isRadarr):
try:
_print = globals()['print']

def print(*values: object):
_print(f"[{file.fileInfo.filenameWithoutExt}]", *values)

Expand All @@ -278,16 +280,19 @@ async def is_accessible(path, timeout=10):

time.sleep(.1) # Wait before processing the file in case it isn't fully written yet.
os.renames(file.fileInfo.filePath, file.fileInfo.filePathProcessing)

if file.torrentInfo.isDotNZBFile and not torbox['enabled']:
raise Exception("Cannot process NZB, no NZB client enabled (i.e. Torbox)")

with open(file.fileInfo.filePathProcessing, 'rb' if file.torrentInfo.isDotTorrentFile else 'r') as f:
with open(file.fileInfo.filePathProcessing, 'rb' if file.torrentInfo.isDotTorrentFile or file.torrentInfo.isDotNZBFile else 'r') as f:
fileData = f.read()
f.seek(0)

torrentConstructors = []
if realdebrid['enabled']:
if realdebrid['enabled'] and not file.torrentInfo.isDotNZBFile:
torrentConstructors.append(RealDebridTorrent if file.torrentInfo.isDotTorrentFile else RealDebridMagnet)
if torbox['enabled']:
torrentConstructors.append(TorboxTorrent if file.torrentInfo.isDotTorrentFile else TorboxMagnet)
torrentConstructors.append(TorboxNZB if file.torrentInfo.isDotNZBFile else TorboxTorrent if file.torrentInfo.isDotTorrentFile else TorboxMagnet)

onlyLargestFile = isRadarr or bool(re.search(r'S[\d]{2}E[\d]{2}(?![\W_][\d]{2}[\W_])', file.fileInfo.filename))
if not blackhole['failIfNotCached']:
Expand Down Expand Up @@ -315,7 +320,7 @@ async def is_accessible(path, timeout=10):

discordError(f"Error processing {file.fileInfo.filenameWithoutExt}", e)

async def fail(torrent: TorrentBase, arr: Arr):
async def fail(torrent: FileBase, arr: Arr):
_print = globals()['print']

def print(*values: object):
Expand All @@ -339,7 +344,7 @@ def print(*values: object):
def getFiles(isRadarr):
print('getFiles')
files = (TorrentFileInfo(filename, isRadarr) for filename in os.listdir(getPath(isRadarr)) if filename not in ['processing', 'completed'])
return [file for file in files if file.torrentInfo.isTorrentOrMagnet]
return [file for file in files if file.torrentInfo.isTorrentMagnetOrNZB]

async def on_created(isRadarr):
print("Enter 'on_created'")
Expand All @@ -360,7 +365,7 @@ async def on_created(isRadarr):
if files:
futures.append(asyncio.gather(*(processFile(file, arr, isRadarr) for file in files)))
elif firstGo:
print('No torrent files found')
print('No torrent or NZB files found')
firstGo = False
await asyncio.sleep(1)

Expand Down
2 changes: 1 addition & 1 deletion blackhole_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, is_radarr):
self.path_name = getPath(is_radarr, create=True)

def on_created(self, event):
if not event.is_directory and event.src_path.lower().endswith((".torrent", ".magnet")):
if not event.is_directory and event.src_path.lower().endswith((".torrent", ".magnet", ".nzb")):
asyncio.run(on_created(self.is_radarr))

async def on_run(self):
Expand Down
Loading
Loading