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
.
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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With