Class: Epodder::Update

Inherits:
Verb show all
Defined in:
lib/verb/update.rb

Instance Method Summary collapse

Methods inherited from Verb

#add_command, #each_argument, #lookup_args, #verb_struct

Constructor Details

#initializeUpdate

Returns a new instance of Update.



8
9
10
11
# File 'lib/verb/update.rb', line 8

def initialize
  super
  @mechanize = Mechanize.new
end

Instance Method Details

#add_eligible_episode(podcast, item) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/verb/update.rb', line 52

def add_eligible_episode podcast, item
    begin
      ep = Episode.first_or_create(
        :title => item.title,
        :url => item.enclosure.url,
        :pub_date => item.pubdate.to_date,
        :downloaded => false,
        :podcast => podcast
      )
      ep.errors.each do |error|
        @log.error error
      end

    rescue Exception => e
      @log.error e
      raise e
    end
end

#check_for_new_episodes(podcast) ⇒ Object



19
20
21
22
# File 'lib/verb/update.rb', line 19

def check_for_new_episodes podcast
    max_pub = get_max_pubdate podcast
    open_podcast podcast, max_pub
end

#get_max_pubdate(podcast) ⇒ Object



24
25
26
# File 'lib/verb/update.rb', line 24

def get_max_pubdate podcast
  Episode.max(:pub_date, :conditions => { :podcast => podcast }) || Time.at(0).to_date
end

#is_valid?(item, max_pub) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/verb/update.rb', line 48

def is_valid? item, max_pub
 item.enclosure && item.pubdate.to_date > max_pub.to_date 
end

#open_podcast(podcast, max_pub) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/verb/update.rb', line 28

def open_podcast podcast, max_pub
  begin
    @mechanize.get(podcast.uri) do |feed|
      @log.info "Maximum pubdate for #{podcast.title} - #{podcast.id} is #{max_pub}"
      parse_feed feed, podcast, max_pub
    end
  rescue StandardError => e
    puts "#{podcast.title} - #{e}"
  end
end

#parse_feed(feed, database_podcast, max_pub) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/verb/update.rb', line 39

def parse_feed feed, database_podcast, max_pub
  podcast = FeedMe.parse feed.body
  podcast.emulate_atom!
  pending_episodes = podcast.items.select {|item| is_valid? item, max_pub}
  pending_episodes.each {|pending_episode| add_eligible_episode database_podcast, pending_episode }
  length = pending_episodes.length
  puts "#{database_podcast.title} has #{length} new episodes" if length > 0
end

#update(args) ⇒ Object



13
14
15
16
17
# File 'lib/verb/update.rb', line 13

def update args
  each_argument(args) do |podcast|
    check_for_new_episodes podcast
  end
end