Skip to content

Commit

Permalink
move Fetcher to namespace Feed and fix CDATA parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nogo committed Jul 14, 2013
1 parent 15fe3da commit e05f224
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 127 deletions.
71 changes: 38 additions & 33 deletions src/Nogo/Feedbox/Controller/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Nogo\Feedbox\Controller;

use Guzzle\Http\Client;
use Nogo\Feedbox\Helper\Fetcher;
use Nogo\Feedbox\Feed\Fetcher;
use Nogo\Feedbox\Api\Source as SourceApi;
use Nogo\Feedbox\Api\Tag as TagApi;
use Nogo\Feedbox\Helper\HtmlPurifierSanitizer;
Expand Down Expand Up @@ -143,46 +143,51 @@ protected function fetchSource(array $source)
$fetcher->setTimeout($this->app->config('fetcher.timeout'));
$content = $fetcher->get($source['uri']);

$defaultWorkerClass = $this->app->config('worker.default');

/**
* @var $worker \Nogo\Feedbox\Feed\Worker
*/
$worker = new $defaultWorkerClass();
$worker->setSanitizer($this->sanitier);
$worker->setContent($content);
try {
$items = $worker->execute();
} catch (\Exception $e) {
$items = null;
}
if (!empty($content)) {
$defaultWorkerClass = $this->app->config('worker.default');

/**
* @var $worker \Nogo\Feedbox\Feed\Worker
*/
$worker = new $defaultWorkerClass();
$worker->setSanitizer($this->sanitier);
$worker->setContent($content);
try {
$items = $worker->execute();
} catch (\Exception $e) {
$items = null;
}

if ($items != null) {
foreach($items as $item) {
if (isset($item['uid'])) {
$dbItem = $this->itemRepository->findBy('uid', $item['uid']);
if (!empty($dbItem)) {
if ($item['content'] !== $dbItem['content']
|| $item['title'] !== $dbItem['title']) {
$item['id'] = $dbItem['id'];
$item['starred'] = $dbItem['starred'];
$item['created_at']= $dbItem['created_at'];
} else {
continue;
if ($items != null) {
foreach($items as $item) {
if (isset($item['uid'])) {
$dbItem = $this->itemRepository->findBy('uid', $item['uid']);
if (!empty($dbItem)) {
if ($item['content'] !== $dbItem['content']
|| $item['title'] !== $dbItem['title']) {
$item['id'] = $dbItem['id'];
$item['starred'] = $dbItem['starred'];
$item['created_at']= $dbItem['created_at'];
} else {
continue;
}
}
}

$item['source_id'] = $source['id'];
$this->itemRepository->persist($item);
}
}

$item['source_id'] = $source['id'];
$this->itemRepository->persist($item);
$source['last_update'] = date('Y-m-d H:i:s');
if (empty($source['period'])) {
$source['period'] = $worker->getUpdateInterval();
}
$source['errors'] = $worker->getErrors();
} else {
$source['errors'] = $fetcher->getError();
}

$source['last_update'] = date('Y-m-d H:i:s');
if (empty($source['period'])) {
$source['period'] = $worker->getUpdateInterval();
}
$source['errors'] = $worker->getErrors();
$source['unread'] = $this->itemRepository->countSourceUnread([$source['id']]);
$this->sourceRepository->persist($source);

Expand Down
90 changes: 90 additions & 0 deletions src/Nogo/Feedbox/Feed/Fetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
namespace Nogo\Feedbox\Feed;

use Guzzle\Http\Client;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Exception\RequestException;

/**
* Class Fetcher
* @package Nogo\Feedbox\Helper
*/
class Fetcher
{
/**
* @var Client
*/
protected $client;

/**
* @var int
*/
protected $timeout = 10;

/**
* @var string
*/
protected $error;

/**
* @param Client $client
*/
public function setClient(Client $client)
{
$this->client = $client;
}

/**
* @return Client
*/
public function getClient()
{
return $this->client;
}

public function getError()
{
return $this->error;
}

/**
* Set fetcher timeout
*
* @param $timeout
*/
public function setTimeout($timeout)
{
$this->timeout = intval($timeout);
}

/**
* Do get a request
* @param $uri
* @return null|string
*/
public function get($uri)
{
try {
/**
* @var $request Request
*/
$request = $this->client->get($uri, array(), array(
'timeout' => $this->timeout
));

/**
* @var $resonse Response
*/
$response = $request->send();

$result = $response->getBody(true);
} catch (RequestException $e) {
$this->error = $e->getMessage();
$result = null;
}

return $result;
}

}
7 changes: 5 additions & 2 deletions src/Nogo/Feedbox/Feed/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public function setContent($content)
if (empty($content)) {
$this->errors = 'Source content is empty.';
} else {
$content = preg_replace('/<title>(.*[&].*[;].*)<\/title>/im', '<title><![CDATA[${1}]]></title>', $content);
$content = preg_replace('/<description>(.*[&].*[;].*)<\/description>/im', '<description><![CDATA[${1}]]></description>', $content);
// TODO flag repair xml content
$content = preg_replace(
'/<(title|description)>(?!\s*<\!\[CDATA\[)(.*[&].*[;].*)(?!\]\]>\s*)<\/(title|description)>/im',
'<${1}><![CDATA[${2}]]></${1}>',
$content);

try {
Reader::registerExtension('Syndication');
Expand Down
59 changes: 0 additions & 59 deletions src/Nogo/Feedbox/Helper/Fetcher.php

This file was deleted.

72 changes: 39 additions & 33 deletions update.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Guzzle\Http\Client;
use Nogo\Feedbox\Helper\Fetcher;
use Nogo\Feedbox\Feed\Fetcher;
use Nogo\Feedbox\Helper\ConfigLoader;
use Nogo\Feedbox\Helper\DatabaseConnector;
use Nogo\Feedbox\Repository\Item;
Expand Down Expand Up @@ -92,45 +92,51 @@
$items = null;

$content = $fetcher->get($source['uri']);
/**
* @var $worker \Nogo\Feedbox\Feed\Worker
*/
$worker = new $defaultWorkerClass();
$worker->setSanitizer($sanitizer);
$worker->setContent($content);
try {
$items = $worker->execute();
} catch (\Exception $e) {
$items = null;
$error = true;
}

if (!$error && $items != null) {
foreach($items as $item) {
if (isset($item['uid'])) {
$dbItem = $itemRepository->findBy('uid', $item['uid']);
if (!empty($dbItem)) {
if ($item['content'] !== $dbItem['content']
|| $item['title'] !== $dbItem['title']) {
$item['id'] = $dbItem['id'];
$item['starred'] = $dbItem['starred'];
$item['created_at']= $dbItem['created_at'];
} else {
continue;
if (!empty($content)) {
/**
* @var $worker \Nogo\Feedbox\Feed\Worker
*/
$worker = new $defaultWorkerClass();
$worker->setSanitizer($sanitizer);
$worker->setContent($content);
try {
$items = $worker->execute();
} catch (\Exception $e) {
$items = null;
$error = true;
}

if (!$error && $items != null) {
foreach($items as $item) {
if (isset($item['uid'])) {
$dbItem = $itemRepository->findBy('uid', $item['uid']);
if (!empty($dbItem)) {
if ($item['content'] !== $dbItem['content']
|| $item['title'] !== $dbItem['title']) {
$item['id'] = $dbItem['id'];
$item['starred'] = $dbItem['starred'];
$item['created_at']= $dbItem['created_at'];
} else {
continue;
}
}
}
}

$item['source_id'] = $source['id'];
$itemRepository->persist($item);
}
$item['source_id'] = $source['id'];
$itemRepository->persist($item);
}

$source['last_update'] = date('Y-m-d H:i:s');
if (empty($source['period'])) {
$source['period'] = $worker->getUpdateInterval();
$source['last_update'] = date('Y-m-d H:i:s');
if (empty($source['period'])) {
$source['period'] = $worker->getUpdateInterval();
}
}
$source['errors'] = $worker->getErrors();
} else {
$error = true;
$source['errors'] = $fetcher->getError();
}
$source['errors'] = $worker->getErrors();
}

// update source unread counter
Expand Down

0 comments on commit e05f224

Please sign in to comment.