使用Powershell 3下载磁链

时间:2013-10-14 19:44:58

标签: powershell download powershell-v3.0 utorrent magnet-uri

我是PowerShell的新手,我正在尝试创建一个简短的脚本,可以在页面上找到磁链接并下载它们。

我对磁性URI的工作原理并不了解,而且我似乎无法让我的脚本通过它们下载文件。

我正在使用以下代码段:

$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)

产生以下异常

    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)

在PowerShell中是否有不同的下载方式,或者磁链接无法实现这一点。

我的最终目标是通过磁力链接开始下载洪流,所以也许可以用链接打开一个torrent客户端,但我不知道我会怎么做。

3 个答案:

答案 0 :(得分:3)

Magnetic links使用Windows本身不支持的协议magnetDownloadFile()不支持。

如果您安装的客户端支持magnet协议,则会为magnet URI方案安装protocol handler

这应该允许你使用Start-Process,只传递磁性URL作为参数,来调用该客户端并让它对它通常对该URL进行的任何处理。

答案 1 :(得分:2)

如果你安装了像µTorrent这样的torrent客户端并设置为处理磁力链接,你可以打开Powershell的链接:

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

应该打开torrent客户端。

您还可以使用命令行torrent客户端aria2并下载:

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

答案 2 :(得分:0)

我有一个github项目(FirefoxMagnetMimeHandler)显示了如何使用Powershell通过传输JSON RPC API将磁链发送到本地或远程传输服务器。从本质上讲,这归结为将一小部分JSON发送到传输服务器。困难的部分是获取正确的会话ID。

以下是相关脚本的链接:magnet_add.ps1。其他脚本仅处理设置firefox MIME处理程序。