gotcreations/client-torrent-bundle

使用PHP与各种种子客户端通信的库

dev-master 2017-02-07 15:28 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:43 UTC


README

主要开发工作目前集中在Deluge上

变更日志

  • 升级以支持php 7
  • 对整数处理进行了多项修复,主要更改了整数检测的处理方式
  • 添加了对HTTPS主机连接的支持
  • 解决了完成百分比计算问题,在处理大文件大小时可能导致负值
  • 修复了无效的InvalidArgumentException参数导致应用程序崩溃的问题
  • 解决了与artax比较的问题,返回stdObj导致尝试遍历结果时失败

待办事项

  • 添加对http和https连接字符串的额外检测
  • 着手支持Transmission(可能有人可以帮助?我没有访问Transmission)
  • 添加对ruTorrent xmlrpc传输的支持

关于

提供了一种简单易用的面向对象接口,用于与种子客户端交互。使用此库,您可以检索Torrent数据作为Torrent对象,并告诉您的种子客户端执行诸如pauseTorrentstartTorrent之类的操作。

目前支持的客户端(已启用远程功能)

  • Deluge

安装

安装通过Composer完成。将以下内容添加到您的composer.json文件中

"require": {
    "j7mbo/torrent-php": "dev-master" 
}

别忘了运行composer install来创建包含所有项目依赖项的vendor/目录。

概述

此库并不特别复杂。它按以下顺序执行以下操作以给您一个包含Torrent对象的数组

ClientTransport

  • 从您的客户端检索数据。在Transmission和Deluge的情况下,这是通过HTTP协议的JSON-RPC调用完成的。

ClientAdapter

  • 将传输检索到的数据转换为Torrent对象。此对象包装了传输并使用适配器模式修改输出。

Torrent

您将得到Torrent对象,以供您在自己的应用程序中使用。

使用方法

显然,运行composer install后的第一件事就是包含自动生成的自动加载器文件

require_once "/path/to/project" . "/vendor/autoload.php";

创建一个ConnectionConfig对象,带有所需的连接参数。这些参数因客户端而异,因此请检查相关构造函数签名上的docblock。以下示例使用Deluge。

use TorrentPHP\Client\Deluge\ConnectionConfig;

$config = new ConnectionConfig(array(
    'host' => 'localhost',
    'port' => 9091,
    'username' => 'username',
    'password' => 'password'
));

我们不是使用cURL来执行RPC请求,而是使用Artax。我们需要一个新的Client对象和一个新的Request对象。

$client = new Amp\Artax\Client;

现在,要获取JSON,请使用ClientTransport对象

use TorrentPHP\Client\Deluge\ClientTransport;

$transport = new ClientTransport($client, $config);

在这里,您可以运行定义在ClientTransport接口中的任何方法,例如

$transport->getTorrents();
$transport->addTorrent('http://urlToTorrentFile.torrent');

以下方法允许您传递一个Torrent对象作为第一个参数,或者一个种子id(散列)作为第二个参数

$transport->startTorrent();
$transport->pauseTorrent();
$transport->deleteTorrent();

上述所有方法都返回客户端提供的原始json。如果您想获得用于在应用程序周围使用的漂亮Torrent对象,请在执行相同的调用之前将传输包装在相关的ClientAdapter中。

use TorrentPHP\Clent\Deluge\ClientAdapter,
    TorrentPHP\TorrentFactory,
    TorrentPHP\FileFactory;

$adapter = new ClientAdapter($transport, new TorrentFactory, new FileFactory);

然后,只需在适配器上调用相同的方法即可获得漂亮的TorrentFile对象。简单!

种子对象

Torrent对象是您将获得的最终实体,其中包含了您通过选择的种子客户端可用的属性。

PHP Torrent Object

所有上述属性都是私有的,可通过getter访问;例如,getHashString()getName()

查看 Torrent 类以获取您所需数据的可用方法。

代码示例

require_once __DIR__ . "/vendor/autoload.php";

use TorrentPHP\Client\Transmission\ConnectionConfig,
    TorrentPHP\Client\Transmission\ClientTransport,
    TorrentPHP\Client\Transmission\ClientAdapter,
    TorrentPHP\TorrentFactory,
    TorrentPHP\FileFactory;

// Create the HTTP Client Object
$client = new Artax\Client;

// Configuration
$config = new ConnectionConfig(array(
    'host'     => 'localhost', 
    'port'     => 9091,
    'username' => 'username',
    'password' => 'password'
));

// Create the transport that returns json
$transport = new ClientTransport($client, $config);

// Create the adapter that returns Torrent objects
$adapter = new ClientAdapter($transport, new TorrentFactory, new FileFactory);

// Add a torrent, and get back a Torrent object
$torrent = $adapter->addTorrent('http://releases.ubuntu.com/14.04/ubuntu-14.04-server-i386.iso.torrent');

// Pause the torrent we just added
$torrent = $adapter->pauseTorrent($torrent);

// Start the torrent we just added
$torrent = $adapter->startTorrent($torrent);

// Delete a torrent by it's hash instead of the object
$adapter->deleteTorrent(null, $torrent->getHashString());

种子客户端

Transmission 需要安装 transmission-remote-gui 并设置其配置以允许远程连接。

Deluge 需要同时运行 delugeddeluge-web

异步调用

喜欢事件驱动编程和回调地狱?您可以使用 TorrentPHP 进行异步调用。这些调用是通过 ArtaxAlert 实现的。实际上,您只需传递一个 PHP callable(需要 PHP 5.4),只有在接收到响应时才会调用它。它是非阻塞的,而且很酷。

目前只有 getTorrents() 支持异步调用。以下是如何向 Deluge 发起异步调用的示例

use TorrentPHP\Client\Deluge\ConnectionConfig,
    TorrentPHP\Client\Deluge\AsyncClientTransport,
    TorrentPHP\Client\Deluge\AsyncClientFactory,
    Alert\ReactorFactory;

// Factory to create the async client
$clientFactory = new TorrentPHP\Client\AsyncClientFactory;

// We need a request object as before
$request = new Amp\Artax\Request;

// Our connection settings
$config = new ConnectionConfig([
    'host'     => 'localhost',
    'port'     => 8112,
    'username' => 'username',
    'password' => 'deluge'
]);

// The new AsyncClientTransport
$transport = new AsyncClientTransport($clientFactory, $request, $config);

// Our own function gets invoked on the async response. The response is "injected" into the first variable for us.
$callable = function($response) {
    echo 'This could have taken 5 seconds or more, but it's async, so it's non-blocking. Yay!' . PHP_EOL;
    var_dump($response);
};

// Let's get our torrents asynchronously - the new AsyncClientTransport object takes a second callable parameter
$transport->getTorrents([], $callable);

扩展

您可以通过在 src/Client/<ClientName> 中创建一个新的客户端目录,并添加 ClientAdapterClientTransportConnectionConfig 来添加对您自己的客户端的支持。然而,它们不需要通过 RPC 进行通信,因为处理方式与应用程序的其余部分是分开的。

确保您使用正确的命名空间并实现/扩展正确的类。请参阅 src/Transmissionsrc/Deluge 目录中的示例。

如果您希望添加对其他种子客户端的支持,但不知道如何自行完成,请随时提交一个问题,我们可以一起解决这个问题。