reddit_comments 0.1.0 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/reddit_comments.rb +78 -0
- data/lib/reddit_comments/version.rb +3 -0
- data/reddit_comments.gemspec +32 -0
- data/spec/reddit_comments_spec.rb +79 -0
- data/spec/spec_helper.rb +2 -0
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af9696f7babb523a57b770e3ba76655616a50e5d
|
4
|
+
data.tar.gz: 35cd8331ae4c2e95efcb63f478d9b24ef1569d0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef79c8adbeae9aeb0b988034550ae88bb94c9037171a33406d69da24e577ef9789b9b89967b54baec414a4c19d21d976033afa6ed85db7695cf326a92929da41
|
7
|
+
data.tar.gz: a86939a30cd29203f123e0d44832ac7a750d92b51e96180ca75f4426ee25b23be1a52d58b5e178ca2e1ccb4cf1e6c1ed337456fea506670a7005593291f1d379
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Esther Leytush
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# RedditComments
|
2
|
+
|
3
|
+
Use this gem to pull in Reddit comments from a given Reddit page.
|
4
|
+
The comments will appear as an array of hashes, where each hash has the following:
|
5
|
+
- comment id
|
6
|
+
- comment author
|
7
|
+
- comment text
|
8
|
+
- parent id
|
9
|
+
|
10
|
+
The parent id refers to the thing that the comment was made on: so if it was a comment on the original post, the id will refer
|
11
|
+
to that, otherwise it will refer to the comment on which THIS comment was made.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'reddit_comments'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install reddit_comments
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Get the URL of the page from which you would like to retrieve comments. The URL should look something like this:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
url = "https://www.reddit.com/r/worldnews/comments/4ozup2/cracks_emerge_in_the_european_consensus_on_russia/"
|
35
|
+
```
|
36
|
+
|
37
|
+
(The gem will append ```/.json``` to your URL unless you feed it a link with the ```/.json``` already appended.)
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
RedditComments.retrieve(url)
|
41
|
+
```
|
42
|
+
|
43
|
+
### Example usage:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'reddit_comments'
|
47
|
+
|
48
|
+
url = "https://www.reddit.com/r/worldnews/comments/4ozup2/cracks_emerge_in_the_european_consensus_on_russia/"
|
49
|
+
|
50
|
+
RedditComments.retrieve(url)
|
51
|
+
```
|
52
|
+
Result:
|
53
|
+
|
54
|
+
```wrap:space
|
55
|
+
[
|
56
|
+
{"id"=>"d4gvid3",
|
57
|
+
"parent_id"=>"t3_4ozup2"
|
58
|
+
"author"=>"extremelycynical",
|
59
|
+
"body"=>"As a European (and German, considering the content of the article) I can only say that I find it completely unacceptable that we impose sanctions on Russia. \n\nIt's nothing but the blind application of glaringly obvious and undeniable double standards as well as mind-boggling hypocrisy.\n\nRussia annexing Crimea is a move that by the best estimates can only be called *supported* by the Crimean people and a direct response to escalating US destabilization in the region akin to the destabilization it caused in Syria. Russia saw that the US took away Tartus and the Golan Heights and now wants to take away Sevastopol and put an end to it. And those sanctions come without regarding the wishes of the Crimean people or in any way negotiating with Russia.\n\nIf we sanction Russia why don't we sanction Israel? (A nation that is committing far worse crimes and is annexing more and more land constantly?) \nWhy don't we sanction the US? A warmongering nation that is committing war crimes and is violating the fundamental human rights of any internet user? \nWhy don't we sanction Saudi Arabia? A country supporting radical Islam and terrorism?\n\nIt is quite undeniable that these sanctions are entirely based on US interests and the anti-Russian agenda pushed for by that country. These sanctions do not contribute to peace. These sanctions don't exist for humanitarian reasons and us caring about human rights. These sanctions don't even exist because anyone actually believes that Russia deserves them. They exist because the West hates Russia. And we need to stop pretending that Russia deserves our sanctions any more than countries like Israel, the US or Saudi Arabia. And we need to face the fact that before we sanction Russia we should first of all sanction those kind of countries."},
|
60
|
+
|
61
|
+
{"id"=>"d4gx7w3",
|
62
|
+
"parent_id"=>"t1_d4gvid3"
|
63
|
+
"author"=>"Esquina1",
|
64
|
+
"body"=>"Good points! I may add that in my view the Crimean case is the correction of a late Soviet-Union-internal mistake which should be solved bilaterally between the sucessors of the Soviet-Union. As for the East Ukrainian secesssion areas, Russia should find a solution to pull out and help with convincing evidence to solve the MH 17 case and indemnify the bereaved."},
|
65
|
+
|
66
|
+
{"id"=>"d4gxtf8",
|
67
|
+
"parent_id"=>"t1_d4gvid3"
|
68
|
+
"author"=>"princefalcon",
|
69
|
+
"body"=>">\nIt is quite undeniable that these sanctions are entirely based on US interests \n>\n\nNot entirely surely? Russia ruled what are now the Ukraine, the Baltic republics, Belarus, Poland right through the 19C as a result of being one of the European great powers and the tendency for the more powerful states to get what they wanted back then. Some of those ethnicities became independent countries after WW1, but got forced into membership of the Warsaw Pact after WW2, so were directly ruled by the USSR or were satellites. There is some doubt about how much Russia respects the principle of national sovereignty. Ideally Russia itself would join the EU so that we could be a happy family of nations, but that doesn't seem likely."}
|
70
|
+
]
|
71
|
+
```
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports, pull requests, and feature requests are welcome. Please fork this repo, pull down to your local machine from your fork, push up your changes, and make a pull request from your fork to this repo.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "reddit_comments"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "pry"
|
14
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require "reddit_comments/version"
|
2
|
+
require 'net/http'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module RedditComments
|
7
|
+
class IncorrectLinkFormat < StandardError; end
|
8
|
+
|
9
|
+
def self.retrieve(link)
|
10
|
+
comments = GetComments.new(link)
|
11
|
+
comments.retrieve
|
12
|
+
end
|
13
|
+
|
14
|
+
class GetComments
|
15
|
+
attr_accessor :url, :comments, :request, :post
|
16
|
+
|
17
|
+
def initialize(url)
|
18
|
+
@url = url
|
19
|
+
@comments = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def recursive_comment_digging(child, comments=[])
|
23
|
+
post = {}
|
24
|
+
post["id"] = child["id"]
|
25
|
+
post["parent_id"] = child["parent_id"]
|
26
|
+
post["author"] = child["author"]
|
27
|
+
post["body"] = child["body"]
|
28
|
+
|
29
|
+
comments << post
|
30
|
+
|
31
|
+
if child["replies"] != nil && child["replies"] != ""
|
32
|
+
child["replies"]["data"]["children"].each do |comment|
|
33
|
+
comments << self.recursive_comment_digging(comment["data"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
comments
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_comments
|
40
|
+
@post[1]["data"]["children"].each do |child|
|
41
|
+
@comments << recursive_comment_digging(child["data"])
|
42
|
+
end
|
43
|
+
@comments = comments.flatten
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_post
|
47
|
+
uri = URI.parse(url.dup)
|
48
|
+
http = Net::HTTP.new(uri.host)
|
49
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
50
|
+
request.initialize_http_header({"User-Agent" => "Reddit_Comments_Gem_0.1_mindplace"})
|
51
|
+
body = http.request(request)
|
52
|
+
@post = JSON.parse(body.body)
|
53
|
+
end
|
54
|
+
|
55
|
+
def append_json
|
56
|
+
unless url.match(/\/.json\z/)
|
57
|
+
if url[-1] == "/"
|
58
|
+
@url = url + ".json"
|
59
|
+
else
|
60
|
+
@url = url + "/.json"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def link_tested
|
66
|
+
unless url.match(/https:\/\/www.reddit.com\/r\/(.*)\/comments\/(.*)\/(.*)\//)
|
67
|
+
raise IncorrectLinkFormat, "Not an acceptable Reddit link, please see documentation."
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def retrieve
|
72
|
+
link_tested
|
73
|
+
append_json
|
74
|
+
parse_post
|
75
|
+
get_comments
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'reddit_comments/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "reddit_comments"
|
8
|
+
spec.version = RedditComments::VERSION
|
9
|
+
spec.authors = ["Esther Leytush"]
|
10
|
+
spec.email = ["eleytush@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Grabs all comments from a given Reddit page and delivers as an array of hashes."
|
13
|
+
spec.homepage = "https://github.com/mindplace/reddit_comments_gem"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files`.split("\n")
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
describe RedditComments do
|
5
|
+
it 'has a version number' do
|
6
|
+
expect(RedditComments::VERSION).not_to be nil
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'link_tested' do
|
10
|
+
it 'raises error if given a non-Reddit link' do
|
11
|
+
url = 'http://www.example.com'
|
12
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
13
|
+
|
14
|
+
expect{ reddit_comments.link_tested }.to raise_error(RedditComments::IncorrectLinkFormat)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises error for Reddit link if not correct format' do
|
18
|
+
url = 'https://www.reddit.com/r/worldnews/comments/were_living_worse_than_in_a_war_venezuelas/'
|
19
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
20
|
+
|
21
|
+
expect{ reddit_comments.link_tested }.to raise_error(RedditComments::IncorrectLinkFormat)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'passes an acceptabe Reddit link' do
|
25
|
+
url = 'https://www.reddit.com/r/worldnews/comments/4p4orb/were_living_worse_than_in_a_war_venezuelas/'
|
26
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
27
|
+
|
28
|
+
expect{ reddit_comments.link_tested }.not_to raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'append_json' do
|
33
|
+
it 'appends .json to the link if not present' do
|
34
|
+
url = 'https://www.reddit.com/r/worldnews/comments/4p4orb/were_living_worse_than_in_a_war_venezuelas/'
|
35
|
+
updated = url + '.json'
|
36
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
37
|
+
reddit_comments.append_json
|
38
|
+
|
39
|
+
expect(reddit_comments.url).to eq(updated)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not append if present' do
|
43
|
+
url = 'https://www.reddit.com/r/worldnews/comments/4p4orb/were_living_worse_than_in_a_war_venezuelas/.json'
|
44
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
45
|
+
reddit_comments.append_json
|
46
|
+
|
47
|
+
expect(reddit_comments.url).to eq(url)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'parse_post' do
|
52
|
+
it 'takes a url and returns JSON object of url content' do
|
53
|
+
url = 'https://www.reddit.com/r/worldnews/comments/4p4orb/were_living_worse_than_in_a_war_venezuelas/.json'
|
54
|
+
reddit_comments = RedditComments::GetComments.new(url)
|
55
|
+
reddit_comments.parse_post
|
56
|
+
|
57
|
+
expect(reddit_comments.post).to be_an_instance_of(Array)
|
58
|
+
expect(reddit_comments.post[0]).to be_an_instance_of(Hash)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'get_comments' do
|
63
|
+
let(:url) { "https://www.reddit.com/r/worldnews/comments/4p71mu/migrant_protests_calais_france_tried_to_force_way/.json" }
|
64
|
+
let(:comments) { RedditComments::GetComments.new(url).retrieve }
|
65
|
+
|
66
|
+
it 'returns an array of comment hashes' do
|
67
|
+
expect(comments).to be_an_instance_of(Array)
|
68
|
+
|
69
|
+
comments.each do |comment|
|
70
|
+
expect(comment).to be_an_instance_of(Hash)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'includes expected comment text' do
|
75
|
+
comment = comments.select{|c| c["body"] == "That moment you realize brexit does nothing to stop this."}
|
76
|
+
expect(comment).not_to be_empty
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reddit_comments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esther Leytush
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,7 +58,21 @@ email:
|
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
|
-
files:
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/reddit_comments.rb
|
72
|
+
- lib/reddit_comments/version.rb
|
73
|
+
- reddit_comments.gemspec
|
74
|
+
- spec/reddit_comments_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
62
76
|
homepage: https://github.com/mindplace/reddit_comments_gem
|
63
77
|
licenses:
|
64
78
|
- MIT
|
@@ -80,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
94
|
version: '0'
|
81
95
|
requirements: []
|
82
96
|
rubyforge_project:
|
83
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.6.7
|
84
98
|
signing_key:
|
85
99
|
specification_version: 4
|
86
100
|
summary: Grabs all comments from a given Reddit page and delivers as an array of hashes.
|