Module: HNScraper
- Defined in:
- lib/hn_scraper.rb
Class Method Summary collapse
- .get_login_cookie(username, password) ⇒ Object
- .get_submit_fnid(cookie) ⇒ Object
- .newest_link ⇒ Object
- .post_to_hn(username, password, title, url, body = nil) ⇒ Object
- .valid_hn_cookie?(cookie) ⇒ Boolean
Class Method Details
.get_login_cookie(username, password) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hn_scraper.rb', line 19 def username, password doc = Nokogiri::HTML(RestClient.get("https://news.ycombinator.com/newslogin")) fnid = doc.css("input[name='fnid']")[0][:value] login_params = {u: username, p: password, fnid: fnid} = nil RestClient.post('https://news.ycombinator.com/y', login_params){ |response, request, result, &block| = response.["user"] # if [301, 302, 307].include? response.code # response.follow_redirection(request, result, &block) # else # response.return!(request, result, &block) # end } end |
.get_submit_fnid(cookie) ⇒ Object
8 9 10 11 12 |
# File 'lib/hn_scraper.rb', line 8 def get_submit_fnid headers = { "Cookie" => "user=#{}" } doc = Nokogiri::HTML(RestClient.get("https://news.ycombinator.com/submit", headers)) fnid = doc.css("input[name='fnid']")[0][:value] end |
.newest_link ⇒ Object
64 65 66 67 |
# File 'lib/hn_scraper.rb', line 64 def newest_link newest = Nokogiri::HTML(open("https://news.ycombinator.com/newest")) hn_link = newest.css('.subtext')[0].css('a:last-child')[0][:href] end |
.post_to_hn(username, password, title, url, body = nil) ⇒ Object
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 |
# File 'lib/hn_scraper.rb', line 35 def post_to_hn username, password, title, url, body=nil = (username, password) fnid = get_submit_fnid() params = { fnid: fnid, t: title } headers = { "Cookie" => "user=#{}", "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Content-Type" => "application/x-www-form-urlencoded", "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31", "Origin" => "https://news.ycombinator.com", "Host" => "news.ycombinator.com" } if body.empty? params[:u] = url else params[:x] = body end res = RestClient.post("https://news.ycombinator.com/r", params, headers){ |response, request, result, &block| if [301, 302, 307].include? response.code response.follow_redirection(request, result, &block) else response.return!(request, result, &block) end } end |
.valid_hn_cookie?(cookie) ⇒ Boolean
14 15 16 17 |
# File 'lib/hn_scraper.rb', line 14 def doc = Nokogiri::HTML(open("https://news.ycombinator.com/news", "Cookie" => "user=#{}")) return !doc.css('.pagetop')[1].text.match("login") end |