use logging in torrent_hash_generator

This commit is contained in:
bobokun 2023-05-22 16:16:38 -04:00
parent 9684bf3e38
commit 99a7984693
No known key found for this signature in database
GPG key ID: B73932169607D927

View file

@ -2,6 +2,11 @@ import hashlib
import bencodepy
from modules import util
from modules.util import Failed
logger = util.logger
class TorrentHashGenerator:
def __init__(self, torrent_file_path):
@ -11,15 +16,15 @@ class TorrentHashGenerator:
try:
with open(self.torrent_file_path, "rb") as torrent_file:
torrent_data = torrent_file.read()
try:
torrent_info = bencodepy.decode(torrent_data)
info_data = bencodepy.encode(torrent_info[b"info"])
info_hash = hashlib.sha1(info_data).hexdigest()
logger.trace(f"info_hash: {info_hash}")
return info_hash
except KeyError:
raise ValueError("Invalid .torrent file format. 'info' key not found.")
logger.error("Invalid .torrent file format. 'info' key not found.")
except FileNotFoundError:
raise FileNotFoundError(f"Torrent file '{self.torrent_file_path}' not found.")
except Exception as e:
raise Exception(f"Error: {e}")
logger.error(f"Torrent file '{self.torrent_file_path}' not found.")
except Failed as err:
logger.error(f"TorrentHashGenerator Error: {err}")