mirror of
https://github.com/StuffAnThings/qbit_manage.git
synced 2024-09-20 07:16:04 +08:00
use logging in torrent_hash_generator
This commit is contained in:
parent
9684bf3e38
commit
99a7984693
|
@ -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}")
|
||||
|
|
Loading…
Reference in a new issue