Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "httparty"
- require "nokogiri"
- require "rainbow"
- require "ostruct"
- 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)
- items.each do |item|
- puts item.title
- puts item.url.ljust(max_width).color("#6666FF")
- puts "Comments: #{item.comment.ljust(max_width).color("#6666FF")}"
- end
- end
- loop {
- show_latest
- sleep 60
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement