Search code examples
pythonhyperlinktransmissionutorrent

Open magnet links on Ubuntu with python


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?


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.