Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9d48c71647
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 76 lines (58 sloc) 2.027 kb
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
#!/usr/bin/ruby

# something's wrong, I think it's that Hacker News sometimes goes down or takes too long to respond

exit if File.new("latest.rss").stat.zero?

require 'rubygems'
gem 'hpricot', '= 0.6'
require 'feed-normalizer'
require 'open-uri'
require 'erb'

# ActiveSupport has some dooky-ass issue with motherfucking Hpricot
class String
  def blank?
    self !~ /\S/
  end
  def new_york_times_get_over_your_pretentious_bullshit_please
    self.gsub!(/’/, "'")
    self.gsub!(/“/, "\"")
    self.gsub!(/”/, "\"")
    self.gsub!(/—/, "-")
    self.gsub!(/–/, "-")
  end
end


def render_partial_story(story_number, header_number)
  story = @stories[story_number]
  partial =<<PARTIAL
<a><h<%= header_number %>><%= story.headline %></h<%= header_number %>></a>

<% unless story.text.blank? %>
<p>
<%= story.text %>
</p>
<% end %>

<p>
<strong><a href="<%= story.url %>"><%= story.domain %></a></strong>
</p>
PARTIAL
  ERB.new(partial).result(binding)
end

class Story < Struct.new(:headline, :domain, :url, :text) ; end

File.open(File.expand_path(File.dirname(__FILE__)) + "/../public/index.html", "w") do |file|
  feed = FeedNormalizer::FeedNormalizer.parse(File.read(File.expand_path(File.dirname(__FILE__)) + "/latest.rss"))
  feed.clean!

  @stories = (feed.entries.collect do |entry|
    next unless entry.url && entry.title

    title = entry.title
    text = Hpricot(entry.content).to_plain_text
    [text, title].each do |string|
      string.new_york_times_get_over_your_pretentious_bullshit_please
    end
    text.gsub!(/^Comments \[http:\/\/news\.ycombinator\.com\/item\?id=\d+\]/, "")
    text.gsub!(/\[[^\[]+\]/, "")
    text = text[0..300]

    match = /http:\/\/([^\/]+)\//.match(entry.url)
    match ? domain = match[1] : next

    # avoiding a shit-ton of Unicode
    text = "" if domain =~ /wikipedia/

    Story.new(title, domain, entry.url, text)
  end).compact
  
  file.puts ERB.new(File.read(File.expand_path(File.dirname(__FILE__)) + "/template.erb")).result(binding)
end

Something went wrong with that request. Please try again.