Question

I have a magnet link from a torrent site that should open this program called transmission.How do i open it with Python?

i'm on ubuntu btw. I heard this thing called xdg-open can do the trick but how do i use it for opening the magnet link?

and if that's not the code i'm looking for, what should i use to run the magnet link?

Was it helpful?

Solution

Taking a look at the command line arguments of transmission-gtk helps:

$ transmission-gtk --help

Usage: transmission-gtk [OPTION...] [torrent files or urls]

A quick and dirty approach for a python solution is using the os module:

import os
os.system("transmission-gtk urlhere")

A much better and more sophisticated way to do such calls to external programms is using the subprocess module. A few more examples can be found under python - how to create a subprocess?.

xdg-open works pretty much the same way. But instead of calling the transmission client directly, it chooses the preferred Torrent application (preferred in this case means default application, which can be set by using the default applications menu in the Ubuntu system settings). Repeatedly pointing you at the help texts given by calling the programm from commandline, it might be interesting to inspect the exit codes of xdg-open:

$ xdg-open --manual

...

1 Error in command line syntax.

2 One of the files passed on the command line did not exist.

3 A required tool could not be found.

4 The action failed.

OTHER TIPS

The below code sums up the method to download on all the operating systems.

  import subprocess , os , sys

  def open_magnet(magnet):
        """Open magnet according to os."""
        if sys.platform.startswith('linux'):
            subprocess.Popen(['xdg-open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        elif sys.platform.startswith('win32'):
            os.startfile(magnet)
        elif sys.platform.startswith('cygwin'):
            os.startfile(magnet)
        elif sys.platform.startswith('darwin'):
            subprocess.Popen(['open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        else:
            subprocess.Popen(['xdg-open', magnet],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top