Files
torrents/Seedbox/50 PrintFilenamesFromTorrentFiles.py
2026-03-03 14:27:37 +01:00

95 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pymysql
import bencodepy
# ============================================================
# DB CONFIG UPRAV
# ============================================================
DB_CONFIG = {
"host": "192.168.1.50",
"user": "root",
"password": "Vlado9674+",
"database": "torrents",
"charset": "utf8mb4",
"cursorclass": pymysql.cursors.SSCursor # streaming
}
# ============================================================
# HELPERS
# ============================================================
def decode_if_bytes(value):
if isinstance(value, bytes):
return value.decode("utf-8", errors="replace")
return value
def parse_torrent(blob):
data = bencodepy.decode(blob)
info = data[b"info"]
torrent_name = decode_if_bytes(info[b"name"])
files = []
# multi-file torrent
if b"files" in info:
for f in info[b"files"]:
path = "/".join(decode_if_bytes(p) for p in f[b"path"])
length = f[b"length"]
files.append((path, length))
# single file torrent
else:
length = info[b"length"]
files.append((torrent_name, length))
return torrent_name, files
# ============================================================
# MAIN
# ============================================================
def main():
conn = pymysql.connect(**DB_CONFIG)
with conn.cursor() as cursor:
cursor.execute("""
SELECT id, title_visible, torrent_content
FROM torrents
WHERE torrent_content IS NOT NULL
""")
for row in cursor:
torrent_id = row[0]
title_visible = row[1]
blob = row[2]
try:
name, files = parse_torrent(blob)
print("=" * 70)
print(f"DB ID : {torrent_id}")
print(f"Title visible : {title_visible}")
print(f"Torrent name : {name}")
print("Files:")
for f, size in files:
print(f" - {f} ({size} bytes)")
except Exception as e:
print(f"ERROR parsing torrent ID {torrent_id}: {e}")
conn.close()
if __name__ == "__main__":
main()