ruby-reddit 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +8 -0
- data/README.txt +60 -0
- data/Rakefile +17 -0
- data/bin/ruby-reddit +0 -0
- data/lib/reddit.rb +91 -0
- data/test/test_helper.rb +2 -0
- data/test/test_reddit.rb +68 -0
- metadata +72 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
ruby-reddit
|
2
|
+
by Julia West
|
3
|
+
http://ruby-reddit.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Interact with reddit.com. Read links and post links (coming soon!).
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Scrapes links from reddit's hot page and new page.
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
# Get all the "hot" links on the first page
|
16
|
+
links = Reddit::Data.read :hot, :page => 0
|
17
|
+
|
18
|
+
# Check out the links!
|
19
|
+
for link in links
|
20
|
+
puts link.rank
|
21
|
+
puts link.site_id
|
22
|
+
puts link.url
|
23
|
+
puts link.title
|
24
|
+
puts link.date
|
25
|
+
end
|
26
|
+
|
27
|
+
== REQUIREMENTS:
|
28
|
+
|
29
|
+
* hpricot
|
30
|
+
* open-uri
|
31
|
+
* mechanize
|
32
|
+
|
33
|
+
== INSTALL:
|
34
|
+
|
35
|
+
* sudo gem install ruby-reddit
|
36
|
+
|
37
|
+
== LICENSE:
|
38
|
+
|
39
|
+
(The MIT License)
|
40
|
+
|
41
|
+
Copyright (c) 2008 Julia West
|
42
|
+
|
43
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
44
|
+
a copy of this software and associated documentation files (the
|
45
|
+
'Software'), to deal in the Software without restriction, including
|
46
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
47
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
48
|
+
permit persons to whom the Software is furnished to do so, subject to
|
49
|
+
the following conditions:
|
50
|
+
|
51
|
+
The above copyright notice and this permission notice shall be
|
52
|
+
included in all copies or substantial portions of the Software.
|
53
|
+
|
54
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
55
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
56
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
57
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
58
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
59
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
60
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/reddit.rb'
|
6
|
+
|
7
|
+
Hoe.new('ruby-reddit', Reddit::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'ruby-reddit'
|
9
|
+
p.author = 'Julia West'
|
10
|
+
p.email = 'juliamae@gmail.com'
|
11
|
+
p.summary = 'Plug reddit.com into your ruby apps.'
|
12
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
13
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
# vim: syntax=Ruby
|
data/bin/ruby-reddit
ADDED
File without changes
|
data/lib/reddit.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "hpricot"
|
5
|
+
require "open-uri"
|
6
|
+
require "mechanize"
|
7
|
+
|
8
|
+
module Reddit
|
9
|
+
|
10
|
+
VERSION = '0.1.0'
|
11
|
+
|
12
|
+
DefaultOptions = {
|
13
|
+
:page => 0
|
14
|
+
}
|
15
|
+
|
16
|
+
Subreddits = %w{programming science politics business gadgets sports gaming entertainment netsec}
|
17
|
+
|
18
|
+
class Data
|
19
|
+
|
20
|
+
def self.read(section, options)
|
21
|
+
conf = Reddit::DefaultOptions.update options
|
22
|
+
reader = Reader.new section, conf[:page]
|
23
|
+
reader.links
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.subreddit_url(subreddit)
|
27
|
+
"http://reddit.com/r/#{subreddit}/.rss"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class Reader
|
33
|
+
GuidRegExp = /id=t3_(.+)$/
|
34
|
+
DescriptionRegExp = /href="(.+)">\[link\]/
|
35
|
+
|
36
|
+
Urls = {
|
37
|
+
:hot => "http://reddit.com/.rss",
|
38
|
+
:new => "http://reddit.com/new.rss",
|
39
|
+
}
|
40
|
+
|
41
|
+
def initialize(section, page)
|
42
|
+
@section = section
|
43
|
+
@page = page
|
44
|
+
end
|
45
|
+
|
46
|
+
def links
|
47
|
+
index=0
|
48
|
+
collection = (page_data/:item).map do |item|
|
49
|
+
rank = link_start + (index += 1)
|
50
|
+
site_id = parse_guid((item/:guid).inner_html)
|
51
|
+
title = (item/:title).inner_html
|
52
|
+
date = (item/:"dc:date").inner_html
|
53
|
+
url = parse_description((item/:description).inner_html)
|
54
|
+
|
55
|
+
Link.new(rank, site_id, title, date, url)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_guid(guid)
|
60
|
+
GuidRegExp.match(guid)[1]
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_description(description)
|
64
|
+
DescriptionRegExp.match(description)[1]
|
65
|
+
end
|
66
|
+
|
67
|
+
def page_data
|
68
|
+
params = "?count=#{link_start}"
|
69
|
+
doc = Hpricot.XML(open("#{Urls[@section]}#{params}"))
|
70
|
+
end
|
71
|
+
|
72
|
+
def link_start
|
73
|
+
@page * 25
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Link
|
78
|
+
attr_accessor :rank, :site_id, :url, :title, :date, :points, :author
|
79
|
+
|
80
|
+
def initialize(rank, site_id, url, title, date, points=nil, author=nil)
|
81
|
+
@rank = rank
|
82
|
+
@site_id = site_id
|
83
|
+
@url = url
|
84
|
+
@title = title
|
85
|
+
@date = date
|
86
|
+
@points = points
|
87
|
+
@author = author
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/test_reddit.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class RedditTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def assert_not_blank(attribute)
|
9
|
+
!attribute.nil? && attribute != ""
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_main_urls
|
13
|
+
for section, url in Reddit::Reader::Urls
|
14
|
+
page = open(url)
|
15
|
+
assert_equal "200", page.status[0]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_subreddit_urls
|
20
|
+
# First assert a bad subreddit will throw an HTTPError
|
21
|
+
assert_raise OpenURI::HTTPError do
|
22
|
+
page = open(Reddit::Data.subreddit_url("thefakestsubreddit"))
|
23
|
+
end
|
24
|
+
|
25
|
+
# Now make sure these are all 200s
|
26
|
+
for subreddit in Reddit::Subreddits
|
27
|
+
page = open(Reddit::Data.subreddit_url(subreddit))
|
28
|
+
assert_equal "200", page.status[0]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_get_hot_links
|
33
|
+
links = Reddit::Data.read :hot, :page => 0
|
34
|
+
|
35
|
+
assert_equal 25, links.length
|
36
|
+
links.each_with_index do |link, index|
|
37
|
+
assert_equal index + 1, link.rank
|
38
|
+
assert_not_blank link.site_id
|
39
|
+
assert_not_blank link.url
|
40
|
+
assert_not_blank link.title
|
41
|
+
assert_not_blank link.date
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_parse_guid
|
46
|
+
reader = Reddit::Reader.new :hot, 0
|
47
|
+
link_start = reader.link_start
|
48
|
+
page_data = reader.page_data
|
49
|
+
|
50
|
+
item = (page_data/:item)[0]
|
51
|
+
site_id = reader.parse_guid((item/:guid).inner_html)
|
52
|
+
|
53
|
+
assert_not_blank site_id
|
54
|
+
assert site_id.length >= 4
|
55
|
+
assert /^[a-zA-Z0-9]+$/.match(site_id)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_parse_description
|
59
|
+
reader = Reddit::Reader.new :hot, 0
|
60
|
+
link_start = reader.link_start
|
61
|
+
page_data = reader.page_data
|
62
|
+
|
63
|
+
(page_data/:item).each do |item|
|
64
|
+
assert URI.parse(reader.parse_description((item/:description).inner_html))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-reddit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julia West
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-01-22 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.4.0
|
23
|
+
version:
|
24
|
+
description: "== FEATURES/PROBLEMS: * Scrapes links from reddit's hot page and new page. == SYNOPSIS: # Get all the \"hot\" links on the first page links = Reddit::Data.read :hot, :page => 0 # Check out the links! for link in links puts link.rank puts link.site_id puts link.url puts link.title puts link.date end == REQUIREMENTS: * hpricot * open-uri * mechanize"
|
25
|
+
email: juliamae@gmail.com
|
26
|
+
executables:
|
27
|
+
- ruby-reddit
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
files:
|
35
|
+
- History.txt
|
36
|
+
- Manifest.txt
|
37
|
+
- README.txt
|
38
|
+
- Rakefile
|
39
|
+
- bin/ruby-reddit
|
40
|
+
- lib/reddit.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
- test/test_reddit.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: " by Julia West"
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: ruby-reddit
|
66
|
+
rubygems_version: 1.0.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: Plug reddit.com into your ruby apps.
|
70
|
+
test_files:
|
71
|
+
- test/test_helper.rb
|
72
|
+
- test/test_reddit.rb
|