Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 76 lines (58 sloc) 2.027 kb
070a472 @gilesbowkett init
authored
1 #!/usr/bin/ruby
2
9d48c71 @gilesbowkett this prevents your Hacker Newspaper from going blank if Hacker News is d...
authored
3 # something's wrong, I think it's that Hacker News sometimes goes down or takes too long to respond
4
5 exit if File.new("latest.rss").stat.zero?
6
070a472 @gilesbowkett init
authored
7 require 'rubygems'
8 gem 'hpricot', '= 0.6'
9 require 'feed-normalizer'
10 require 'open-uri'
11 require 'erb'
12
13 # ActiveSupport has some dooky-ass issue with motherfucking Hpricot
14 class String
15 def blank?
16 self !~ /\S/
17 end
18 def new_york_times_get_over_your_pretentious_bullshit_please
19 self.gsub!(//, "'")
20 self.gsub!(//, "\"")
21 self.gsub!(//, "\"")
22 self.gsub!(//, "-")
23 self.gsub!(//, "-")
24 end
25 end
26
27
28 def render_partial_story(story_number, header_number)
29 story = @stories[story_number]
30 partial =<<PARTIAL
31 <a><h<%= header_number %>><%= story.headline %></h<%= header_number %>></a>
32
33 <% unless story.text.blank? %>
34 <p>
35 <%= story.text %>
36 </p>
37 <% end %>
38
39 <p>
40 <strong><a href="<%= story.url %>"><%= story.domain %></a></strong>
41 </p>
42 PARTIAL
43 ERB.new(partial).result(binding)
44 end
45
46 class Story < Struct.new(:headline, :domain, :url, :text) ; end
47
48 File.open(File.expand_path(File.dirname(__FILE__)) + "/../public/index.html", "w") do |file|
49 feed = FeedNormalizer::FeedNormalizer.parse(File.read(File.expand_path(File.dirname(__FILE__)) + "/latest.rss"))
50 feed.clean!
51
52 @stories = (feed.entries.collect do |entry|
53 next unless entry.url && entry.title
54
55 title = entry.title
56 text = Hpricot(entry.content).to_plain_text
57 [text, title].each do |string|
58 string.new_york_times_get_over_your_pretentious_bullshit_please
59 end
60 text.gsub!(/^Comments \[http:\/\/news\.ycombinator\.com\/item\?id=\d+\]/, "")
61 text.gsub!(/\[[^\[]+\]/, "")
62 text = text[0..300]
63
64 match = /http:\/\/([^\/]+)\//.match(entry.url)
65 match ? domain = match[1] : next
66
67 # avoiding a shit-ton of Unicode
68 text = "" if domain =~ /wikipedia/
69
70 Story.new(title, domain, entry.url, text)
71 end).compact
72
73 file.puts ERB.new(File.read(File.expand_path(File.dirname(__FILE__)) + "/template.erb")).result(binding)
74 end
75
Something went wrong with that request. Please try again.