Skip to content

Instantly share code, notes, and snippets.

@omarroth
Last active February 3, 2021 22:55
Show Gist options
  • Save omarroth/b56d4ed4cf6fad017908549f40e60964 to your computer and use it in GitHub Desktop.
Save omarroth/b56d4ed4cf6fad017908549f40e60964 to your computer and use it in GitHub Desktop.
Given list of infohashes, create .torrent files in torrent/ directory
#!/usr/bin/env python3
import libtorrent as lt
import time
import sys
import os
# Timeout for individual torrent (in seconds)
timeout = 600
batch_size = 20
directory = "./torrents"
hashes = open('infohashes.txt').readlines()
hashes.reverse()
ses = lt.session()
ses.listen_on(6881, 6891)
ses.add_dht_router("router.utorrent.com", 6881)
ses.add_dht_router("router.bittorrent.com", 6881)
ses.add_dht_router("dht.transmissionbt.com", 6881)
ses.add_dht_router("router.bitcomet.com", 6881)
ses.add_dht_router("dht.aelitis.com", 6881)
ses.start_dht()
if not os.path.exists(directory):
os.makedirs(directory)
time.sleep(1)
handles = []
size = len(hashes)
while len(handles) != batch_size and hashes != []:
hash = hashes.pop()
info_hash = hash.rstrip('\n')
if info_hash == '':
continue
magnet = "magnet:?xt=urn:btih:" + info_hash
torrent_params = {
'save_path': directory,
'duplicate_is_error': True,
'storage_mode': lt.storage_mode_t(1),
'file_priority': 0,
'file_priorities': [0]*100,
'paused': False,
'auto_managed': True,
'duplicate_is_error': True
}
handle = lt.add_magnet_uri(ses, magnet, torrent_params)
handles.append((handle, 0))
i = 1
print("Downloading metadata... ", end='\r')
sys.stdout.flush()
while handles != []:
for handle, j in handles:
handles.remove((handle, j))
# Unless we've been waiting more than 10 minutes, add it back to the queue
if j > timeout * 10:
size -= 1
ses.remove_torrent(handle, 1)
print("Downloaded metadata for {0}/{1}, most recent : ".format(str(i).rjust(len(str(size)))), end='\r')
continue
if j < timeout * 10:
handles.append((handle, j + 1))
if handle.has_metadata():
torrent_file = open("torrents/{0}.torrent".format(handle.name()), "wb")
torrent_file.write(lt.bencode(handle.write_resume_data()))
torrent_file.close()
ses.remove_torrent(handle, 1)
handles.remove((handle, j + 1))
print("Downloaded metadata for {0}/{1}, most recent : {2}".format(str(i).rjust(len(str(size))), size, handle.info_hash()), end='\r')
sys.stdout.flush()
i += 1
if hashes != []:
# Add new torrent to batch
hash = hashes.pop()
info_hash = hash.rstrip('\n')
if info_hash == '':
continue
magnet = "magnet:?xt=urn:btih:" + info_hash
torrent_params = {
'save_path': directory,
'duplicate_is_error': True,
'storage_mode': lt.storage_mode_t(1),
'file_priority': 0,
'file_priorities': [0]*100,
'paused': False,
'auto_managed': True,
'duplicate_is_error': True
}
handle = lt.add_magnet_uri(ses, magnet, torrent_params)
handles.append((handle, 0))
time.sleep(0.1)
print("")
@TheFrenchGhosty
Copy link

TheFrenchGhosty commented Feb 3, 2021

I've been using this script since you created it for me 2 years ago (thanks a lot by the way, it's SO useful).

However it's currently broken with libtorrent 1.2.X

The torrent files created are missing something when created by 1.2.X.

qBittorrent report: "Error: missing or invalid 'info' section in torrent file"

Are you whiling to fix it?

By the way, if you do, do note that torrent_params needs to be changed to since the others aren't in libtorrent anymore (it can maybe be properly fixed, however not having them never caused any problem):

                torrent_params = {
                'save_path': directory,
                'storage_mode': lt.storage_mode_t(1),
                'file_priorities': [0]*100,
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment