2012-05-28 22:27:18 +04:00
|
|
|
<?php
|
2013-03-22 14:18:16 +04:00
|
|
|
|
2012-05-28 22:27:18 +04:00
|
|
|
/**
|
2013-03-22 14:18:16 +04:00
|
|
|
* ownCloud - News
|
2012-05-28 22:27:18 +04:00
|
|
|
*
|
|
|
|
* @author Alessandro Cosentino
|
2013-03-22 14:18:16 +04:00
|
|
|
* @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.
|
2012-07-21 18:45:37 +04:00
|
|
|
*
|
2013-03-22 14:18:16 +04:00
|
|
|
* 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/>.
|
2012-07-21 18:45:37 +04:00
|
|
|
*
|
2012-05-28 22:27:18 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-22 14:18:16 +04:00
|
|
|
namespace OCA\News\Utility;
|
2012-08-11 20:19:37 +04:00
|
|
|
|
2013-04-02 12:41:01 +04:00
|
|
|
use \OCA\AppFramework\Core\API;
|
2013-04-17 20:29:01 +04:00
|
|
|
use \OCA\AppFramework\Utility\FaviconFetcher;
|
2013-04-18 17:56:12 +04:00
|
|
|
use \OCA\AppFramework\Utility\SimplePieAPIFactory;
|
|
|
|
use \OCA\AppFramework\Utility\TimeFactory;
|
2013-04-02 12:41:01 +04:00
|
|
|
|
2013-03-26 21:04:02 +04:00
|
|
|
use \OCA\News\Db\Item;
|
|
|
|
use \OCA\News\Db\Feed;
|
|
|
|
|
2012-07-03 07:39:19 +04:00
|
|
|
|
2013-04-02 13:09:33 +04:00
|
|
|
class FeedFetcher implements IFeedFetcher {
|
2012-08-16 23:34:41 +04:00
|
|
|
|
2013-04-02 12:41:01 +04:00
|
|
|
private $api;
|
2013-04-12 02:33:08 +04:00
|
|
|
private $cacheDirectory;
|
|
|
|
private $cacheDuration;
|
2013-04-17 20:29:01 +04:00
|
|
|
private $faviconFetcher;
|
2013-04-18 17:56:12 +04:00
|
|
|
private $simplePieFactory;
|
|
|
|
private $time;
|
|
|
|
|
|
|
|
public function __construct(API $api,
|
|
|
|
SimplePieAPIFactory $simplePieFactory,
|
|
|
|
FaviconFetcher $faviconFetcher,
|
|
|
|
TimeFactory $time,
|
|
|
|
$cacheDirectory,
|
|
|
|
$cacheDuration){
|
2013-04-02 12:41:01 +04:00
|
|
|
$this->api = $api;
|
2013-04-12 02:33:08 +04:00
|
|
|
$this->cacheDirectory = $cacheDirectory;
|
|
|
|
$this->cacheDuration = $cacheDuration;
|
2013-04-17 20:29:01 +04:00
|
|
|
$this->faviconFetcher = $faviconFetcher;
|
2013-04-18 17:56:12 +04:00
|
|
|
$this->simplePieFactory = $simplePieFactory;
|
|
|
|
$this->time = $time;
|
2013-04-02 12:41:01 +04:00
|
|
|
}
|
|
|
|
|
2012-08-16 23:34:41 +04:00
|
|
|
|
2013-04-17 20:29:01 +04:00
|
|
|
/**
|
|
|
|
* This fetcher handles all the remaining urls therefore always returns true
|
|
|
|
*/
|
2013-04-02 13:09:33 +04:00
|
|
|
public function canHandle($url){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-28 22:27:18 +04:00
|
|
|
/**
|
2013-03-22 14:40:15 +04:00
|
|
|
* Fetch a feed from remote
|
|
|
|
* @param string url remote url of the feed
|
|
|
|
* @throws FetcherException if simple pie fails
|
2013-03-22 15:35:30 +04:00
|
|
|
* @return array an array containing the new feed and its items
|
2012-05-28 22:27:18 +04:00
|
|
|
*/
|
2013-03-22 14:18:16 +04:00
|
|
|
public function fetch($url) {
|
2013-04-18 17:56:12 +04:00
|
|
|
$simplePie = $this->simplePieFactory->getCore();
|
|
|
|
$simplePie->set_feed_url($url);
|
2013-04-14 00:48:31 +04:00
|
|
|
$simplePie->enable_cache(true);
|
2013-04-12 02:33:08 +04:00
|
|
|
$simplePie->set_cache_location($this->cacheDirectory);
|
|
|
|
$simplePie->set_cache_duration($this->cacheDuration);
|
2013-03-22 15:35:30 +04:00
|
|
|
|
2013-03-22 14:40:15 +04:00
|
|
|
if (!$simplePie->init()) {
|
2013-03-22 15:35:30 +04:00
|
|
|
throw new FetcherException('Could not initialize simple pie');
|
2012-07-30 22:08:36 +04:00
|
|
|
}
|
2012-08-02 20:52:33 +04:00
|
|
|
|
2013-04-18 17:56:12 +04:00
|
|
|
|
2012-10-23 22:54:55 +04:00
|
|
|
try {
|
2013-04-18 17:56:12 +04:00
|
|
|
// somehow $simplePie turns into a feed after init
|
2012-10-23 22:54:55 +04:00
|
|
|
$items = array();
|
2013-03-22 14:40:15 +04:00
|
|
|
if ($feedItems = $simplePie->get_items()) {
|
|
|
|
foreach($feedItems as $feedItem) {
|
2013-04-18 17:56:12 +04:00
|
|
|
array_push($items, $this->buildItem($feedItem));
|
2012-08-15 20:27:32 +04:00
|
|
|
}
|
2012-07-30 23:38:58 +04:00
|
|
|
}
|
2012-08-02 20:52:33 +04:00
|
|
|
|
2013-04-18 17:56:12 +04:00
|
|
|
$feed = $this->buildFeed($simplePie, $url);
|
2013-04-14 00:48:31 +04:00
|
|
|
|
2013-03-22 14:40:15 +04:00
|
|
|
return array($feed, $items);
|
2013-04-14 00:48:31 +04:00
|
|
|
|
2013-03-22 15:35:30 +04:00
|
|
|
} catch(\Exception $ex){
|
2013-03-22 14:40:15 +04:00
|
|
|
throw new FetcherException($ex->getMessage());
|
2012-07-23 23:02:49 +04:00
|
|
|
}
|
2013-03-22 14:40:15 +04:00
|
|
|
|
2012-07-23 20:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:15 +04:00
|
|
|
|
2013-04-18 17:56:12 +04:00
|
|
|
protected function buildItem($simplePieItem) {
|
|
|
|
$item = new Item();
|
|
|
|
$item->setStatus(0);
|
|
|
|
$item->setUnread();
|
|
|
|
$item->setUrl($simplePieItem->get_permalink());
|
|
|
|
// unescape content because angularjs helps agains XSS
|
|
|
|
$item->setTitle(html_entity_decode($simplePieItem->get_title()));
|
|
|
|
$guid = $simplePieItem->get_id();
|
|
|
|
$item->setGuid($guid);
|
|
|
|
$item->setGuidHash(md5($guid));
|
|
|
|
$item->setBody($simplePieItem->get_content());
|
|
|
|
$item->setPubDate($simplePieItem->get_date('U'));
|
|
|
|
$item->setLastModified($this->time->getTime());
|
|
|
|
|
|
|
|
$author = $simplePieItem->get_author();
|
|
|
|
if ($author !== null) {
|
2013-04-19 17:00:25 +04:00
|
|
|
$item->setAuthor(html_entity_decode($author->get_name()));
|
2013-04-18 17:56:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make it work for video files also
|
|
|
|
$enclosure = $simplePieItem->get_enclosure();
|
|
|
|
if($enclosure !== null) {
|
|
|
|
$enclosureType = $enclosure->get_type();
|
|
|
|
if(stripos($enclosureType, "audio/") !== false) {
|
|
|
|
$item->setEnclosureMime($enclosureType);
|
|
|
|
$item->setEnclosureLink($enclosure->get_link());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function buildFeed($simplePieFeed, $url) {
|
|
|
|
$feed = new Feed();
|
|
|
|
|
|
|
|
// unescape content because angularjs helps agains XSS
|
|
|
|
$feed->setTitle(html_entity_decode($simplePieFeed->get_title()));
|
|
|
|
$feed->setUrl($url);
|
|
|
|
$feed->setLink($simplePieFeed->get_link());
|
|
|
|
$feed->setUrlHash(md5($url));
|
|
|
|
$feed->setAdded($this->time->getTime());
|
|
|
|
|
|
|
|
// get the favicon from the feed or the webpage
|
|
|
|
$favicon = $simplePieFeed->get_image_url();
|
|
|
|
|
|
|
|
if ($favicon) {
|
|
|
|
$feed->setFaviconLink($favicon);
|
|
|
|
} else {
|
|
|
|
$webFavicon = $this->faviconFetcher->fetch($feed->getLink());
|
|
|
|
$feed->setFaviconLink($webFavicon);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $feed;
|
|
|
|
}
|
|
|
|
|
2012-05-28 22:27:18 +04:00
|
|
|
}
|