Hi, I've built a "RSS Feed Reader" module to my application. This module read and store all feeds in my database for future use. I want to know if there is a better and faster way to do that than the code described bellow. This code is executed every 10 minutes in a cron job.
// In my FeedEntries Model
public function fetchNewEntries()
{
require_once(APPLICATION_PATH . '/../library/simplehtmldom/simple_html_dom.php');
$TableFeeds = new Model_DbTable_Feeds();
$Feeds = $TableFeeds->fetchAll();
$limit = 15;
$count = 0;
foreach($Feeds as $Feed)
{
if($count >$limit) break;
try
{
$entries = Zend_Feed_Reader::import($Feed->link);
}
catch(Zend_Feed_Exception $e)
{
// TODO: Log
continue;
}
$linksList = $this->getAllEntriesPermaLinksByFeed($Feed->id);
foreach ($entries as $entry)
{
$commentsCount = ($entry->getCommentCount() > 0) ? $entry->getCommentCount() : 0;
if(in_array($entry->getPermaLink(),$linksList))
{
if(empty($Feed->link_comentarios))
{
// Update the comments count
$post = $this->getEntryByLink($entry->getLink(),$entry->getPermaLink());
$post->comments = $commentsCount;
$post->comments_date = date('Y-m-d H:i:s');
$post->save();
}
continue;
}
$linksList[] = $entry->getPermaLink();
$count++;
if($count >$limit) break;
$categories = $entry->getCategories()->getValues();
$Category = new Model_DbTable_Categorias();
$availableCategories = $Category->getList('slug');
$categorias_id = array();
foreach ($categories as $cat) {
if(!empty($cat))
{
$cat = str_replace(' & ',' ',$cat);
$slug = slugfy($cat);
if(in_array($slug,$availableCategories))
{
$categorias_id[] = array_isearch($slug,$availableCategories);
}
else
{
$cat_id = $Category->insert(array('nome' => utf8_encode(ucwords(strtolower(utf8_decode($cat)))), 'slug' => $slug));
$categorias_id[] = $cat_id;
$availableCategories[$cat_id] = $slug;
}
}
}
if(!in_array($Feed->categoria_id,$categorias_id))
{
$categorias_id[] = $Feed->categoria_id;
}
$author = $entry->getAuthor(0);
$Colunistas = new Model_DbTable_Colunistas();
$author_id = $Colunistas->insertAuthorFromRss($author['name']);
$entrySlug = slugfy($entry->getTitle());
$data = array(
'feed_id' => $Feed->id,
'colunista_id' => $author_id,
'titulo' => $entry->getTitle(),
'descricao' => substr(strip_tags($entry->getContent()),0,350),
'slug' => $entrySlug,
'created_at' => $entry->getDateCreated()->get(Zend_Date::ISO_8601),
'link' => $entry->getLink(),
'permaLink' => $entry->getPermaLink(),
'html' => $entry->getContent(),
'comments' => $commentsCount,
'comments_date' => date('Y-m-d H:i:s')
);
$entry_id = $this->addEntry($data);
$EntriesCategorias = new Model_DbTable_EntriesCategorias();
foreach($categorias_id as $categoria_id)
{
$EntriesCategorias->insert(array('entry_id' => $entry_id,'categoria_id' => $categoria_id));
}
}
}
}