5

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.

AndiDog
  • 68,631
  • 21
  • 159
  • 205
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
  • 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. – Eden Crow Feb 19 '12 at 16:32

3 Answers3

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.

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
  • I get the following error: `WindowsError: [Error -2147217406] Windows Error 0x%X: 'magent:?xt=urn:btich:1c1b9f5a3b6f19d8dbcbab5d5a43a6585e4a7db6'` – Eden Crow Feb 19 '12 at 16:46
  • Which OS? Are you sure you have an application associated with the magnet protocol? – Zach Kelling Feb 19 '12 at 16:49
  • This is on Windows 7 - I'm pretty sure that I have uTorrent associated with the magnet protocol, but how could I check this? – Eden Crow Feb 19 '12 at 16:56
  • 1
    For completeness: On Linux, you can probably call the `xdg-open` command with it. – Thomas K Feb 19 '12 at 17:01
  • @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. – Zach Kelling Feb 19 '12 at 17:10
  • @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. – Eden Crow Feb 19 '12 at 17:12
  • 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/announce` – Zach Kelling Feb 19 '12 at 17:15
  • 1
    @EdenCrow: Try `magnet:...` instead of `magent:...` – AndiDog Feb 19 '12 at 17:16
  • 1
    Also it should be `magnet:?xt=urn:btih` not `btich`. – Zach Kelling Feb 19 '12 at 17:19
  • AndiDog: Thanks - it's always the little typos that are the worst! @zeekay That works. Thanks. – Eden Crow Feb 19 '12 at 17:27
  • how to exactly use the xdg-open for ubuntu to open magnet links? –  Mar 09 '12 at 07:19
2

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)
Community
  • 1
  • 1
Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
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

gazhay
  • 131
  • 1
  • 11