Skip to content

Commit

Permalink
Update Ceres::Feeds::Reader, Ajaxify administration page
Browse files Browse the repository at this point in the history
  • Loading branch information
glejeune committed Mar 4, 2010
1 parent e5f5ad2 commit b2269cc
Show file tree
Hide file tree
Showing 7 changed files with 5,009 additions and 10 deletions.
62 changes: 60 additions & 2 deletions ceres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
require 'lib/feed'
require 'lib/core_ext'

Ceres::Feeds::Reader.new(20).start

# -- Modele -------------------------------------------------------------------

class Parameter < Capcode::Base
include Capcode::Resource

property :id, Serial
property :name, String
property :int_value, Integer
property :str_value, String
end

class Feed < Capcode::Base
include Capcode::Resource

Expand Down Expand Up @@ -213,6 +220,7 @@ def get
@feeds = Feed.all
@users = Moderator.all
@colors = ["#EEEEEE", "#AAAAAA"]
@reader = READER
render :erb => :administration
end
end
Expand Down Expand Up @@ -264,10 +272,60 @@ def get( id )
redirect Administration
end
end

class AjaxFeedChecking < Route '/ajax/feed/checking'
def get
render :json => READER.checking
end
end

class AjaxFeedCheckNow < Route '/ajax/feed/check'
def get
READER.populate
render 200 => "Ok"
end
end

class AjaxFeedInterval < Route '/ajax/feed/interval/(.*)'
def get( interval )
interval = interval.to_i
puts "** INFO: Change interval to #{interval}"

param = Parameter.first( :name => "interval" )
param = Parameter.new( :name => "interval" ) if param.nil?
param.int_value = interval
if param.save
READER.interval = interval

begin
READER.stop
rescue
puts "** ERROR: stop"
end

begin
READER.start
rescue
puts "** ERROR: start"
end
else
interval = READER.interval
end

render :json => interval
end
end
end

if $0 == __FILE__
Capcode.run( :db_config => "ceres.yml" ) do

p = Parameter.first( :name => "interval" )
interval = (p.nil?)?(60):(p.int_value)
READER = Ceres::Feeds::Reader.new(interval)
READER.start
puts "** interval : #{interval} seconds"

if Moderator.all.count <= 0
puts "** Create user admin with password admin..."
m = Moderator.new( :login => "admin", :realname => "Admin")
Expand Down
26 changes: 18 additions & 8 deletions lib/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,41 @@ def self.fromURL( url )
end

class Reader
attr_accessor :interval
attr_reader :checking
attr_reader :status

def initialize( interval = 60*60*6 )
# Default : we extract new feeds every 6 hours
@interval = interval
@checking = false
@status = false
end

def populate
return if @checking
puts "** START..."
@checking = true

begin
feeds = Feed.all( :active.not => "" )
feeds.each do |feed|
new_posts = false
content = FeedNormalizer::FeedNormalizer.parse open(feed.feed)

# Check all items
content.items.each do |item|
content.entries.each do |item|
# Verify if the items already existe or not
post = Post.all( :post_id => item.id )

if post.nil? or post.size == 0
new_posts = true

require 'pp'
pp item

Post.new(
:title => item.title,
:content => ((item.content.nil? or item.content.size == 0)?(item.description):(item.content)),
:date => item.last_updated || Time.now(),
:url => item.urls[0],
:date => item.date_published || Time.now(),
:url => item.url,
:post_id => item.id,
:feed => feed
).save
Expand All @@ -82,15 +88,18 @@ def populate
rescue => e
puts "** ERROR : #{e.message}"
end

ensure
@checking = false
puts "** END..."
end

def start
Thread.new do
EventMachine.run do
EM.add_periodic_timer(@interval) do
at_exit { EM.stop_event_loop }

@status = true

populate
end
end
Expand All @@ -99,6 +108,7 @@ def start

def stop
EM.stop_event_loop
@status = false
end

end
Expand Down
Binary file added static/images/loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions static/javascript/ceres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function feed_checking() {
new Ajax.Request('/ajax/feed/checking',
{
method:'get',
onSuccess: function(transport){
var response = transport.responseText || "no response text";
if( response == "true" ) {
$('checking').show()
$('not_checking').hide()
} else {
$('checking').hide()
$('not_checking').show()
}
},
onFailure: function(){ alert('Something went wrong...') }
});
}

function admin_feed() {
feed_checking()
b = setInterval(feed_checking, 10000);
}

function admin_check_now() {
new Ajax.Request('/ajax/feed/check', {method:'get'})
feed_checking()
}

function admin_change_interval() {

new Ajax.Request('/ajax/feed/interval/'+$('interval').value ,
{
method:'get',
onSuccess: function(transport){
var response = transport.responseText || "0";
$('interval').value = response
}
});
}
Loading

0 comments on commit b2269cc

Please sign in to comment.