Last active
May 24, 2022 14:51
-
-
Save x1ppy/58c0427c2a9df8d81a2cefb6287e4f7e to your computer and use it in GitHub Desktop.
Create origin files from existing .torrents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bencoding | |
import hashlib | |
import io | |
import os | |
import sys | |
import time | |
from origin import GazelleAPI | |
# Before running this script: | |
# pip install bencoding | |
# pip install git+https://github.com/x1ppy/gazelle-origin | |
# Change to the input/output paths you want to use. | |
in_dir = 'torrents' | |
out_dir = 'origins' | |
def hash_from_torrent(file): | |
with open(file, 'rb') as f: | |
data = f.read() | |
data = bencoding.bdecode(data) | |
info = data[b'info'] | |
return hashlib.sha1(bencoding.bencode(info)).hexdigest() | |
cookie = os.environ.get('RED_COOKIE') | |
if not cookie: | |
print('RED_COOKIE environment variable not set.', file=sys.stderr) | |
sys.exit(ERR_COOKIE) | |
api = GazelleAPI(cookie) | |
for filename in os.listdir(in_dir): | |
if not filename.endswith('.torrent'): | |
continue | |
print(filename) | |
basename = os.path.basename(filename)[:-8] | |
outfile = os.path.join(out_dir, basename + '.txt') | |
if os.path.exists(outfile): | |
print('...exists, skipping') | |
continue | |
hash = hash_from_torrent(os.path.join(in_dir, filename)) | |
info = api.get_torrent_info(hash) | |
with io.open(outfile, 'w', encoding='utf-8') as f: | |
f.write(info) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment