importlibtorrentasltimporttimeimportsysimportbencodeimporthashlibimportbase64importrandomimporturllib################################################################################### ### This script can download torrents from magnet urls and .torrent files ### It requires two folders to be created beforehand: dwn_folder & trr_folder ### Torrent files should be placed inside the trr_folder. ### To run you just need to pipe the torrent file name (path not required) or ### the magnetic link to this script. E.g.: ### ### $ cat list.txt ### magnet:?xt=urn:btih:LEDGO2NZVVBNULSQQYI4GPL4ISALHBL3&dn=ubuntu-17.04\ ### -desktop-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fa\ ### nounce&xl=1609039872 ### ubuntu-17.04-desktop-amd64.iso.torrent ### $ cat list.txt | xargs -I{} -n 1 -P 4 bash -c "echo '{}' | python t.py" ### ### Note that both magnet url and the torrent file name on the list.txt file. ### Using xargs -P allows performing the downloads in parallel. ### ### You might require to pip install bencode and libtorrent. ### ###################################################################################CHANGE THESE##########dwn_folder='/tmp/'trr_folder='torrents/'#######################link=sys.stdin.readline()ran=random.randint(1,999999)print('Reading line: %s'%(link))if".torrent"inlink:link=link.rstrip()torrent=open(trr_folder+link,'r').read()metadata=bencode.bdecode(torrent)hashcontents=bencode.bencode(metadata['info'])digest=hashlib.sha1(hashcontents).digest()b32hash=base64.b32encode(digest)print('Hash: %s'%(b32hash))params={'dn':metadata['info']['name'],'tr':metadata['announce'],'xl':metadata['info']['length']}printparamsparamstr=urllib.urlencode(params)link='magnet:?xt=urn:btih:%s&%s'%(b32hash,paramstr)print('Magnetic URL: %s'%(link))ses=lt.session()ses.listen_on(6881,6891)params={'save_path':dwn_folder,'storage_mode':lt.storage_mode_t(2),'paused':False,'auto_managed':True,'duplicate_is_error':True}printlinkhandle=lt.add_magnet_uri(ses,link,params)ses.start_dht()print'downloading metadata...'while(nothandle.has_metadata()):time.sleep(1)print'got metadata, starting torrent download...'while(handle.status().state!=lt.torrent_status.seeding):s=handle.status()state_str=['queued','checking','downloading metadata','downloading','finished','seeding','allocating']print('[%d] %.2f%% complete (down: %.2f kb/s up: %.2f kB/s peers: %d) %s%.3f'%(ran,s.progress*100,s.download_rate/1000,s.upload_rate/1000,s.num_peers,state_str[s.state],s.total_download/1000000))time.sleep(5)
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.