Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Last active June 19, 2023 12:26
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save samukasmk/940ca5d5abd9019e8b1af77c819e4ca9 to your computer and use it in GitHub Desktop.
Save samukasmk/940ca5d5abd9019e8b1af77c819e4ca9 to your computer and use it in GitHub Desktop.
Script of example to download files by torrent in Python (By: magnet url)
# source:
# https://stackoverflow.com/questions/6051877/loading-magnet-link-using-rasterbar-libtorrent-in-python
#
# ATTENTION: This is only a example of to use a python bind of torrent library in Python for educational purposes.
# I am not responsible for your download of illegal content or without permission.
# Please respect the laws license permits of your country.
import libtorrent as lt
import time
ses = lt.session()
ses.listen_on(6881, 6891)
params = {
'save_path': '/home/user/Downloads/torrent',
'storage_mode': lt.storage_mode_t(2),
'paused': False,
'auto_managed': True,
'duplicate_is_error': True}
link = "MAGNET_LINK_HERE"
handle = lt.add_magnet_uri(ses, link, params)
ses.start_dht()
print 'downloading metadata...'
while (not handle.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 '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state])
time.sleep(5)
@SnappierSoap318
Copy link

is it possible to import a text file with magnets split by a semi colon or inverter comma and then the script run the whole batch at once?

@martinetmayank
Copy link

is it possible to import a text file with magnets split by a semi colon or inverter comma and then the script run the whole batch at once?

Yes. You will have to open the file, and use readline() function of python.
Just make sure you don't and endline characters while reading (which occurs at the end of the line) and finally run that in a loop!

@anandpskerala
Copy link

How can we install libtorrent?

I have tried this >> pip install libtorrent
Collecting libtorrent
ERROR: Could not find a version that satisfies the requirement libtorrent (from versions: none)
ERROR: No matching distribution found for libtorrent

@martinetmayank
Copy link

sudo apt install python3-libtorrent

This will work

@FlafyDev
Copy link

Is it the same as pip install python3-libtorrent for Windows? Because it doesn't work...

@harrcorr
Copy link

i jus randomly stumbled on this github buuut "pip install python-libtorrent-bin" might work

@FlafyDev
Copy link

pip install python-libtorrent-bin Doesn't exists in Windows.

@harrcorr
Copy link

hav u included pip in your path environment variable

@FlafyDev
Copy link

Yes. Pip usually works for me for downloading modules.
For the record:

C:\Users\flafy>pip install python-libtorrent-bin
ERROR: Could not find a version that satisfies the requirement python-libtorrent-bin (from versions: none)
ERROR: No matching distribution found for python-libtorrent-bin

@harrcorr
Copy link

i thiink? it got removed or somink but here is a stack overflow post that might help if u need it that badly and can be arsed to build from source
https://stackoverflow.com/questions/43478842/installing-libtorrent-for-python-3-6-on-windows-7

@martinetmayank
Copy link

pip install python-libtorrent-bin Doesn't exists in Windows.

yes, you need to compile it in windows as the original code is written in C.

@martinetmayank
Copy link

Yes. Pip usually works for me for downloading modules.
For the record:

C:\Users\flafy>pip install python-libtorrent-bin
ERROR: Could not find a version that satisfies the requirement python-libtorrent-bin (from versions: none)
ERROR: No matching distribution found for python-libtorrent-bin

It works for Linux based OS but I'm not sure about Unix based OS such as Mac

@ShashiTharoor
Copy link

pip install libtorrent is successful
But still it is unable to find libtorrent

@Dryrtan
Copy link

Dryrtan commented Dec 10, 2021

Would it be possible to replace the magnet link with a .torrent? How could I do that?

@mffseal
Copy link

mffseal commented Apr 12, 2022

Would it be possible to replace the magnet link with a .torrent? How could I do that?

ses = lt.session({'listen_interfaces': '0.0.0.0:6881'})
    # look here, link is one of .torrent file's path
    info = lt.torrent_info(link)
    h = ses.add_torrent({'ti':info, 'save_path':'/Downloads'})
    s = h.status()
    print('starting', s.name)

    while not s.is_seeding:
        s = h.status()

        print('\r%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s' % (
            s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000,
            s.num_peers, s.state), end=' ')

        alerts = ses.pop_alerts()
        for a in alerts:
            if a.category() & lt.alert.category_t.error_notification:
                print(a)
        sys.stdout.flush()
        time.sleep(1)
    print(h.status().name, 'complete')

@SC-DO
Copy link

SC-DO commented Aug 18, 2022

Is it possible to pass a proxy configuration for the download?

@mikebilly
Copy link

can I view the files in the big torrent, and select specific files to download, instead of downloading everything?

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