taproot/subscriptions

轻松订阅网页内容。

v0.1.1 2015-01-16 10:13 UTC

This package is auto-updated.

Last update: 2024-09-16 21:43:09 UTC


README

taproot/subscriptions 是一个用于从 Silex/Symfony+Pimple 项目中轻松订阅网页内容的轻量级框架。

它提供了一个 API(以及一些基本的网页界面),用于订阅资源的未来更新以及爬取历史内容。目前只完全支持 HTML 资源。

安装

使用 Composer 安装

./composer.phar require taproot/subscriptions:~0.1

设置

taproot/subscriptions 需要几个服务才能正常运行。它们应该设置如下

<?php

// subscriptions.storage needs to be an instance implementing Taproot\Subscriptions\SubscriptionStorage
// Currently the only provided implementation is PdoSubscriptionStorage, which takes a PDO instance and an
// optional prefix
$app['subscriptions.storage'] = $app->share(function () use ($app) {
	return new Taproot\Subscriptions\PdoSubscriptionStorage($db, 'subscriptions_');
});

// subscriptions.defaulthub should be an subclass of Taproot\Subscriptions\PushHub, which will be used to 
// subscribe to content which doesn’t natively support PuSH. Most of the time this should be a Superfeedr hub,
// for which Taproot\Subscriptions\SuperfeedrHub is provided.
$app['subscriptions.defaulthub'] = function () use ($app) {
	return new Taproot\Subscriptions\SuperfeedrHub('username', 'password or token');
};

然后,设置 taproot/subscriptions 并通过调用 Taproot\Subscriptions\controllers() 在您的应用程序中某个位置挂载其路由

第一个参数是 Silex 中的 $app 的引用,或者在其他情况下是 Pimple 兼容的依赖容器。所有其他参数都是可选的回调函数。第二个是授权回调函数,将在每次非公开请求之前调用当前 $request。它应该检查当前用户是否有权访问,否则中止。第三个是添加对 subscriptions.ping 事件的监听器的快捷方式——它是一个可调用的函数,

<?php

$app->mount('/subscriptions', Taproot\Subscriptions\controller($app, function ($request) {
	// For the subscription admin routes, check if the current user is allowed to view them.
	if (!userIsAdmin($request)) {
		$app->abort(401, 'Only admins may view this page');
	}
}, function ($resource) {
	// A new resource has been fetched, either via historical crawling or new content from a subscription.
	// Do something with the resource which has been fetched, e.g. store, index, processing
	$url = $resource['url'];
}));

网页界面

转到您网站上挂载订阅路由的地方,以查看网页界面。它列出了您当前的订阅,并允许您订阅或订阅+爬取,查看单个订阅信息和单个ping内容。

API

订阅一个源

<?php

use Taproot\Subscriptions;

list($subscription, $error) = Subscriptions\subscribe($app, 'http://waterpigs.co.uk');
if ($error !== null) {
	// There was a problem, and $error is a Guzzle Exception with more information about exactly what.
} else {
	// Otherwise, $subscription is an array representing the subscription which was just created
}

订阅一个源并爬取历史内容

<?php

use Taproot\Subscriptions;

// The extra (optional) argument is a callback to be run for each page which gets crawled, in addition to any listeners
// on subscriptions.ping — handy e.g. for logging purposes
list($subscription, $error) = Subscriptions\subscribeAndCrawl($app, 'http://waterpigs.co.uk', function ($resource) {
	echo "Crawled {$resource['url']}\n";
});

对新的/旧的内容执行任务

每当从订阅 ping 或爬取 rel=prev[ious] 链接的历史内容中获取源资源时,都会在 $app['dispatcher'] 上分发 subscriptions.ping 事件,该事件包含有关资源的信息。

您可以将监听器附加到该事件,或将一个作为第三个参数传递给 Subscriptions\controllers() —— 它们完成完全相同的事情,只是其中一个是一个快捷方式。

TODO:详细说明事件具有哪些属性。

您的处理程序应该编写成这样,多次运行相同的相同内容不会产生重复的结果,因为它们完全可能被多次运行。

例如,一个典型的 h-feed 订阅应该看起来像这样(伪代码)

<?php

function ($resource) {
	$posts = postsFromMicroformats($resource['mf']);
	foreach ($posts as $post) {
		createOrUpdatePost($post);
	}
}

变更日志

dev-master(迄今为止未版本化)

  • 添加了未保存的“resource”键到 $subscription 返回值中,包含 HTML+URL+microformats 等内容,以供立即使用
  • 标准化了资源处理事件上下文的构建方式
  • 将 PdoSubscriptionStorage ID 列的定义更改为 varchar,以允许更灵活的 ID

v0.1.0

  • 第一个版本