【问题标题】:Downloading a magnetic link with Powershell 3使用 Powershell 3 下载磁力链接
【发布时间】:2013-10-22 11:29:11
【问题描述】:

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

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

我正在使用以下代码sn-p:

$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 客户端,但我不知道该怎么做。

【问题讨论】:

    标签: powershell download powershell-3.0 utorrent magnet-uri


    【解决方案1】:

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

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

    【讨论】:

      【解决方案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"
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-01
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多