Class: KindleFeeds

Inherits:
Object
  • Object
show all
Defined in:
lib/kindle-feeds.rb

Constant Summary collapse

VERSION =
"1.0.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KindleFeeds

config is a text file with a certain format



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/kindle-feeds.rb', line 210

def initialize(config)
  @sections = [] 
  raw_sections = config.split(/^\s*$/)
  results = []
  raw_sections.each do |section|
    lines = section.strip.split("\n")
    title = lines.shift.strip 
    urls = lines.map {|line| line.strip}
    results << [title, *urls]
  end
  # an array of arrays. each array is composed of a section title followed by urls of the feeds
  results
  puts "Fetching feeds:"
  results.each do |r|
    puts "- " + r.first
    r[1..-1].each do |x|
      puts "  - " + x
    end
  end
  puts
  # subscribe
  results.each do |r|
    @sections << Section.new(r.shift, r)
  end
end

Instance Attribute Details

#sectionsObject

Returns the value of attribute sections.



208
209
210
# File 'lib/kindle-feeds.rb', line 208

def sections
  @sections
end

Class Method Details

.run(argv = ARGV) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/kindle-feeds.rb', line 256

def self.run(argv=ARGV)
  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = KindleFeeds::VERSION
    opt.banner = <<-EOT
Usage: #{opt.program_name} 

kindle-feeds reads a feed list from #{CONFIGFILE}.conf, downloads the feeds, and
generates a Kindle-compatiable and optimized HTML file that can be sent to 
[email protected] or [email protected] for conversion 
into an .azw file for reading on the Kindle. 

The first time kindle-feeds is run, it will generate a stub #{CONFIGFILE}.conf file
in the same directory. Please edit this file to specify the feeds you want to
download and convert for Kindle reading. Further instructions can be found at the
top of kindle-feeds.conf once it is generated.

Project homepage: 
http://danielchoi.com/software/kindle-feeds.html
    EOT
  end
  opts.parse! argv

  if ! File.exist?(CONFIGFILE)
    puts "Can't find #{CONFIGFILE}. Generating..."
    File.open(CONFIGFILE, "w") do |f|
      f.write DEFAULT_FEEDS
    end
    puts "Please edit #{CONFIGFILE} before running kindle-feeds again."
    exit
  end
  puts "Reading #{CONFIGFILE} for feed URLs."
  puts 
  configfile = File.open(CONFIGFILE).readlines
  configfile = configfile.select {|line| line !~ /^#/}.join.strip
  kf = KindleFeeds.new(configfile)
  kf.to_html
end

Instance Method Details

#to_htmlObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/kindle-feeds.rb', line 236

def to_html
  puts "Converting feeds into Kindle-compatible and optimized HTML..."
  puts 
  erb = ERB.new(File.read(ERB_TEMPLATE))
  out = erb.result(binding())
  # TODO put timestamp in filename
  date = Time.now.strftime('%m-%d-%Y')
  outfile = "Kindle Feeds #{date}.html"
  File.open(outfile, "w") do |f|
    f.write out
  end
  puts "Output written to file:"
  puts outfile
  puts
  puts "Email this file as an attachment to [email protected] or [email protected]."
  puts
  puts "Visit http://www.amazon.com/gp/help/customer/display.html?nodeId=200140600 for more help."
  puts "Done."
end