Friday, August 7, 2015

Joomla componenet: Article generator from RSS feeds - part 3

This project (as many others) started just like a "..let's try it" so it suffering from the ugly title disease :) (remember myFrontController which is now a small cms which started a  test for Front Controller design pattern). So I had correct the title to "com_rssaggregator"

Now going back to the functionality, in the cron.rssaggregator.php I instantiate the RSSReader and retrieve the result into an array:

$list_of_feed_articles=$rss->getContent($model->feedsList,$model->noOfFeeds,$model->show_graphic);

What will be now saved in the database looks like this:
$tobind = array(
                           "title" => $feed_article['title'],
                           "alias" => JFilterOutput::stringURLUnicodeSlug($feed_article['title']),
                           "introtext" => $feed_article['desc'],
                           "state"=>'1',
                           "created"=>JFactory::getDate()->toSql(),
                           "featured"=>$featured,
                           "created_by"=>$author,
                           "language"=>'*',
                           "catid" => $category,
                           "metadata"=>'{"page_title":"","author":"","robots":""}',
                        );


- alias - is the slug for each article
- state - state of an article in Joomla com_content component. 1 means published, 0 unpublished
- featured - an article can be also marked as featured (that little star) in the administrator area next to article names
- created_by  - is the ID of the author
- cadid is the ID of the category in which the articles will be published.

In the RSSReader.php you can find the logic for reading the feed and parsing it according to the needs. I tried to  apply the 20/80 rules, meaning that with 20% of effort be able to read image from 80% of the RSS/Atom feeds formats.  I am checking now for the following tags:

<enclosure
<image
<media:content
<media:thumbnail

Also I added the method articleExists($slug) to the model (file models/rssaggreggator.php ). Checks the database for an article with same alias (slug), if there is one already with the same name do not added it again to the table #__content.

As this script will be executed from cron it would be nice to log all the eventual issues that the script encountered in the execution. I made a  logging errors system inspired from here:  http://joomla.stackexchange.com/questions/7286/logging-to-a-file-only-with-jlog

If the log file older than 48 hours, delete it order too prevent having large log files:

if(file_exists($filename)){
    if(time() - filectime($filename) >  48* 3600) unlink($filename);
}


Please go ahead and DOWNLOAD  this beautiful component and give it a try on your own Joomla! website. Any feedback is welcomed.

How to use it:
1.Step 1 install the component
2. In your administrator dashboard look for RSS Aggregator ->Sources view
3. Add at list one valid RSS feed
4. run cron.rssaggregator.com (if you allow direct access to your files which is very bad practice, you write in the browser the path to your file, something like this: http://127.0.0.1/joomla1/administrator/components/com_rssaggregator/cron.rssaggregator.php). Otherwise you can use a command line and execute it as any php file.
5. add the script in cron to be executed recursively, depending on how often the content of the RSS is refreshed.


No comments:

Post a Comment