Skip to content

Instantly share code, notes, and snippets.

@elig0n
Created June 29, 2019 07:55
Show Gist options
  • Save elig0n/fd610d606c2db01e20b420ab2ced1592 to your computer and use it in GitHub Desktop.
Save elig0n/fd610d606c2db01e20b420ab2ced1592 to your computer and use it in GitHub Desktop.
Fetch torrents names from magnet URLs
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Description- Fetch torrents names from magnet URLs
# Usage- Supply a filename argument containing magnet URLS or use '-' for stdin
import sys
import time
import libtorrent as lt
IFACE = "0.0.0.0"
PORT = "6881"
if (len(sys.argv) != 2):
sys.stderr.write("Usage: {} filename/-\n".format(sys.argv[0]))
sys.exit(1)
if (sys.argv[1] == "-"):
r = sys.stdin.readlines()
else:
f=open(sys.argv[1], "r")
r=f.readlines()
sess = lt.session({'listen_interfaces': IFACE + ':' + PORT})
spinner = ['-', '\\', '|', '/']
spinner_pos = 0
for line in r:
if line == '\n':
continue
line=line.rstrip()
h = sess.add_torrent({
'save_path': "./" ,
'info_hash': lt.parse_magnet_uri(line)['info_hash']
})
h.prioritize_files([0]*1000)
s = h.status()
while True:
try:
print("\r" + h.torrent_file().name())
break
except:
sys.stdout.write("\r" +
spinner[spinner_pos % len(spinner)])
sys.stdout.flush()
time.sleep(0.05)
spinner_pos = spinner_pos + 1
continue
spinner_pos = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment