Regularly import each user's RSS feed with Feedzirra in Rails

418 Views Asked by At

I'm trying to regularly import an array of RSS feeds into my Posts table in Rails. I'm trying to avoid Cron for the moment. Here's what I have so far (in my application controller).

require 'rubygems'
require 'rufus/scheduler'
require 'feedzirra'
scheduler = Rufus::Scheduler.start_new
scheduler.every '60m' do
  @users = User.all(:joins => :roles, :conditions => {:roles => {:name => "writer"}})
  @users.each do |user|
    if user.feed.blank? 

    else
      FEEDZIRRA IMPORT CODE??
    end
  end

As you've probably concluded, the array of RSS feeds comes from users with the role of Writer who've set their Feed field. The script executes every 60 minutes.

My question is how to I import the feeds without reimporting any previously imported posts? And as I'm new to programming and doing this as just a learning-curve project, if there's any constructive criticism on making my code more effective, I'm all ears.

edit: on second thought, is it appropriate for this to be in my application controller? that would create a new instance of this scheduler every time a page is loaded, right? if i'm correct in that, then where can I safely put this code?

2

There are 2 best solutions below

1
On

To answer your second question: no, this should not be run in your controller. There are several popular gems for making scheduled tasks out there. I would recommend 'whenever':

https://github.com/javan/whenever

They have good documentation and should be rather easy to follow. The tasks will be specified in a config/schedule.rb file instead of your application controller.

0
On

What you could consider doing to avoid importing previously imported feeds is created a field in you db, for example "id_unique", add any constant variable from each feed to this field, for example, the date/time from the RSS feed, then you could either validates_uniqueness_of id_unique in your model, or add a method to find_or_create by id_unique.