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

[WIP] Added extra logging for troubleshooting #16

Closed
wants to merge 3 commits into from
Closed
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
39 changes: 18 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@


def cli_entrypoint(args):
try:
# using input_file means this is probably running as a script and extra printing wouldn't be appreciated
should_print = args.input_directory or args.server
config = command_log_wrapper("Reading config file:", should_print, lambda: Config().load(args.config_file))

if config.inject_torrents:
injector = command_log_wrapper("Connecting to torrent client:", should_print, lambda: Injection(config).setup())
else:
injector = None

red_api, ops_api = command_log_wrapper("Verifying API keys:", should_print, lambda: __verify_api_keys(config))

if args.server:
run_webserver(args.input_directory, args.output_directory, red_api, ops_api, injector, port=config.server_port)
elif args.input_file:
print(scan_torrent_file(args.input_file, args.output_directory, red_api, ops_api, injector))
elif args.input_directory:
print(scan_torrent_directory(args.input_directory, args.output_directory, red_api, ops_api, injector))
except Exception as e:
print(f"{Fore.RED}{str(e)}{Fore.RESET}")
exit(1)
# using input_file means this is probably running as a script and extra printing wouldn't be appreciated
should_print = args.input_directory or args.server
config = command_log_wrapper("Reading config file:", should_print, lambda: Config().load(args.config_file))

if config.inject_torrents:
injector = command_log_wrapper("Connecting to torrent client:", should_print, lambda: Injection(config).setup())
else:
injector = None

red_api, ops_api = command_log_wrapper("Verifying API keys:", should_print, lambda: __verify_api_keys(config))

if args.server:
run_webserver(args.input_directory, args.output_directory, red_api, ops_api, injector, port=config.server_port)
elif args.input_file:
print(scan_torrent_file(args.input_file, args.output_directory, red_api, ops_api, injector))
elif args.input_directory:
print(scan_torrent_directory(args.input_directory, args.output_directory, None, None, injector))



def __verify_api_keys(config):
Expand Down
27 changes: 24 additions & 3 deletions src/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,29 @@ def scan_torrent_directory(
`FileNotFoundError`: if the input directory does not exist.
"""

print(f"assert_path_exists called with {input_directory}")
input_directory = assert_path_exists(input_directory)
print(f"assert_path_exists returned {input_directory}")

print(f"mkdir_p called with {output_directory}")
output_directory = mkdir_p(output_directory)
print(f"mkdir_p returned {output_directory}")

print(f"list_files_of_extension called with {input_directory}, '.torrent'")
input_torrents = list_files_of_extension(input_directory, ".torrent")
print(f"list_files_of_extension returned {len(input_torrents)} torrents")

print(f"list_files_of_extension called with {output_directory}, '.torrent'")
output_torrents = list_files_of_extension(output_directory, ".torrent")
print(f"list_files_of_extension returned {len(output_torrents)} torrents")

print("__collect_infohashes_from_files called")
input_infohashes = __collect_infohashes_from_files(input_torrents)
print(f"__collect_infohashes_from_files returned {len(input_infohashes)} infohashes")

print("__collect_infohashes_from_files called")
output_infohashes = __collect_infohashes_from_files(output_torrents)
print(f"__collect_infohashes_from_files returned {len(output_infohashes)} infohashes")

p = Progress(len(input_torrents))

Expand Down Expand Up @@ -147,14 +163,19 @@ def scan_torrent_directory(

def __collect_infohashes_from_files(files: list[str]) -> dict:
infohash_dict = {}

counter = 0
for filepath in files:
counter += 1
try:
torrent_data = get_bencoded_data(filepath)

if torrent_data:
infohash = calculate_infohash(torrent_data)
infohash_dict[infohash] = filepath
try:
infohash = calculate_infohash(torrent_data)
infohash_dict[infohash] = filepath
except KeyError as e:
print(f'KeyError: no "info" key in {filepath} (processed {counter} files)')
raise e
except UnicodeDecodeError:
continue

Expand Down
Loading