hackernews 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +69 -0
- data/lib/hackernews.rb +61 -0
- data/test/hackernews_test.rb +28 -0
- metadata +57 -0
data/README.rdoc
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
= HackerNews
|
2
|
+
|
3
|
+
by Tim Morgan (http://timmorgan.org)
|
4
|
+
|
5
|
+
== About
|
6
|
+
|
7
|
+
This is a fairly simple Ruby class for accessing certain parts of Hacker News (http://news.ycombinator.com). I may add functionality with time.
|
8
|
+
|
9
|
+
== Features
|
10
|
+
|
11
|
+
* Handles Hacker News session cookie and login.
|
12
|
+
* Can retreive user page, along with karma and average karma values.
|
13
|
+
|
14
|
+
== To Do
|
15
|
+
|
16
|
+
* Add ability to vote.
|
17
|
+
* Add ability to post news.
|
18
|
+
* Add ability to post comments.
|
19
|
+
|
20
|
+
== Source Code
|
21
|
+
|
22
|
+
http://github.com/seven1m/hackernews
|
23
|
+
|
24
|
+
== Installation
|
25
|
+
|
26
|
+
gem install hackernews
|
27
|
+
|
28
|
+
== Usage
|
29
|
+
|
30
|
+
require 'hackernews'
|
31
|
+
|
32
|
+
hn = HackerNews.new(USERNAME, PASSWORD)
|
33
|
+
hn.karma # => '62'
|
34
|
+
hn.average_karma # => '1.87'
|
35
|
+
|
36
|
+
== Feedback
|
37
|
+
|
38
|
+
I’d love to hear from you if you have suggestions for improvement, bug fixes, or whatever. Email me at tim@timmorgan.org or fork the project and send a pull request.
|
39
|
+
|
40
|
+
To run the tests:
|
41
|
+
|
42
|
+
HN_USERNAME=yourusername \
|
43
|
+
HN_PASSWORD=yourpassword \
|
44
|
+
ruby test/hackernews_test.rb
|
45
|
+
|
46
|
+
== License
|
47
|
+
|
48
|
+
(The MIT License)
|
49
|
+
|
50
|
+
Copyright (c) 2010 Tim Morgan
|
51
|
+
|
52
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
53
|
+
a copy of this software and associated documentation files (the
|
54
|
+
'Software'), to deal in the Software without restriction, including
|
55
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
56
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
57
|
+
permit persons to whom the Software is furnished to do so, subject to
|
58
|
+
the following conditions:
|
59
|
+
|
60
|
+
The above copyright notice and this permission notice shall be
|
61
|
+
included in all copies or substantial portions of the Software.
|
62
|
+
|
63
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
64
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
65
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
66
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
67
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
68
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
69
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/hackernews.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class HackerNews
|
6
|
+
|
7
|
+
VERSION = '0.1.0'
|
8
|
+
|
9
|
+
# Returns the version string for the library.
|
10
|
+
def self.version
|
11
|
+
VERSION
|
12
|
+
end
|
13
|
+
|
14
|
+
BASE_URL = 'http://news.ycombinator.com'
|
15
|
+
USER_URL = 'http://news.ycombinator.com/user?id=%s'
|
16
|
+
|
17
|
+
# Creates a new HackerNews object.
|
18
|
+
# Specify your username and password.
|
19
|
+
def initialize(username, password)
|
20
|
+
login_url = open(BASE_URL).read.match(/href="([^"]+)">login<\/a>/)[1]
|
21
|
+
form_html = open(BASE_URL + login_url).read
|
22
|
+
submit_url = URI.parse(BASE_URL)
|
23
|
+
response = Net::HTTP.new(submit_url.host, submit_url.port).start do |http|
|
24
|
+
req = Net::HTTP::Post.new('/y')
|
25
|
+
req.set_form_data(
|
26
|
+
'fnid' => form_html.match(/<input type=hidden name="fnid" value="([^"]+)"/)[1],
|
27
|
+
'u' => username,
|
28
|
+
'p' => password
|
29
|
+
)
|
30
|
+
http.request(req)
|
31
|
+
end
|
32
|
+
@cookie = response.header['set-cookie']
|
33
|
+
@username = username
|
34
|
+
@password = password
|
35
|
+
end
|
36
|
+
|
37
|
+
# Retrieves the karma for the logged in user, or for the specified username (if given).
|
38
|
+
def karma(username=nil)
|
39
|
+
user_page(username).match(/<td valign=top>karma\:<\/td><td>(\d+)<\/td>/)[1]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Retrieves the average karma per post for the logged in user.
|
43
|
+
def average_karma
|
44
|
+
user_page.match(/<td valign=top>avg:<\/td><td>([\d\.]+)<\/td>/)[1]
|
45
|
+
end
|
46
|
+
|
47
|
+
# Retrieves the user page html for the specified username (or the current logged in user if none is specified).
|
48
|
+
def user_page(username=nil)
|
49
|
+
username ||= @username
|
50
|
+
@user_pages ||= {}
|
51
|
+
@user_pages[username] ||= begin
|
52
|
+
url = URI.parse(USER_URL % username)
|
53
|
+
response = Net::HTTP.start(url.host, url.port) do |http|
|
54
|
+
header = {'Cookie' => @cookie}
|
55
|
+
http.get(url.path + '?' + url.query, header)
|
56
|
+
end
|
57
|
+
response.body
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/hackernews.rb'
|
3
|
+
|
4
|
+
class HackerNewsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
if ENV['HN_USERNAME'] and ENV['HN_PASSWORD']
|
8
|
+
@hn = HackerNews.new(ENV['HN_USERNAME'], ENV['HN_PASSWORD'])
|
9
|
+
else
|
10
|
+
puts 'Must set HN_USERNAME and HN_PASSWORD env variables before running.'
|
11
|
+
exit(1)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_login
|
16
|
+
assert @hn.instance_eval('@cookie') =~ /user=[a-z0-9]+;/i
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_karma
|
20
|
+
assert @hn.karma.to_s != ''
|
21
|
+
assert @hn.karma('pg').to_i > 59000
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_average_karma
|
25
|
+
assert @hn.average_karma.to_s != ''
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hackernews
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-03 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: tim@timmorgan.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- lib/hackernews.rb
|
27
|
+
- test/hackernews_test.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://rdoc.info/projects/seven1m/hackernews
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.3.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Ruby gem to login and interact with the Hacker News website.
|
56
|
+
test_files: []
|
57
|
+
|