Question

I'm new to PowerShell and I'm trying to create a short script that will find magnetic links on a page and download them.

I don't have much of an understanding of how magnetic URIs work and I can't seem to get my script to download files via them.

I am using the following code snippet:

$webclient = New-Object System.Net.WebClient
$url = "magnet:?xt=urn:btih:44bb5e0325b7dad0bdc5abce459b85b014766ec0&dn=MY_TORRENT&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp"

$file = "C:\Some\Path\myfile.torrent"
$webclient.DownloadFile($url, $file)

Which produces the following exception

    System.Management.Automation.MethodInvocationException: Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." ---> System.Net.We
bException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at CallSite.Target(Closure , CallSite , Object , Object , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo member
Info)
   at CallSite.Target(Closure , CallSite , Object , Object , Object )
   at System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

Is there a different way of downloading in PowerShell or is this not possible with a magnetic link.

My ultimate goal is to start downloading a torrent via a magnetic link, so maybe it's possible to just open a torrent client with the link, but I'm not sure how I would do that.

Was it helpful?

Solution 2

If you have a torrent client like µTorrent installed and it is set to handle magnet links, you can just open the link from Powershell:

start "magnet:?xt=urn:btih:44bb5e0325b7dad0bdc5abce459b85b014766ec0&dn=MY_TORRENT&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp"

That should open the torrent client.

You can also use a command line torrent client aria2 and download:

aria2c "magnet:?xt=urn:btih:44bb5e0325b7dad0bdc5abce459b85b014766ec0&dn=MY_TORRENT&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp"

OTHER TIPS

Magnetic links use a protocol magnet not natively supported by Windows, and not supported by DownloadFile().

If you install a client that does support the magnet protocol, it will install a protocol handler for the magnet URI Scheme.

That should allow you to use Start-Process, passing just the magnetic URL as an argument, to invoke that client and have it do whatever processing it normally does with that URL.

I have a github project (FirefoxMagnetMimeHandler) shows how to send a magnet link to a local or remote transmission server via the transmission JSON RPC API using powershell. In essence this comes down to sending small chunks of JSON to the transmission server. The difficult part is getting a correct session ID.

Here's a link to the relevant script: magnet_add.ps1 . The other scripts only deal with setting up firefox mime handlers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top