Python 3: Opening A Magnet Link Contained In A Variable
Asked Answered
V

3

6

I have a magnet link (e.g.: magnet:?xt=urn:btih:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6) contained in a variable as a string and would like the script to open the default program that handles magnet links so that it starts downloading the torrent (like if I opened a magnet link from within my file manager).

For the sake of making answers clear we will say that we have the magnet link in a variable called magnet_link.

Vasilikivasilis answered 19/2, 2012 at 16:28 Comment(1)
I am on Windows myself, but if you could give me the command(s) for Mac and Linux also and then I can use os.name or some equivalent to find the operating system so that the right command can be used.Vasilikivasilis
H
11

On Windows you can use os.startfile:

os.startfile(magnet_link)

For Mac/OSX you could probably use applescript and pipe it to osascript, for Linux you might be able to use xdg-open.

Heavyduty answered 19/2, 2012 at 16:42 Comment(11)
I get the following error: WindowsError: [Error -2147217406] Windows Error 0x%X: 'magent:?xt=urn:btich:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6'Vasilikivasilis
Which OS? Are you sure you have an application associated with the magnet protocol?Heavyduty
This is on Windows 7 - I'm pretty sure that I have uTorrent associated with the magnet protocol, but how could I check this?Vasilikivasilis
For completeness: On Linux, you can probably call the xdg-open command with it.Sivan
@EdenCrow Interestingly the magnet link in your question does seem to throw that error for me, however a real magnet link seems to work fine. I haven't tested extensively, though.Heavyduty
@zeekay Could you give an example of a "real" magnet link? The one I put in the question is a real magnet link and should result to some Ubuntu installation. Is there some information I haven't included/should include? The problem may be in the way I have generated the magnet link itself rather than the solution you have provided.Vasilikivasilis
For instance consider this magnet link for OpenOffice: magnet:?xt=urn:btih:bb372b915a4b057b58264847e56407f7a531f369&dn=OpenOffice+3.1.1+%28Language+Pack+Only%29+Linux+%28x86%29+English+%28en-GB%29&tr=http://borft.student.utwente.nl:6969/announceHeavyduty
@EdenCrow: Try magnet:... instead of magent:...Fistulous
Also it should be magnet:?xt=urn:btih not btich.Heavyduty
AndiDog: Thanks - it's always the little typos that are the worst! @zeekay That works. Thanks.Vasilikivasilis
how to exactly use the xdg-open for ubuntu to open magnet links?Permission
A
3

Here is a small code snippet that sums up the method on all the operating systems

  import sys , subprocess
  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)
Armful answered 28/11, 2017 at 8:14 Comment(0)
G
1

On the mac, if you have an installed app that will handle it, just pass the link to the open command

open "some url"

Using something from subprocess i would imagine

Georgenegeorges answered 29/2, 2012 at 13:2 Comment(1)
Thinking more, the open command will let you specify an application too.Georgenegeorges

© 2022 - 2024 — McMap. All rights reserved.