Class: RubyPodder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypodder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_base = "~/.rubypodder/rp") ⇒ RubyPodder

Returns a new instance of RubyPodder.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubypodder.rb', line 22

def initialize(file_base="~/.rubypodder/rp")
  @file_base = File.expand_path(file_base)
  @rp_dir = File.dirname(@file_base)
  @conf_file = @file_base + ".conf"
  @log_file = @file_base + ".log"
  @done_file = @file_base + ".done"
  create_default_config_file
  @log = Logger.new(@log_file)
  File.touch @done_file
  @date_dir = create_date_dir
end

Instance Attribute Details

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



20
21
22
# File 'lib/rubypodder.rb', line 20

def conf_file
  @conf_file
end

#date_dirObject (readonly)

Returns the value of attribute date_dir.



20
21
22
# File 'lib/rubypodder.rb', line 20

def date_dir
  @date_dir
end

#done_fileObject (readonly)

Returns the value of attribute done_file.



20
21
22
# File 'lib/rubypodder.rb', line 20

def done_file
  @done_file
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



20
21
22
# File 'lib/rubypodder.rb', line 20

def log_file
  @log_file
end

Instance Method Details

#already_downloaded(url) ⇒ Object



72
73
74
75
# File 'lib/rubypodder.rb', line 72

def already_downloaded(url)
  url_regexp = Regexp.new(url)
  File.open(@done_file).grep(url_regexp).length > 0
end

#create_date_dirObject



50
51
52
53
54
# File 'lib/rubypodder.rb', line 50

def create_date_dir
  date_dir = @rp_dir + "/" + date_string(Time.now)
  File.makedirs date_dir
  date_dir
end

#create_default_config_fileObject



34
35
36
37
38
39
# File 'lib/rubypodder.rb', line 34

def create_default_config_file
  expanded_path = File.expand_path(@conf_file)
  return if File.exists?(expanded_path)
  make_dirname(expanded_path)
  rio(expanded_path) < "http://downloads.bbc.co.uk/rmhttp/downloadtrial/radio4/thenowshow/rss.xml"
end

#date_string(time) ⇒ Object



46
47
48
# File 'lib/rubypodder.rb', line 46

def date_string(time)
  time.strftime("%Y-%m-%d")
end

#dest_file_name(url) ⇒ Object



64
65
66
# File 'lib/rubypodder.rb', line 64

def dest_file_name(url)
  @date_dir + "/" + File.basename(URI.parse(url).path)
end

#download(url) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/rubypodder.rb', line 77

def download(url)
  return if already_downloaded(url)
  @log.info("  Downloading: #{url}")
  file_name = dest_file_name(url)
  rio(file_name) < rio(url)
  record_download(url)
end

#make_dirname(full_filename) ⇒ Object



41
42
43
44
# File 'lib/rubypodder.rb', line 41

def make_dirname(full_filename)
  dirname = File.dirname(full_filename)
  File.makedirs dirname
end

#parse_rss(rss_source) ⇒ Object



60
61
62
# File 'lib/rubypodder.rb', line 60

def parse_rss(rss_source)
  RSS::Parser.parse(rss_source, false)
end

#read_feedsObject



56
57
58
# File 'lib/rubypodder.rb', line 56

def read_feeds
  IO.readlines(@conf_file).each {|l| l.chomp!}
end

#record_download(url) ⇒ Object



68
69
70
# File 'lib/rubypodder.rb', line 68

def record_download(url)
  rio(@done_file) << "#{url}\n"
end

#remove_dir_if_empty(dirname) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rubypodder.rb', line 85

def remove_dir_if_empty(dirname)
  begin
    Dir.rmdir(dirname)
  rescue SystemCallError
    @log.info("#{dirname} has contents, not removed")
  else
    @log.info("#{dirname} was empty, removed")
  end
end

#runObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubypodder.rb', line 95

def run
  @log.info("Starting")
  read_feeds.each do |url|
    http_body = rio(url).contents
    rss = parse_rss(http_body)
    @log.info("Channel: #{rss.channel.title}")
    rss.items.each do |item|
      download(item.enclosure.url)
    end
  end
  remove_dir_if_empty(@date_dir)
  @log.info("Finished")
end