Created
January 7, 2017 14:03
-
-
Save itorres/467ec280f9c480648ebcbe5d843047ad to your computer and use it in GitHub Desktop.
This script sorts a directory full of torrents into subdirectories per tracker.
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
#!/usr/bin/env python3.5 | |
import os | |
from urllib.parse import urlparse | |
def read_header(h): | |
assert(h[:11] == b'd8:announce') | |
ann_head_end = h.index(b':',11) | |
announce_length = int(h[11:ann_head_end]) | |
announce_start = ann_head_end + 1 | |
announce_end = announce_start + announce_length | |
announce = h[announce_start:announce_end] | |
return announce.decode('utf8') | |
def get_tracker(f): | |
with(open(f, "rb")) as fh: | |
header = fh.read(1024) | |
announce = read_header(header) | |
tracker = urlparse(announce).netloc.split(':')[0] | |
return tracker | |
for f in os.scandir('.'): | |
if f.is_file() and '.torrent' in f.name: | |
tracker = get_tracker(f.name) | |
os.makedirs(tracker, exist_ok=True) | |
dst = os.path.join(tracker, f.name) | |
os.rename(f.name, dst) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment