32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""mktorrent.py <file> <announce-url> <out.torrent> — a single-file, PRIVATE (no DHT/PEX) v1 torrent, 4 MiB pieces.
|
|
The night's R-1261 rig only; stdlib only."""
|
|
import hashlib, os, sys
|
|
|
|
def benc(x):
|
|
if isinstance(x, int):
|
|
return b"i%de" % x
|
|
if isinstance(x, bytes):
|
|
return b"%d:%s" % (len(x), x)
|
|
if isinstance(x, str):
|
|
return benc(x.encode())
|
|
if isinstance(x, list):
|
|
return b"l" + b"".join(benc(i) for i in x) + b"e"
|
|
if isinstance(x, dict):
|
|
return b"d" + b"".join(benc(k) + benc(v) for k, v in sorted(x.items())) + b"e"
|
|
raise TypeError(type(x))
|
|
|
|
path, announce, out = sys.argv[1:4]
|
|
plen = 4 << 20
|
|
pieces = []
|
|
with open(path, "rb") as f:
|
|
while True:
|
|
b = f.read(plen)
|
|
if not b:
|
|
break
|
|
pieces.append(hashlib.sha1(b).digest())
|
|
info = {"name": os.path.basename(path), "length": os.path.getsize(path), "piece length": plen,
|
|
"pieces": b"".join(pieces), "private": 1}
|
|
open(out, "wb").write(benc({"announce": announce, "info": info, "created by": "felhom CC rig 2026-10-25"}))
|
|
print(out, hashlib.sha1(benc(info)).hexdigest())
|