summaryrefslogtreecommitdiffstats
path: root/utility
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-22 11:40:15 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-22 11:40:15 +0100
commit880184d0c2372c2493d62cb6fa4d268902043a65 (patch)
treeff4a1fe0953fb41c8ce0cf75f2735578f72f9063 /utility
parentc8352ac5817a3174fa0947aa5eeef349e760f824 (diff)
added a seperate exception for fetching feeds
Diffstat (limited to 'utility')
-rw-r--r--utility/feedfetcher.php126
-rw-r--r--utility/fetcherexception.php38
2 files changed, 102 insertions, 62 deletions
diff --git a/utility/feedfetcher.php b/utility/feedfetcher.php
index f33f8a82e..edaa72b9a 100644
--- a/utility/feedfetcher.php
+++ b/utility/feedfetcher.php
@@ -30,109 +30,110 @@ class FeedFetcher {
/**
- * @brief Fetch a feed from remote
- * @param url remote url of the feed
- * @returns an instance of OC_News_Feed
+ * Fetch a feed from remote
+ * @param string url remote url of the feed
+ * @throws FetcherException if simple pie fails
+ * @returns array an array containing the new feed and its items
*/
public function fetch($url) {
- $spfeed = new \SimplePie_Core();
- $spfeed->set_feed_url( $url );
- $spfeed->enable_cache( false );
+ $simplePie = new \SimplePie_Core();
+ $simplePie->set_feed_url( $url );
+ $simplePie->enable_cache( false );
- if (!$spfeed->init()) {
+ // TODO: throw error
+ if (!$simplePie->init()) {
return null;
}
- //temporary try-catch to bypass SimplePie bugs
+ // temporary try-catch to bypass SimplePie bugs
try {
- $spfeed->handle_content_type();
- $title = $spfeed->get_title();
+ $simplePie->handle_content_type();
$items = array();
- if ($spitems = $spfeed->get_items()) {
- foreach($spitems as $spitem) {
- $itemUrl = $spitem->get_permalink();
- $itemTitle = $spitem->get_title();
- $itemGUID = $spitem->get_id();
- $itemBody = $spitem->get_content();
- $item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody);
-
- $spAuthor = $spitem->get_author();
- if ($spAuthor !== null) {
- $item->setAuthor($spAuthor->get_name());
+ if ($feedItems = $simplePie->get_items()) {
+ foreach($feedItems as $feedItem) {
+ $item = new Item();
+ $item->setUrl( $feedItem->get_permalink() );
+ $item->setTitle( $feedItem->get_title() );
+ $item->setGuid( $feedItem->get_id() );
+ $item->setBody( $feedItem->get_content() );
+ $item->setDate( $feedItem->get_date('U') );
+
+ $author = $feedItem->get_author();
+ if ($author !== null) {
+ $item->setAuthor( $author->get_name() );
}
- //date in Item is stored in UNIX timestamp format
- $itemDate = $spitem->get_date('U');
- $item->setDate($itemDate);
-
// associated media file, for podcasts
- $itemEnclosure = $spitem->get_enclosure();
- if($itemEnclosure !== null) {
- $enclosureType = $itemEnclosure->get_type();
- $enclosureLink = $itemEnclosure->get_link();
- if(stripos($enclosureType, "audio/") !== FALSE) {
- $enclosure = new Enclosure();
- $enclosure->setMimeType($enclosureType);
- $enclosure->setLink($enclosureLink);
- $item->setEnclosure($enclosure);
+ $enclosure = $feedItem->get_enclosure();
+ if($enclosure !== null) {
+ $enclosureType = $enclosure->get_type();
+ if(stripos($enclosureType, "audio/") !== false) {
+ $enclosure->setEnclosureMime($enclosureType);
+ $enclosure->setEnclosureLink($enclosure->get_link());
}
}
- $items[] = $item;
+ array_push($items, $item);
}
}
- $feed = new Feed($url, $title, $items);
+ $feed = new Feed();
+ $feed->setTitle( $simplePie->get_title());
+ $feed->setUrl($url);
+ $feed->setUrlHash(md5($url));
+ $feed->setAdded(time());
- $favicon = $spfeed->get_image_url();
+ $favicon = $simplePie->get_image_url();
- if ($favicon !== null && $this->checkFavicon($favicon)) { // use favicon from feed
- $feed->setFavicon($favicon);
- }
- else { // try really hard to find a favicon
+ if ($favicon !== null && $this->checkFavicon($favicon)) {
+ $feed->setFaviconLink($favicon);
+
+ } else {
$webFavicon = $this->discoverFavicon($url);
if ($webFavicon !== null) {
- $feed->setFavicon($webFavicon);
+ $feed->setFaviconLink($webFavicon);
}
}
- return $feed;
+ return array($feed, $items);
+ } catch(Exception $ex){
+ throw new FetcherException($ex->getMessage());
}
- catch (Exception $e) {
- return null;
- }
+
}
/**
* Perform a "slim" fetch of a feed from remote.
* Differently from Utils::fetch(), it doesn't retrieve items nor a favicon
*
- * @param url remote url of the feed
- * @returns an instance of OC_News_Feed
+ * @param string url remote url of the feed
+ * @returns \OCA\News\Db\Feed
*/
public function slimFetch($url) {
- $spfeed = new \SimplePie_Core();
- $spfeed->set_feed_url( $url );
- $spfeed->enable_cache( false );
- $spfeed->set_stupidly_fast( true );
+ $simplePie = new \SimplePie_Core();
+ $simplePie->set_feed_url( $url );
+ $simplePie->enable_cache( false );
+ $simplePie->set_stupidly_fast( true );
- if (!$spfeed->init()) {
+ if (!$simplePie->init()) {
return null;
}
- //temporary try-catch to bypass SimplePie bugs
- try {
- $title = $spfeed->get_title();
-
- $feed = new Feed($url, $title);
+ // temporary try-catch to bypass SimplePie bugs
+ try {
+ $feed = new Feed($url, $title);
+ $feed->setUrl($url);
+ $feed->setUrlHash(md5($url));
+ $feed->setTitle($simplePie->get_title());
+ $feed->setAdded(time());
+ return $feed;
- return $feed;
+ } catch(Exception $ex){
+ throw new FetcherException($ex->getMessage());
}
- catch (Exception $e) {
- return null;
- }
}
+
public function checkFavicon($favicon) {
if ($favicon === null || $favicon == false)
return false;
@@ -153,6 +154,7 @@ class FeedFetcher {
return false;
}
+
public function discoverFavicon($url) {
//try webroot favicon
$favicon = \SimplePie_Misc::absolutize_url('/favicon.ico', $url);
diff --git a/utility/fetcherexception.php b/utility/fetcherexception.php
new file mode 100644
index 000000000..2f6764077
--- /dev/null
+++ b/utility/fetcherexception.php
@@ -0,0 +1,38 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\Utility;
+
+class FetcherException extends \Exception {
+
+ /**
+ * Constructor
+ * @param string $msg the error message
+ */
+ public function __construct($msg){
+ parent::__construct($msg);
+ }
+
+} \ No newline at end of file