-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhashing.py
More file actions
39 lines (33 loc) · 1.24 KB
/
hashing.py
File metadata and controls
39 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# hashing.py - Torrent hash calculation utilities
import hashlib
import bencodepy
import httpx
def calculate_torrent_hash_from_bytes(torrent_bytes: bytes) -> str | None:
"""
Calculates a torrent info hash from raw .torrent bytes.
"""
try:
torrent_data = bencodepy.decode(torrent_bytes)
if b'info' not in torrent_data:
return None
bencoded_info = bencodepy.encode(torrent_data[b'info'])
return hashlib.sha1(bencoded_info).hexdigest()
except Exception:
return None
async def calculate_torrent_hash_from_url(url: str) -> str | None:
"""
Downloads a .torrent file from the given URL and calculates its info hash.
Args:
url: The URL to download the .torrent file from
Returns:
The SHA1 hash of the torrent's info dictionary, or None if failed
"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(url, timeout=10)
response.raise_for_status()
return calculate_torrent_hash_from_bytes(response.content)
except Exception as e:
# Note: We can't use app.logger here since this is a separate module
# The calling code should handle logging
return None