Module: HNScraper

Defined in:
lib/hn_scraper.rb

Class Method Summary collapse

Class Method Details



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]
   = {u: username, p: password, fnid: fnid}
  cookie = nil
  RestClient.post('https://news.ycombinator.com/y', ){ |response, request, result, &block|
    cookie = response.cookies["user"]
    # if [301, 302, 307].include? response.code
    #   response.follow_redirection(request, result, &block)
    # else
    #   response.return!(request, result, &block)
    # end
  }
  cookie
end

.get_submit_fnid(cookie) ⇒ Object



8
9
10
11
12
# File 'lib/hn_scraper.rb', line 8

def get_submit_fnid cookie
  headers = { "Cookie" => "user=#{cookie}" }
  doc = Nokogiri::HTML(RestClient.get("https://news.ycombinator.com/submit", headers))
  fnid = doc.css("input[name='fnid']")[0][:value]
end


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
  cookie = (username, password)
  fnid = get_submit_fnid(cookie)
  params = {
    fnid: fnid,
    t: title
  }
  headers = {
    "Cookie" => "user=#{cookie}",
    "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

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/hn_scraper.rb', line 14

def valid_hn_cookie? cookie
  doc = Nokogiri::HTML(open("https://news.ycombinator.com/news", "Cookie" => "user=#{cookie}"))
  return !doc.css('.pagetop')[1].text.match("login")
end