Symfony RSS reader
Simple batch rss reader for Symfony. Saves all posts to db.
install
-------
$ symfony plugin-install http://plugins.symfony-project.com/sfFeed2Plugin
$ symfony plugin-install http://plugins.symfony-project.com/sfWebBrowserPlugin
$ symfony cc
propel schema.yml
-----------------
propel:
rss_post:
id:
unique_id: { type: varchar, size: 255, index: true }
author_name: varchar(255)
title: varchar(255)
link: varchar(255)
description: longvarchar
pub_date: timestamp
created_at:
updated_at:
$ symfony propel-build-all
model
-----
- RssPostPeer.php -
<?php
class RssPostPeer extends BaseRssPostPeer
{
public static function retrieveByUniqueId($id) {
$c = new Criteria();
$c->add(self::UNIQUE_ID, $id);
return self::doSelectOne($c);
}
}
batch
-----
$ symfony init-batch default rss <app>
<?php
....
// batch process here
$uris = array('http://rss.rss.cz/rss.php');
foreach($uris as $uri) {
$browser = new sfWebBrowser(array(
'user_agent' => 'sfFeedReader/0.9',
'timeout' => 5
));
$feedString = $browser->get($uri)->getResponseText();
// load from reply
$feed = new sfRssFeed();
$feed->fromXml($feedString);
// posts
foreach($feed->getItems() as $post) {
if(!RssPostPeer::retrieveByUniqueId($post->getUniqueId())) {
$p = new RssPost;
$p->setAuthorName($post->getAuthorName());
$p->setTitle($post->getTitle());
$p->setDescription($post->getDescription());
$p->setPubDate($post->getPubDate());
$p->setUniqueId($post->getUniqueId());
$p->save();
echo myString::urilize($post->getTitle())."\n";
}
}
}
Tagy:
php 75 řádků | 2008-03-21 15:32:06 | air.kadlec@seznam.cz