Class: Epodder::Update
Instance Method Summary
collapse
Methods inherited from Verb
#add_command, #each_argument, #lookup_args, #verb_struct
Constructor Details
#initialize ⇒ Update
Returns a new instance of Update.
4
5
6
7
8
9
10
|
# File 'lib/verb/update.rb', line 4
def initialize
super
require 'date'
require 'feedme'
require 'mechanize'
@mechanize = Mechanize.new
end
|
Instance Method Details
#add_eligible_episode(podcast, item) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/verb/update.rb', line 51
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 StandardError => e
@log.error e
raise e
end
end
|
#check_for_new_episodes(podcast) ⇒ Object
18
19
20
21
|
# File 'lib/verb/update.rb', line 18
def check_for_new_episodes(podcast)
max_pub = get_max_pubdate podcast
open_podcast podcast, max_pub
end
|
#get_max_pubdate(podcast) ⇒ Object
23
24
25
|
# File 'lib/verb/update.rb', line 23
def get_max_pubdate(podcast)
Episode.max(:pub_date, conditions: { podcast: podcast }) || Time.at(0).to_date
end
|
#is_valid?(item, max_pub) ⇒ Boolean
47
48
49
|
# File 'lib/verb/update.rb', line 47
def is_valid?(item, max_pub)
item.enclosure && item.pubdate.to_date > max_pub.to_date
end
|
#open_podcast(podcast, max_pub) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/verb/update.rb', line 27
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
38
39
40
41
42
43
44
45
|
# File 'lib/verb/update.rb', line 38
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
12
13
14
15
16
|
# File 'lib/verb/update.rb', line 12
def update(args)
each_argument(args) do |podcast|
check_for_new_episodes podcast
end
end
|