-
-
Save stuffmc/b342c3ed638045d1bda1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# episodes_fetch.rb (called with "episodes_fetch_ctl start" on the command line or via capistrano) | |
while($running) do | |
Episode.fetch_all | |
sleep 5 | |
end | |
# episode.rb | |
class Episode < ActiveRecord::Base | |
def self.fetch_all | |
Podcast.all.each do |podcast| | |
podcast.fetch_episodes | |
end | |
end | |
end | |
# podcast.rb | |
class Podcast < ActiveRecord::Base | |
def fetch_episodes | |
feed = init_fetch | |
success = feed.class == Feedzirra::Parser::ITunesRSS || feed.class == Feedzirra::Parser::RSS || feed.class == Feedzirra::Parser::Atom | |
update_attributes(:success => success) | |
if (success) | |
add_entries(feed.entries, feed) | |
update_attributes(:episodes_count => feed.entries.count, :fetched_at => Time.now) | |
end | |
end | |
def init_fetch | |
Feedzirra::Feed.add_feed_class(Feedzirra::Parser::ITunesRSS) | |
Feedzirra::Feed.fetch_and_parse(self.url) | |
end | |
def add_entries(entries, feed) | |
require 'htmlentities' | |
coder = HTMLEntities.new | |
entries.each do |entry| | |
entry_id = (feed.class == Feedzirra::Parser::ITunesRSS) ? entry.guid : entry.id | |
unless Episode.exists? :guid => entry_id | |
episode = episodes.create(:name => (entry.title.to_s.strip unless entry.title.blank?), | |
:url => (entry.url.to_s.strip unless entry.url.blank?), | |
:author => (entry.author.to_s.strip unless entry.author.blank?), | |
:published => (entry.published unless entry.published.blank?), | |
:guid => (entry_id.to_s.strip unless entry_id.blank?), | |
:description => (entry.summary.to_s.strip unless entry.summary.blank?), | |
:enclosure_url => (coder.decode(entry.enclosure_url.to_s.strip) unless entry.enclosure_url.blank?), | |
:enclosure_type => (entry.enclosure_type.to_s.strip unless entry.enclosure_type.blank?), | |
:enclosure_length => (entry.enclosure_length.strip unless entry.enclosure_length.blank?) | |
) | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment