-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfpcasts.rb
executable file
·69 lines (53 loc) · 1.78 KB
/
fpcasts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env ruby
require 'cgi'
require 'erb'
require 'rss'
require 'yaml'
require 'ostruct'
def get_image_path!(rss, output_directory)
if (rss.channel.itunes_image)
image_location = rss.channel.itunes_image.href
else
image_location = rss.image.url
end
image_path = File.basename(image_location).split("?").first
`wget -O - #{image_location} > #{File.join(output_directory, image_path)}`
image_path
end
def feed_summary(feed_url, output_directory)
puts "[#{Time.now}] generating summary for #{feed_url}"
rss = RSS::Parser.parse(feed_url, false)
latest = rss.items.first
image = get_image_path!(rss, output_directory)
OpenStruct.new({
url: feed_url,
title: CGI.escapeHTML(rss.channel.title),
image: image,
description: CGI.escapeHTML(rss.channel.description),
latest_episode_title: CGI.escapeHTML(latest.title),
latest_episode_link: latest.link,
last_updated: latest.date.strftime("%F")
})
rescue => e
STDERR.puts "error generating summary for feed: #{feed_url}; error was: #{e.inspect}"
OpenStruct.new({
url: feed_url,
})
end
def get_feed_summaries(feed_config, output_directory)
feed_urls = YAML.load(File.read(feed_config))
feed_urls.map{ |feed_url| feed_summary(feed_url, output_directory) }.sort_by{ |feed| feed.title.downcase }
end
def main(config, template_path, output_directory)
`cp ./*.css #{output_directory}`
template = File.read(template_path)
output_filename = File.basename(template_path, '.erb')
renderer = ERB.new(template)
feeds = get_feed_summaries(config, output_directory)
output = renderer.result(binding)
output_path = File.join(output_directory, output_filename)
File.open(output_path, 'w') do |f|
f << output
end
end
main('config/feeds.yml', 'templates/index.html.erb', 'output')