hn_scraper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: df642ecc3557825101ec404f283c07a601669b69
4
+ data.tar.gz: 79bb2a712a007778e03c7cefa168d7bde5326b97
5
+ !binary "U0hBNTEy":
6
+ metadata.gz: 17e70d28304af6a832072771618ee58ca0edc0bc26b03e334e23182563ce5be784bbd5e5a5efeeac3a34b43b12a7d054c11664a0e8fd4a411cf20b741c4ad1f2
7
+ data.tar.gz: 4fad330662234b858fe3fcc878e76f97d5a42997ed69e93a0cf7d867a7e45b9278699fc5886819552411d3deb8886399aca06894ba7301ad5c718204d4f57939
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hn_scraper.gemspec
4
+ gemspec
5
+ gem 'rspec'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hank Stoever
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # HnScraper
2
+
3
+ A gem for logging in and posting to Hacker News
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hn_scraper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hn_scraper
18
+
19
+ ## Usage
20
+
21
+ ~~~Ruby
22
+ cookie = HNScraper.get_login_cookie("username", "password")
23
+ puts cookie
24
+ if HNScraper.valid_hn_cookie?(cookie)
25
+ url = "https://github.com/hstove/hn-scraper"
26
+ title = "A Ruby Gem for Posting to Hacker News"
27
+ HNScraper.post_to_hn("username", "password", title, url)
28
+ link = HNScraper.newest_link
29
+ puts "Posted: #{link}"
30
+ end
31
+ ~~~
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hn_scraper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hn_scraper"
8
+ spec.version = HnScraper::VERSION
9
+ spec.authors = ["Hank Stoever"]
10
+ spec.email = ["hstove@gmail.com"]
11
+ spec.description = %q{A gem for logging in and posting to Hacker News}
12
+ spec.summary = %q{A gem for logging in and posting to Hacker News}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "rest-client"
25
+ spec.add_dependency "nokogiri"
26
+ end
@@ -0,0 +1,69 @@
1
+ require "hn_scraper/version"
2
+ require 'nokogiri'
3
+ require 'rest-client'
4
+ require 'open-uri'
5
+
6
+ module HNScraper
7
+ class << self
8
+ def get_submit_fnid cookie
9
+ headers = { "Cookie" => "user=#{cookie}" }
10
+ doc = Nokogiri::HTML(RestClient.get("https://news.ycombinator.com/submit", headers))
11
+ fnid = doc.css("input[name='fnid']")[0][:value]
12
+ end
13
+
14
+ def valid_hn_cookie? cookie
15
+ doc = Nokogiri::HTML(open("https://news.ycombinator.com/news", "Cookie" => "user=#{cookie}"))
16
+ return !doc.css('.pagetop')[1].text.match("login")
17
+ end
18
+
19
+ def get_login_cookie username, password
20
+ doc = Nokogiri::HTML(RestClient.get("https://news.ycombinator.com/newslogin"))
21
+ fnid = doc.css("input[name='fnid']")[0][:value]
22
+ login_params = {u: username, p: password, fnid: fnid}
23
+ cookie = nil
24
+ RestClient.post('https://news.ycombinator.com/y', login_params){ |response, request, result, &block|
25
+ cookie = response.cookies["user"]
26
+ # if [301, 302, 307].include? response.code
27
+ # response.follow_redirection(request, result, &block)
28
+ # else
29
+ # response.return!(request, result, &block)
30
+ # end
31
+ }
32
+ cookie
33
+ end
34
+
35
+ def post_to_hn username, password, title, url, body=nil
36
+ cookie = get_login_cookie(username, password)
37
+ fnid = get_submit_fnid(cookie)
38
+ params = {
39
+ fnid: fnid,
40
+ t: title
41
+ }
42
+ headers = {
43
+ "Cookie" => "user=#{cookie}",
44
+ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
45
+ "Content-Type" => "application/x-www-form-urlencoded",
46
+ "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",
47
+ "Origin" => "https://news.ycombinator.com",
48
+ "Host" => "news.ycombinator.com"
49
+ }
50
+ if body.empty?
51
+ params[:u] = url
52
+ else
53
+ params[:x] = body
54
+ end
55
+ res = RestClient.post("https://news.ycombinator.com/r", params, headers){ |response, request, result, &block|
56
+ if [301, 302, 307].include? response.code
57
+ response.follow_redirection(request, result, &block)
58
+ else
59
+ response.return!(request, result, &block)
60
+ end
61
+ }
62
+ end
63
+
64
+ def newest_link
65
+ newest = Nokogiri::HTML(open("https://news.ycombinator.com/newest"))
66
+ hn_link = newest.css('.subtext')[0].css('a:last-child')[0][:href]
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module HnScraper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe HNScraper do
4
+ before do
5
+ @cookie = HNScraper.get_login_cookie("hnbuffer", "nwscom1225")
6
+ end
7
+
8
+ it "gets an FNID token for submitting" do
9
+ fnid = HNScraper.get_submit_fnid(@cookie)
10
+ fnid.should_not be(nil)
11
+ end
12
+
13
+ it "logs in successfully and validates correctly" do
14
+ cookie = @cookie
15
+ cookie.should_not be(nil)
16
+ # cookie.should match("user=")
17
+ doc = Nokogiri::HTML(open("https://news.ycombinator.com/news", "Cookie" => "user=#{cookie}"))
18
+ doc.css('.pagetop')[1].text.should match("hnbuffer")
19
+
20
+ end
21
+
22
+ it "validates correct cookie successfully" do
23
+ HNScraper.valid_hn_cookie?(@cookie).should eq(true)
24
+ end
25
+
26
+ it "raises error on unsuccessful login" do
27
+ HNScraper.get_login_cookie("hnbuffer", "wrong").should be_nil
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler.require
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hn_scraper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hank Stoever
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A gem for logging in and posting to Hacker News
70
+ email:
71
+ - hstove@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - hn_scraper.gemspec
82
+ - lib/hn_scraper.rb
83
+ - lib/hn_scraper/version.rb
84
+ - spec/hn_scraper_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A gem for logging in and posting to Hacker News
110
+ test_files:
111
+ - spec/hn_scraper_spec.rb
112
+ - spec/spec_helper.rb
113
+ has_rdoc: