fb_scrape 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ab9e7be1ccd90aba528b2d6066ca78a88d52b2f
4
+ data.tar.gz: 3332a40809f0d331fc26442b4b9a89bb24a9facd
5
+ SHA512:
6
+ metadata.gz: f25735dc3e5d82e38f70077f59a31bf55814e54378c1c8fb23472bfafe1ca76a7b6e5e74cfe6f77e569867fb9d08aaa7c37c679b2208a502b647cd7405acaafa
7
+ data.tar.gz: '08c38ed64d2007ddc243c58011ca8b2e74f0f1364076f0e828b37e647d3741084e3c5e278988863b6726e888e4cfe73be3212a0ef010674f9a39ec7835b6de5b'
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fb_scrape.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Trevor Kimenye
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,102 @@
1
+ # FB_Scrape
2
+
3
+ This gem provides a utility for scraping facebook posts and comments. You will require a facebook app id
4
+ to use this gem
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'fb_scrape'
12
+ ```
13
+
14
+ And then execute:
15
+ ```ruby
16
+ $ bundle
17
+ ```
18
+ Or install it yourself as:
19
+ ```ruby
20
+ $ gem install fb_scrape
21
+ ```
22
+ ## Usage
23
+
24
+ ### Getting information about a Facebook page
25
+
26
+ ```ruby
27
+ require 'fb_scrape'
28
+
29
+ client = FBScrape::Client.new("page_name", "access_token")
30
+ puts client.id
31
+ puts client.name
32
+ ```
33
+
34
+ ### Loading all the posts for an Facebook account
35
+
36
+ ```ruby
37
+ require 'fb_scrape'
38
+ client = FBScrape::Client.new("page_name", "access_token", "page_id")
39
+ client.load
40
+
41
+ puts client.posts.length
42
+ ```
43
+
44
+ ### Loading a post
45
+
46
+ ```ruby
47
+ require 'fb_scrape'
48
+
49
+ post = FBScrape::Post.load_from_id(id, access_token)
50
+ puts post.has_more_comments?
51
+
52
+ post.load_all_comments
53
+ puts post.has_more_comments?
54
+ ```
55
+
56
+
57
+ ### Loading a comment and its replies
58
+
59
+ ```ruby
60
+ require 'fb_scrape'
61
+
62
+ comment = FBScrape::Comment.new(payload, access_token, page_id)
63
+ comment.load_replies
64
+
65
+ puts comment.comments.count
66
+ puts comment.has_more_replies?
67
+ comment.load_all_replies
68
+ puts comment.comments.count
69
+ ```
70
+
71
+ ### Using the CLI
72
+
73
+ You can use the CLI to get a dump in JSON of posts and comments
74
+
75
+ ```
76
+ gem install fb_scrape
77
+
78
+ fb_scrape help
79
+
80
+ # get the page id for a url
81
+ fb_scrape id --page_name theusername --token token
82
+
83
+ # load all the posts for an account
84
+ fb_scrape posts --page_name theusername --token token
85
+
86
+ # load all the comments for a post
87
+ fb_scrape comments --id post_id --token token --page_id page_id
88
+ ```
89
+
90
+ ## Development
91
+
92
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
93
+
94
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ongair/ig_scrape.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fb_scrape"
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 "irb"
14
+ IRB.start(__FILE__)
data/bin/fb_scrape ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fb_scrape'
4
+ require 'thor'
5
+ require 'json'
6
+
7
+ class FBScrape::CLI < Thor
8
+
9
+ desc "id", "Get the page_id for page"
10
+ option :page_name, :required => true
11
+ option :token, :required => true
12
+ def id
13
+ begin
14
+ client = FBScrape::Client.new(options[:page_name], options[:token])
15
+ client.init
16
+ puts client.id
17
+ rescue ArgumentError => e
18
+ puts e.message
19
+ end
20
+ end
21
+
22
+ desc "posts", "Get all the posts for the page"
23
+ option :page_id, :required => true
24
+ option :token, :required => true
25
+ def posts
26
+ begin
27
+ client = FBScrape::Client.new(nil, options[:token], options[:page_id])
28
+ client.load
29
+ posts = client.posts
30
+ puts JSON.pretty_generate(posts)
31
+ rescue ArgumentError => e
32
+ puts e.message
33
+ end
34
+ end
35
+
36
+ desc "comments", "Get all the comments for a post's shortcode"
37
+ option :id, :required => true
38
+ option :token, :required => true
39
+ option :page_id, :required => false
40
+ def comments
41
+ begin
42
+ # post = FBScrape::Post.load_from_shortcode(options[:shortcode])
43
+ post = FBScrape::Post.load_from_id(options[:id], options:[token])
44
+ post.load_all_comments
45
+
46
+ comments = post.comments
47
+ puts JSON.pretty_generate(comments)
48
+ rescue ArgumentError => e
49
+ puts e.message
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ IGScrape::CLI.start(ARGV)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/fb_scrape.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fb_scrape/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fb_scrape"
8
+ spec.version = FBScrape::VERSION
9
+ spec.authors = ["Trevor Kimenye"]
10
+ spec.email = ["kimenye@gmail.com"]
11
+
12
+ spec.summary = %q{A gem to scrape facebook posts and comments}
13
+ spec.description = %q{A gem to scrape facebook posts and comments}
14
+ spec.homepage = "https://github.com/ongair/fb_scrape"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "minitest-reporters"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_dependency "httparty"
30
+ spec.add_dependency "thor"
31
+ end
@@ -0,0 +1,78 @@
1
+ require 'httparty'
2
+
3
+ class FBScrape::Client
4
+
5
+ attr_accessor :page_name, :id, :name, :posts
6
+
7
+ def initialize(page_name, token_secret, id=nil)
8
+ @page_name = page_name
9
+ @token_secret = token_secret
10
+ @id = id
11
+ @posts = []
12
+ @loaded_initial = false
13
+ if @id
14
+ load_initial_posts
15
+ else
16
+ get_page_id
17
+ end
18
+ end
19
+
20
+ def load
21
+ load_initial_posts
22
+ while has_more_posts? do
23
+ # load more posts
24
+ load_more_posts
25
+ end
26
+ end
27
+
28
+ def has_more_posts?
29
+ @page_info && next_cursor
30
+ end
31
+
32
+ private
33
+
34
+ def load_initial_posts
35
+ if !@loaded_initial
36
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/posts?access_token=#{@token_secret}"
37
+ load_posts_from_url url
38
+ @loaded_initial = true
39
+ end
40
+ end
41
+
42
+ def load_more_posts
43
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{id}/posts?access_token=#{@token_secret}&limit=25&after=#{next_cursor}"
44
+ load_posts_from_url url
45
+ end
46
+
47
+ def load_posts_from_url url
48
+ resp = HTTParty.get(url)
49
+ case resp.code
50
+ when 200
51
+ response = JSON.parse(resp.body)
52
+ more_posts = response["data"].collect { |p| FBScrape::Post.new(p) }
53
+ @posts = @posts.concat(more_posts)
54
+ @page_info = response["paging"]
55
+ end
56
+ end
57
+
58
+ def next_cursor
59
+ @page_info["cursors"]["after"]
60
+ end
61
+
62
+ def get_page_id
63
+ url = "https://graph.facebook.com/#{@page_name}?access_token=#{@token_secret}"
64
+ resp = HTTParty.get(url)
65
+
66
+ case resp.code
67
+ when 200
68
+ response = JSON.parse(resp.body)
69
+ @name = response["name"]
70
+ @id = response["id"]
71
+ when 400
72
+ response = JSON.parse(resp.body)
73
+ error = response["error"]["message"]
74
+ raise ArgumentError.new(error)
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+
3
+ class FBScrape::Comment
4
+
5
+ attr_accessor :id, :created_at, :from_name, :from_id, :message, :comments
6
+
7
+ def initialize(payload, access_token=nil, page_id=nil)
8
+ @comments = []
9
+ @access_token = access_token
10
+ @page_id = page_id
11
+ load_from_payload payload
12
+ end
13
+
14
+ def is_page_response?
15
+ @page_id == @from_id
16
+ end
17
+
18
+ def load_replies access_token=nil
19
+ @access_token = access_token if access_token
20
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@access_token}"
21
+ load_from_url url
22
+ end
23
+
24
+ def has_more_replies?
25
+ @page_info && next_cursor
26
+ end
27
+
28
+ def load_all_replies
29
+ while has_more_replies? do
30
+ load_more_replies
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def next_cursor
37
+ @page_info["cursors"]["next"]
38
+ end
39
+
40
+ def load_more_replies
41
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@access_token}&limit=25&after=next_cursor"
42
+ load_from_url url
43
+ end
44
+
45
+ def load_from_url url
46
+
47
+ resp = HTTParty.get(url)
48
+
49
+ case resp.code
50
+ when 200
51
+ response = JSON.parse(resp)
52
+ @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c, @access_token, @page_id) })
53
+ @page_info = response["paging"]
54
+ end
55
+ end
56
+
57
+ def load_from_payload payload
58
+ if payload
59
+ @id = payload['id']
60
+ @created_at = payload['created_at']
61
+ @message = payload['message']
62
+
63
+ if payload['from']
64
+ @from_name = payload['from']['name']
65
+ @from_id = payload['from']['id']
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,65 @@
1
+ require 'json'
2
+
3
+ class FBScrape::Post
4
+
5
+ attr_accessor :id, :created_at, :message, :comments
6
+
7
+ def initialize payload
8
+ @comments = []
9
+
10
+ if payload
11
+ load_from_payload(payload)
12
+ end
13
+ end
14
+
15
+ def self.load_from_id id, access_token
16
+ post = FBScrape::Post.new({ 'id' => id })
17
+ post.load_comments(access_token)
18
+ post
19
+ end
20
+
21
+ def load_comments token
22
+ @token = token
23
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@token}"
24
+ load_from_url url
25
+ end
26
+
27
+ def has_more_comments?
28
+ @page_info && next_cursor
29
+ end
30
+
31
+ def load_all_comments
32
+ while has_more_comments? do
33
+ load_more_comments
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def load_from_url url
40
+ resp = HTTParty.get(url)
41
+
42
+ case resp.code
43
+ when 200
44
+ response = JSON.parse(resp.body)
45
+ @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c) })
46
+ @page_info = response["paging"]
47
+ end
48
+ end
49
+
50
+ def load_more_comments
51
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@token}&limit=15&after=#{next_cursor}"
52
+ load_from_url url
53
+ end
54
+
55
+
56
+ def next_cursor
57
+ @page_info["cursors"]["after"]
58
+ end
59
+
60
+ def load_from_payload payload
61
+ @id = payload["id"]
62
+ @created_at = payload["created_at"]
63
+ @message = payload["message"]
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module FBScrape
2
+ VERSION = "0.0.1"
3
+ end
data/lib/fb_scrape.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "fb_scrape/version"
2
+ require "fb_scrape/client"
3
+ require "fb_scrape/post"
4
+ require "fb_scrape/comment"
5
+
6
+ module FBScrape
7
+ GRAPH_VERSION = "2.10"
8
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb_scrape
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Trevor Kimenye
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-09 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-reporters
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A gem to scrape facebook posts and comments
112
+ email:
113
+ - kimenye@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/fb_scrape
126
+ - bin/setup
127
+ - fb_scrape.gemspec
128
+ - lib/fb_scrape.rb
129
+ - lib/fb_scrape/client.rb
130
+ - lib/fb_scrape/comment.rb
131
+ - lib/fb_scrape/post.rb
132
+ - lib/fb_scrape/version.rb
133
+ homepage: https://github.com/ongair/fb_scrape
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.6.11
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: A gem to scrape facebook posts and comments
157
+ test_files: []