Skip to content

Instantly share code, notes, and snippets.

@bronzehedwick
Last active August 29, 2015 14:15
Show Gist options
  • Save bronzehedwick/b92a78751ae93615f758 to your computer and use it in GitHub Desktop.
Save bronzehedwick/b92a78751ae93615f758 to your computer and use it in GitHub Desktop.
A ruby script to read hacker news from the terminal. I found this at http://andrewvos.com/2013/08/02/hacker-news-in-the-terminal/, but it didn't work for me, so these are my modifications. Please note, I have never programmed in ruby before.
#!/usr/bin/env ruby
require "httparty"
require "nokogiri"
require "rainbow"
require "ostruct"
require 'rainbow/ext/string'
def show_latest
response = HTTParty.get("http://news.ycombinator.com")
if response.body == @previous_body
return
end
@previous_body = response.body
html = Nokogiri::HTML(response.body)
items = []
comments = []
html.css("td.title a").each do |node|
unless node.text == "More"
items << OpenStruct.new(:points => node.parent.parent.next_sibling.css("span").text, :title => node.text,
:url => node["href"],
:comment => "https://news.ycombinator.com/#{node.parent.parent.next_sibling.css('a').drop(1).first['href']}"
)
end
break if items.size == 10
end
puts "\e[H\e[2J"
max_width = items.map {|i|
["#{i.title} #{i.points}".size, i.url.size]
}.flatten.max
puts "Hacker News".ljust(max_width).color(:red)
items.each do |item|
puts item.title.color(:yellow)
puts item.url.ljust(max_width).color(:cyan)
puts "Comments: #{item.comment.ljust(max_width)}".color(:magenta)
end
end
loop {
show_latest
sleep 60
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment