FeedRSS.php

Go to the documentation of this file.
00001 <!--
00002                            _    _______          _     
00003                           | |  |__   __\        | |    
00004                      _ __ | |_ __ | | ___   ___ | |___ 
00005                     | '_ \| | '_ \| |/ _ \ / _ \| / __|
00006                     | | | | | |_) | | (_) | (_) | \__ \
00007                     |_| |_|_| .__/|_|\___/ \___/|_|___/
00008  ___________________________| |_________________________________________
00009 |                           |_|                                        |\
00010 |                                                                      |_\
00011 |   File    : FeedRSS.php                                                 |
00012 |   Created : 16-Feb-2012                                                 |
00013 |   By      : atrilla                                                     |
00014 |                                                                         |
00015 |   nlpTools - Natural Language Processing Toolkit for PHP                |
00016 |                                                                         |
00017 |   Copyright (c) 2012 Alexandre Trilla                                   |
00018 |                                                                         |
00019 |   ___________________________________________________________________   |
00020 |                                                                         |
00021 |   This file is part of nlpTools.                                        |
00022 |                                                                         |
00023 |   nlpTools is free software: you can redistribute it and/or modify      |
00024 |   it under the terms of the MIT/X11 License as published by the         |
00025 |   Massachusetts Institute of Technology. See the MIT/X11 License        |
00026 |   for more details.                                                     |
00027 |                                                                         |
00028 |   You should have received a copy of the MIT/X11 License along with     |
00029 |   this source code distribution of nlpTools (see the COPYING file       |
00030 |   in the root directory). If not, see                                   |
00031 |   <http://www.opensource.org/licenses/mit-license>.                     |
00032 |_________________________________________________________________________|
00033 -->
00034 
00035 <?php
00036 
00037 require(dirname(__FILE__)."/Feeder.php");
00038 
00045 class FeedRSS implements Feeder {
00046 
00050     private $maxItems = 20;
00051 
00057     public function getFood($sourceURL) {
00058         $sourceURL = (string)$sourceURL;
00059         // http://www.softarea51.com/tutorials/parse_rss_with_php.html
00060         $doc = new DOMDocument();
00061         $ok = $doc->load($sourceURL);
00062         if (!$ok) {
00063             throw new Exception("FeedRSS feeder: ".
00064                 "source cannot be loaded!\n");
00065         } else {
00066             $arrFeeds = array();
00067             $newsCount = 0;
00068             foreach ($doc->getElementsByTagName("item") as $node) {
00069                 if ($newsCount >= $this->maxItems) {
00070                     break;
00071                 } else {
00072                     $newsCount += 1;
00073                 }
00074                 $itemRSS = array ( 
00075                     "title" => $node->getElementsByTagName("title")->item(0)->nodeValue,
00076                     "desc" => $node->getElementsByTagName("description")->item(0)->nodeValue,
00077                     "link" => $node->getElementsByTagName("link")->item(0)->nodeValue,
00078                     "date" => $node->getElementsByTagName("pubDate")->item(0)->nodeValue
00079                     );
00080                 array_push($arrFeeds, $itemRSS);
00081             }
00082         }
00083         return $arrFeeds;
00084     }
00085 
00095     public function setMaxItems($maxits) {
00096         $maxits = (int)$maxits;
00097         assert($maxits > 0);
00098         $this->maxItems = $maxits;
00099     }
00100 }
00101 
00102 ?>