Skip to content

Commit

Permalink
add commandline subscriptions import, fix feed redirs
Browse files Browse the repository at this point in the history
  • Loading branch information
acidtv committed Mar 28, 2013
1 parent 1f1ae7d commit 8f9230f
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 68 deletions.
84 changes: 60 additions & 24 deletions application/classes/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ private function write($message)
return $this;
}

/**
* Update all feeds
*/
public function update_all()
{
// loop feeds and update
$feed = ORM::factory('Feed');
$feeds = $feed->find_all();

foreach ($feeds as $feed)
{
$this->write($feed->name . ' updating...');
$client = $this->get_client($feed);
$this->update_articles($feed, $client);
}
}

/**
* Update a single feed
*/
Expand All @@ -49,6 +32,13 @@ public function update_single(Model_Feed $feed)
throw new Exception('Cannot update articles for new feed');

$client = $this->get_client($feed);

if ( ! $client)
{
$this->write($feed->name . ' no new data ');
return;
}

$this->write($feed->name . ' got ' . $client->status());
$this->update_articles($feed, $client);
}
Expand All @@ -74,7 +64,7 @@ private function update_articles(Model_Feed $feed, $client)
'url' => $article->get_link(),
'content' => $article->get_content(),
'guid' => $article->get_id(),
'pub_date' => $article->get_date('Y-m-d H:i:s'),
'pub_date' => $article->get_gmdate('Y-m-d H:i:s'),
'author' => ($article->get_author() ? $article->get_author()->get_name() : ''),
);
$object->values($values);
Expand Down Expand Up @@ -107,14 +97,19 @@ public function add_feed($url, $user)

if ($feed->loaded())
{
$user->add('feeds', $feed);
$this->_add_feed($user, $feed);
return $feed;
}

$feed = ORM::factory('Feed');
$feed->url = $url;
$client = $this->get_client($feed);

if ( ! $client)
{
throw new Exception('RSSClient did not return any data for new feed');
}

$feed->name = $client->get_title();

try
Expand All @@ -129,14 +124,29 @@ public function add_feed($url, $user)
// this feed already exists
}

$user->add('feeds', $feed);
$this->_add_feed($user, $feed);
$this->update_articles($feed, $client);

return $feed;
}

private function _add_feed($user, $feed)
{
try
{
$user->add('feeds', $feed);
}
catch (Database_Exception $e)
{
// user is already subscribed to this feed, ignore
if ($e->getCode() != 1062)
throw $e;
}
}

/**
* Return a new RSS client
* Return a new RSS client.
* This also update the feed url if there was a permanent redirect.
*/
private function get_client($feed)
{
Expand All @@ -148,10 +158,36 @@ private function get_client($feed)
$client->etag($feed->server_etag);
}

$client->init();
try
{
$result = $client->init();
}
catch (Exception $e)
{
$this->write('Could not get feed contents: ' . $e->getMessage());
return false;
}

// permanent redirect, update url
if ($result['previous_status'] == 301)
{
$feed->url = $result['url'];
}

// not modified
if ($result['status'] == 304)
return false;
// success
elseif ($result['status'] == 200)
{
$feed->server_modified = $client->modified();
$feed->server_etag = $client->etag();
}
else
{
throw new Exception('Got invalid status: ' . $result['status']);
}

$feed->server_modified = $client->modified();
$feed->server_etag = $client->etag();
$feed->save();

return $client;
Expand Down
13 changes: 0 additions & 13 deletions application/classes/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ public function filters()
return $filters;
}

private function format_date($date)
{
$timestamp = strtotime($date);
return date('j M, H:i', $timestamp);
}

public function as_array()
{
$array = parent::as_array();
$array['_pub_date'] = $this->format_date($array['pub_date']);
return $array;
}

public function get_by_user(Model_User $user, Model_Feed $feed = null, $limit = 100)
{
$articles = ORM::factory('Article')
Expand Down
83 changes: 54 additions & 29 deletions application/classes/RSSClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class RSSClient {

public $max_redirs = 5;

private $url = null;

private $modified = null;
Expand Down Expand Up @@ -57,52 +59,72 @@ public function status()

public function init()
{
$request = Request::factory($this->url);

if ($this->modified)
{
$request->headers('If-Modified-Since', $this->modified);
}

if ($this->etag)
{
$request->headers('If-None-Match', $this->etag);
}

$request = $this->get_request();
$response = $request->execute();
$this->status = $response->status();

if ($response->headers('Date'))
// follow redirects
$redirs = 0;
$previous_status = null;
while (in_array($response->status(), array(301, 302)) && $redirs <= $this->max_redirs)
{
$this->modified = $response->headers('Date');
}
$previous_status = $response->status();
$this->url = $response->headers('Location');

if ($response->headers('ETag'))
{
$this->etag = $response->headers('ETag');
}
$request = $this->get_request();
$response = $request->execute();
$this->status = $response->status();

if ($response->status() == 304)
{
// not modified
return;
$redirs++;
}
elseif ($response->status() != 200)

if ($response->status() == 200)
{
throw new Exception('Invalid response: ' . $response->status());
// success!
$client = $this->get_client($response->body());
$this->_client = $client;
$this->items = $client->get_items();

if ($response->headers('Date'))
{
$this->modified = $response->headers('Date');
}

if ($response->headers('ETag'))
{
$this->etag = $response->headers('ETag');
}
}

$client = $this->get_client($response->body());
$this->_client = $client;

$this->items = $client->get_items();
return array(
'status' => $response->status(),
'url' => $this->url,
'previous_status' => $previous_status
);
}

public function get_items()
{
return $this->items;
}

private function get_request()
{
$request = Request::factory($this->url);

if ($this->modified)
{
$request->headers('If-Modified-Since', $this->modified);
}

if ($this->etag)
{
$request->headers('If-None-Match', $this->etag);
}

return $request;
}

private function get_client($content)
{
$client = new SimplePie();
Expand All @@ -121,6 +143,9 @@ private function get_client($content)

public function get_title()
{
if ( ! $this->_client)
return;

return $this->_client->get_title();
}
}
55 changes: 55 additions & 0 deletions application/classes/Task/Feeds/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?

class Task_Feeds_Import extends Minion_Task {

protected $_options = array(
'file' => null,
'user' => null,
);

public function _execute(array $params)
{
$xml = file_get_contents($params['file']);
$user = ORM::factory('User', $params['user']);
$feeds = new Feeds();
$feeds->register_callback(function ($msg) { Minion_CLI::write($msg);});

if ( ! $user->loaded())
throw new Exception('User does not exist');

$dom = new DOMDocument();
$dom->loadXML($xml);

$stack = array($dom);
$i = 0;

while ($parent = array_shift($stack))
{
foreach ($parent->childNodes as $node)
{
if ($node instanceof DOMElement
&& $node->tagName == 'outline'
&& $node->hasAttribute('xmlUrl'))
{
Minion_CLI::write($node->getAttribute('xmlUrl'));
try
{
$feeds->add_feed($node->getAttribute('xmlUrl'), $user);
}
catch (Exception $e)
{
Minion_CLI::write('Could not add feed: ' . $e->getMessage());
}
}

if ($node->hasChildNodes())
{
// recursion is for noobs ;)
$stack[] = $node;
}
}
}

//return $dom->saveHTML();
}
}
6 changes: 6 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

#feed-list-container {
overflow: hidden;
overflow-y: auto;
height: 90%;
}

#feed-list li {
/*width: 100px !important;*/
margin-bottom: 3px;
Expand Down
22 changes: 20 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ $(document).ready(function () {
// mark current feed as read
$('#mark-feed-read').click(function () {
$.post('/ajax/read', {feed: selected_feed}, function (data) {
alert('yay');
reset_feeds();
select_feed(selected_feed);
});
});

Expand Down Expand Up @@ -194,7 +195,7 @@ $(document).ready(function () {
feed = $('<td></td>').addClass('feed').html(value.feed.name);

title = $('<a></a>').addClass('title').html(value.title);
date = $('<span></span>').addClass('date').html(value._pub_date);
date = $('<span></span>').addClass('date').html(localize_date(value.pub_date));
content = $('<div></div>').addClass('content').html(value.content);
article = $('<td></td>')
.append(title)
Expand All @@ -214,4 +215,21 @@ $(document).ready(function () {
$('#articles').append(row);
})
}

function localize_date(utcdate) {
var now = new Date();
var date = new Date(utcdate + ' UTC');

if (''+now.getYear()+now.getMonth()+now.getDate() == ''+date.getYear()+date.getMonth()+date.getDate()) {
return date.getHours() + ':' + prefix(date.getMinutes());
}

datestr = date.getFullYear() + '-' + prefix(date.getMonth()) + '-' + prefix(date.getDate());
datestr += ' ' + prefix(date.getHours()) + ':' + prefix(date.getMinutes());
return datestr;
}

function prefix(nr) {
return ('0'+nr).substr(-2);
}
})

0 comments on commit 8f9230f

Please sign in to comment.