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.



6
7
8
# File 'lib/verb/update.rb', line 6

def initialize
    super
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.published.to_date,
            downloaded: false,
            podcast: podcast
        )
        ep.errors.each do |error|
            @log.error error
        end

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

#check_for_new_episodes(podcast) ⇒ Object



16
17
18
19
# File 'lib/verb/update.rb', line 16

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

#get_max_pubdate(podcast) ⇒ Object



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

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)


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

def is_valid?(item, max_pub)
    enclosure = item.respond_to? :enclosure_url
    published = item.published.to_date
    enclosure && (published > max_pub.to_date)
end

#open_podcast(podcast, max_pub) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/verb/update.rb', line 25

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

#parse_feed(feed, database_podcast, max_pub) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/verb/update.rb', line 34

def parse_feed(feed, database_podcast, max_pub)
    podcast = Feedzirra::Feed.fetch_and_parse(feed.to_s)
    unless podcast.is_a? Fixnum
        pending_episodes = podcast.entries.select {|item| is_valid? item, max_pub}
    else
        raise "HTTP Error #{podcast}"
    end
    pending_episodes.each {|episode| add_eligible_episode database_podcast, episode }
    length = pending_episodes.length
    puts "#{database_podcast.title} has #{length} new episodes" if length > 0
end

#update(args) ⇒ Object



10
11
12
13
14
# File 'lib/verb/update.rb', line 10

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