1

我是 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,所以也许可以通过链接打开 torrent 客户端,但我不确定我会怎么做。

4

3 回答 3

3

磁性链接使用magnetWindows 本身不支持的协议, DownloadFile().

如果您安装支持该magnet协议的客户端,它将为URI 方案安装协议处理程序。magnet

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

于 2013-10-14T19:51:44.070 回答
2

如果您安装了类似 Torrent 客户端µTorrent并且设置为处理磁力链接,您可以从 Powershell 打开链接:

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

那应该打开洪流客户端。

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

aria2c "magnet:?xt=urn:btih:44bb5e0325b7dad0bdc5abce459b85b014766ec0&dn=MY_TORRENT&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp"
于 2013-10-15T04:29:36.420 回答
0

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

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

于 2019-03-31T14:04:09.267 回答