Skip to content

Commit

Permalink
use scandir_rec_simple instead of rglob() to catch and handle individ…
Browse files Browse the repository at this point in the history
…ual file or directory errors

support multiple torrent dirs at once
  • Loading branch information
Dobatymo committed Jun 17, 2022
1 parent 03f5efa commit a06d88c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ line_length = 120

[tool.mypy]
ignore_missing_imports = true
warn_unused_configs = true
30 changes: 18 additions & 12 deletions qbtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import qbittorrentapi
import requests
from genutility.args import is_dir
from genutility.filesystem import scandir_rec_simple
from genutility.iter import batch, retrier
from genutility.json import json_lines
from genutility.time import TakeAtleast
Expand Down Expand Up @@ -240,9 +241,9 @@ def __len__(self) -> int:
def _build_inverse_tree(basepath: Path) -> InversePathTree:

tree = InversePathTree()
for path in basepath.rglob("*"):
if path.is_file():
tree.add(path, path.stat().st_size)
for entry in scandir_rec_simple(fspath(basepath)):
if entry.is_file():
tree.add(Path(entry.path), entry.stat().st_size)

return tree

Expand All @@ -258,14 +259,16 @@ def _load_torrent_info(path: str) -> Optional[dict]:

def find_torrents(client, args) -> None:
infos = {}
for file in args.torrents_dir.rglob("*.torrent"):
info = _load_torrent_info(file)
if info is None:
continue
for dir in args.torrents_dirs:
for file in dir.rglob("*.torrent"):
info = _load_torrent_info(file)
if info is None:
continue

infos[fspath(file)] = info
infos[fspath(file)] = info

logging.info("Loaded %s torrents from <%s>", len(infos), args.torrents_dir)
dirs = ", ".join(f"<{dir}>" for dir in args.torrents_dirs)
logging.info("Loaded %s torrents from %s", len(infos), dirs)

for dir in args.data_dirs:
invtree = _build_inverse_tree(dir)
Expand All @@ -289,16 +292,19 @@ def find_torrents(client, args) -> None:
logging.debug("Found path, but no size matches for %s", info)
elif len(meta_matches) == 1:
full_path = meta_matches[0] / single_file
assert full_path.exists()
if not full_path.exists():
logging.error("File does not exist: <%s>", full_path)
continue

print(f"Found possible match for {torrent_file}: {full_path}")
num_add_try += 1
if args.do_add:
result = client.torrents_add(
torrent_files=torrent_file,
save_path=fspath(meta_matches[0]),
is_skip_checking=False,
is_paused=False,
)
num_add_try += 1
if result == "Fails.":
logging.error("Failed to add %s", torrent_file)
num_add_fail += 1
Expand Down Expand Up @@ -371,7 +377,7 @@ def find_torrents(client, args) -> None:
parser_g = subparsers.add_parser(
"find-torrents", help="Delete torrents files from directory if they are already loaded in qBittorrent"
)
parser_g.add_argument("--torrents-dir", type=is_dir, help="Directory with torrent files", required=True)
parser_g.add_argument("--torrents-dirs", nargs="+", type=is_dir, help="Directory with torrent files", required=True)
parser_g.add_argument(
"--data-dirs", nargs="+", type=is_dir, help="Directory to look for torrent data", required=True
)
Expand Down

0 comments on commit a06d88c

Please sign in to comment.