-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.rb
76 lines (66 loc) · 1.87 KB
/
render.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
70
71
72
73
74
75
76
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'erb'
require 'rss'
require 'httparty'
def title
ENV['RSS_TITLE'] || 'News Firehose'
end
def rss_urls
if ENV['RSS_URLS']
ENV['RSS_URLS'].split(',')
else
File.readlines('urls.txt').map(&:chomp)
end
end
def description
ENV['RSS_DESCRIPTION'] || 'View the latest news.'
end
def analytics_ua
ENV['ANALYTICS_UA']
end
def render_html
begin
html = File.open('templates/index.html.erb').read
template = ERB.new(html, trim_mode: '-')
File.open('public/index.html', 'w') do |f|
f.puts template.result(binding)
end
rescue => e
puts "Warning: Failed to render HTML. Error: #{e.message}"
end
end
def render_manifest
begin
json = File.open('templates/manifest.json.erb').read
template = ERB.new(json, trim_mode: '-')
File.open('public/manifest.json', 'w') do |f|
f.puts template.result(binding)
end
rescue => e
puts "Warning: Failed to manifest JSON. Error: #{e.message}"
end
end
# Get the feeds and parse them. We don't validate because some feeds are
# malformed slightly and break the parser.
def feed(url)
begin
response = HTTParty.get(url, timeout: 60, headers: { 'User-Agent' => 'rss-firehose feed aggregator' })
rss_content = RSS::Parser.parse(response.body, false) if response.code == 200
# If the feed is empty or nil, set the rss_content a single item stating the feed is offline
rss_content = RSS::Rss.new('2.0') if rss_content.nil? || rss_content.items.empty?
rss_content
rescue HTTParty::Error, RSS::Error => e
puts "Error fetching or parsing feed from '#{url}': #{e.class} - #{e.message}"
return "Feed currently offline: #{url}"
rescue => e
puts "General error with feed '#{url}': #{e.message}"
nil
end
end
begin
render_manifest
render_html
rescue => e
puts "Error during rendering process: #{e.message}"
end