Class: RubyPodder

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

Constant Summary collapse

Version =
'rubypodder v0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RubyPodder.



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

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.



22
23
24
# File 'lib/rubypodder.rb', line 22

def conf_file
  @conf_file
end

#date_dirObject (readonly)

Returns the value of attribute date_dir.



22
23
24
# File 'lib/rubypodder.rb', line 22

def date_dir
  @date_dir
end

#done_fileObject (readonly)

Returns the value of attribute done_file.



22
23
24
# File 'lib/rubypodder.rb', line 22

def done_file
  @done_file
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



22
23
24
# File 'lib/rubypodder.rb', line 22

def log_file
  @log_file
end

Instance Method Details

#already_downloaded(url) ⇒ Object



75
76
77
78
# File 'lib/rubypodder.rb', line 75

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

#create_date_dirObject



52
53
54
55
56
# File 'lib/rubypodder.rb', line 52

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

#create_default_config_fileObject



36
37
38
39
40
41
# File 'lib/rubypodder.rb', line 36

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\n"
end

#date_string(time) ⇒ Object



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

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

#dest_file_name(url) ⇒ Object



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

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

#download(url) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubypodder.rb', line 80

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

#make_dirname(full_filename) ⇒ Object



43
44
45
46
# File 'lib/rubypodder.rb', line 43

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

#parse_rss(rss_source) ⇒ Object



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

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

#read_feedsObject



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

def read_feeds
  #IO.readlines(@conf_file).each {|l| l.chomp!}
  a = rio(@conf_file).chomp.readlines.reject {|i| i =~ /^#/}
end

#record_download(url) ⇒ Object



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

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

#remove_dir_if_empty(dirname) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/rubypodder.rb', line 92

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubypodder.rb', line 102

def run
  @log.info("Starting (#{Version})")
  read_feeds.each do |url|
    begin
      http_body = rio(url).contents
    rescue
      @log.error("  Can't read from #{url}")
      next
    end
    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