代码之家  ›  专栏  ›  技术社区  ›  Alex

使用Powershell 3下载磁性链接

  •  1
  • Alex  · 技术社区  · 13 年前

    我是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客户端,但我不确定该怎么做。

    3 回复  |  直到 13 年前
        1
  •  3
  •   Eric J.    13 年前

    Magnetic links 使用协议 magnet 不受Windows本机支持,也不受 DownloadFile() .

    如果您安装的客户端确实支持 磁铁 协议,它将安装 protocol handler 对于 磁铁 URI方案。

    这应该允许您使用 Start-Process ,只传递磁性URL作为参数,以调用该客户端,并让它对该URL进行正常处理。

        2
  •  2
  •   manojlds    13 年前

    如果你有一个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
  •  0
  •   jmik    7 年前

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

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

    推荐文章