|
1 | 1 | import base64 |
2 | 2 | import hashlib |
3 | 3 | from urllib.parse import quote |
| 4 | +import logging |
4 | 5 |
|
5 | 6 | import bencodepy |
6 | 7 |
|
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +def calculate_v2_hash(info_dict): |
| 12 | + """计算BitTorrent v2 hash""" |
| 13 | + # 检查是否为v2种子或混合种子 |
| 14 | + if b"meta version" in info_dict and info_dict[b"meta version"] == 2: |
| 15 | + # 对于v2种子,使用SHA256 |
| 16 | + encoded_info = bencodepy.encode(info_dict) |
| 17 | + return hashlib.sha256(encoded_info).digest() |
| 18 | + |
| 19 | + # 检查是否存在v2特有的字段 |
| 20 | + if b"file tree" in info_dict: |
| 21 | + # 这是一个混合种子,计算v2 hash |
| 22 | + encoded_info = bencodepy.encode(info_dict) |
| 23 | + return hashlib.sha256(encoded_info).digest() |
| 24 | + |
| 25 | + return None |
| 26 | + |
7 | 27 |
|
8 | 28 | async def torrent_to_link(torrent_file): |
9 | | - torrent_info = bencodepy.decode(torrent_file) |
| 29 | + try: |
| 30 | + logger.debug( |
| 31 | + f"torrent_to_link: Processing torrent file, type: {type(torrent_file)}, size: {len(torrent_file) if hasattr(torrent_file, '__len__') else 'unknown'}" |
| 32 | + ) |
10 | 33 |
|
11 | | - # 获取 info 字段并进行 bencode 编码 |
12 | | - info = torrent_info[b"info"] |
13 | | - encoded_info = bencodepy.encode(info) |
| 34 | + torrent_info = bencodepy.decode(torrent_file) |
| 35 | + logger.debug( |
| 36 | + f"torrent_to_link: Successfully decoded torrent, keys: {list(torrent_info.keys())}" |
| 37 | + ) |
14 | 38 |
|
15 | | - # 计算 info_hash (SHA1 hash of the encoded info dictionary) |
16 | | - info_hash = hashlib.sha1(encoded_info).digest() |
| 39 | + # 获取 info 字段并进行 bencode 编码 |
| 40 | + info = torrent_info[b"info"] |
| 41 | + encoded_info = bencodepy.encode(info) |
17 | 42 |
|
18 | | - # 将 hash 转换为磁力链接格式 |
19 | | - info_hash_hex = base64.b16encode(info_hash).decode("utf-8").lower() |
| 43 | + # 计算 info_hash (SHA1 hash of the encoded info dictionary) |
| 44 | + info_hash_v1 = hashlib.sha1(encoded_info).digest() |
| 45 | + info_hash_hex_v1 = base64.b16encode(info_hash_v1).decode("utf-8").lower() |
| 46 | + logger.debug(f"torrent_to_link: V1 hash calculated: {info_hash_hex_v1}") |
20 | 47 |
|
21 | | - # # 获取文件名 |
22 | | - # name = torrent_info.get(b"info", {}).get(b"name", b"").decode("utf-8") |
| 48 | + # 尝试计算v2 hash |
| 49 | + info_hash_v2 = calculate_v2_hash(info) |
23 | 50 |
|
24 | | - # 构建磁力链接 |
25 | | - magnet_link = f"magnet:?xt=urn:btih:{info_hash_hex}" |
26 | | - # if name: |
27 | | - # magnet_link += f"&dn={quote(name)}" |
| 51 | + # 如果是混合种子或v2种子,优先使用v2 hash |
| 52 | + if info_hash_v2: |
| 53 | + info_hash_hex_v2 = base64.b16encode(info_hash_v2).decode("utf-8").lower() |
| 54 | + # V2 hash 使用 btmh (BitTorrent Meta Hash) 格式,1220表示SHA256 |
| 55 | + magnet_link = f"magnet:?xt=urn:btmh:1220{info_hash_hex_v2}" |
| 56 | + logger.debug(f"torrent_to_link: Using V2 hash: {info_hash_hex_v2}") |
| 57 | + else: |
| 58 | + # 使用v1 hash |
| 59 | + magnet_link = f"magnet:?xt=urn:btih:{info_hash_hex_v1}" |
| 60 | + logger.debug(f"torrent_to_link: Using V1 hash: {info_hash_hex_v1}") |
28 | 61 |
|
29 | | - # 添加 trackers (可选) |
30 | | - if b"announce" in torrent_info: |
31 | | - tracker = torrent_info[b"announce"].decode("utf-8") |
32 | | - magnet_link += f"&tr={quote(tracker)}" |
| 62 | + # # 获取文件名 |
| 63 | + # name = torrent_info.get(b"info", {}).get(b"name", b"").decode("utf-8") |
| 64 | + # if name: |
| 65 | + # magnet_link += f"&dn={quote(name)}" |
33 | 66 |
|
34 | | - if b"announce-list" in torrent_info: |
35 | | - for tracker_list in torrent_info[b"announce-list"]: |
36 | | - tracker = tracker_list[0].decode("utf-8") |
| 67 | + # 添加 trackers (可选) |
| 68 | + if b"announce" in torrent_info: |
| 69 | + tracker = torrent_info[b"announce"].decode("utf-8") |
37 | 70 | magnet_link += f"&tr={quote(tracker)}" |
38 | | - return magnet_link |
| 71 | + |
| 72 | + if b"announce-list" in torrent_info: |
| 73 | + for tracker_list in torrent_info[b"announce-list"]: |
| 74 | + tracker = tracker_list[0].decode("utf-8") |
| 75 | + magnet_link += f"&tr={quote(tracker)}" |
| 76 | + |
| 77 | + logger.debug(f"torrent_to_link: Generated magnet link: {magnet_link}") |
| 78 | + return magnet_link |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + logger.error(f"torrent_to_link: Error processing torrent file: {e}") |
| 82 | + logger.error(f"torrent_to_link: Torrent file type: {type(torrent_file)}") |
| 83 | + if hasattr(torrent_file, "__len__"): |
| 84 | + logger.error(f"torrent_to_link: Torrent file size: {len(torrent_file)}") |
| 85 | + if isinstance(torrent_file, bytes) and len(torrent_file) > 0: |
| 86 | + logger.error(f"torrent_to_link: First 50 bytes: {torrent_file[:50]}") |
| 87 | + return "" |
| 88 | + |
| 89 | + |
| 90 | +async def get_torrent_hashes(torrent_file): |
| 91 | + """获取种子的所有可能hash (v1和v2)""" |
| 92 | + try: |
| 93 | + logger.debug(f"get_torrent_hashes: Processing torrent file") |
| 94 | + |
| 95 | + torrent_info = bencodepy.decode(torrent_file) |
| 96 | + info = torrent_info[b"info"] |
| 97 | + encoded_info = bencodepy.encode(info) |
| 98 | + |
| 99 | + # V1 hash (SHA1) |
| 100 | + v1_hash = hashlib.sha1(encoded_info).digest() |
| 101 | + v1_hash_hex = base64.b16encode(v1_hash).decode("utf-8").lower() |
| 102 | + logger.debug(f"get_torrent_hashes: V1 hash: {v1_hash_hex}") |
| 103 | + |
| 104 | + hashes = {"v1": v1_hash_hex} |
| 105 | + |
| 106 | + # V2 hash (SHA256) |
| 107 | + v2_hash = calculate_v2_hash(info) |
| 108 | + if v2_hash: |
| 109 | + v2_hash_hex = base64.b16encode(v2_hash).decode("utf-8").lower() |
| 110 | + hashes["v2"] = v2_hash_hex |
| 111 | + logger.debug(f"get_torrent_hashes: V2 hash: {v2_hash_hex}") |
| 112 | + else: |
| 113 | + logger.debug( |
| 114 | + "get_torrent_hashes: No V2 hash found, this is a V1-only torrent" |
| 115 | + ) |
| 116 | + |
| 117 | + return hashes |
| 118 | + |
| 119 | + except Exception as e: |
| 120 | + logger.error(f"get_torrent_hashes: Error processing torrent file: {e}") |
| 121 | + return {"v1": ""} |
0 commit comments