Advertisement
jespi

HN with comments

Aug 2nd, 2013
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.09 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "httparty"
  4. require "nokogiri"
  5. require "rainbow"
  6. require "ostruct"
  7.  
  8. def show_latest
  9.   response = HTTParty.get("http://news.ycombinator.com")
  10.   if response.body == @previous_body
  11.     return
  12.   end
  13.   @previous_body = response.body
  14.  
  15.   html = Nokogiri::HTML(response.body)
  16.   items = []
  17.   comments = []
  18.   html.css("td.title a").each do |node|
  19.     unless node.text == "More"
  20.       items << OpenStruct.new(:points => node.parent.parent.next_sibling.css("span").text, :title => node.text,
  21.         :url => node["href"],
  22.         :comment => "https://news.ycombinator.com/#{node.parent.parent.next_sibling.css('a').drop(1).first['href']}"
  23.         )
  24.     end
  25.     break if items.size == 10
  26.   end
  27.  
  28.   puts "\e[H\e[2J"
  29.  
  30.   max_width = items.map {|i|
  31.     ["#{i.title} #{i.points}".size, i.url.size]
  32.   }.flatten.max
  33.  
  34.   puts "Hacker News".ljust(max_width)
  35.   items.each do |item|
  36.     puts item.title
  37.     puts item.url.ljust(max_width).color("#6666FF")
  38.     puts "Comments: #{item.comment.ljust(max_width).color("#6666FF")}"
  39.   end
  40. end
  41.  
  42. loop {
  43.   show_latest
  44.   sleep 60
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement