fb_scrape 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ab9e7be1ccd90aba528b2d6066ca78a88d52b2f
4
- data.tar.gz: 3332a40809f0d331fc26442b4b9a89bb24a9facd
3
+ metadata.gz: 6c8ee7a39b2206dd1a81a1a5cb63bad79576d62d
4
+ data.tar.gz: 6007835efa4e40368fa8cbcf210ea9209e837218
5
5
  SHA512:
6
- metadata.gz: f25735dc3e5d82e38f70077f59a31bf55814e54378c1c8fb23472bfafe1ca76a7b6e5e74cfe6f77e569867fb9d08aaa7c37c679b2208a502b647cd7405acaafa
7
- data.tar.gz: '08c38ed64d2007ddc243c58011ca8b2e74f0f1364076f0e828b37e647d3741084e3c5e278988863b6726e888e4cfe73be3212a0ef010674f9a39ec7835b6de5b'
6
+ metadata.gz: be7eb5fc04239dba07944d6a21a9697896ace34336ad51a62675bc8b89037da7d2a2e12c26b8213c27a787604cacb092030486368f02179fd5102afca976be2a
7
+ data.tar.gz: 854edabfbc131bce88a1d87d9aa2358100b253b703f0eca483e54491ceec3df76cba2893f093a1fcf5069e39b678926ad06552f685cf3baa77ce09a6d44b8b57
data/README.md CHANGED
@@ -41,6 +41,18 @@ Or install it yourself as:
41
41
  puts client.posts.length
42
42
  ```
43
43
 
44
+ #### Limiting the number of posts
45
+
46
+ You can limit the number of posts returned
47
+
48
+ ```ruby
49
+ require 'fb_scrape'
50
+ client = FBScrape::Client.new("page_name", "access_token", "page_id", 10)
51
+ client.load
52
+
53
+ puts client.posts.length <= 10
54
+ ```
55
+
44
56
  ### Loading a post
45
57
 
46
58
  ```ruby
data/bin/fb_scrape CHANGED
@@ -6,13 +6,17 @@ require 'json'
6
6
 
7
7
  class FBScrape::CLI < Thor
8
8
 
9
+ desc "version", "Get the version of the client running"
10
+ def version
11
+ puts FBScrape::VERSION
12
+ end
13
+
9
14
  desc "id", "Get the page_id for page"
10
15
  option :page_name, :required => true
11
16
  option :token, :required => true
12
17
  def id
13
18
  begin
14
19
  client = FBScrape::Client.new(options[:page_name], options[:token])
15
- client.init
16
20
  puts client.id
17
21
  rescue ArgumentError => e
18
22
  puts e.message
@@ -22,14 +26,17 @@ class FBScrape::CLI < Thor
22
26
  desc "posts", "Get all the posts for the page"
23
27
  option :page_id, :required => true
24
28
  option :token, :required => true
29
+ option :limit, :required => false
25
30
  def posts
26
31
  begin
32
+ limit = options[:limit] || nil
27
33
  client = FBScrape::Client.new(nil, options[:token], options[:page_id])
28
- client.load
34
+ client.load(limit)
35
+
29
36
  posts = client.posts
30
37
  puts JSON.pretty_generate(posts)
31
38
  rescue ArgumentError => e
32
- puts e.message
39
+ puts "Error:" + e.message
33
40
  end
34
41
  end
35
42
 
@@ -39,8 +46,11 @@ class FBScrape::CLI < Thor
39
46
  option :page_id, :required => false
40
47
  def comments
41
48
  begin
42
- # post = FBScrape::Post.load_from_shortcode(options[:shortcode])
43
- post = FBScrape::Post.load_from_id(options[:id], options:[token])
49
+
50
+ token = options[:token]
51
+ id = options[:id]
52
+
53
+ post = FBScrape::Post.load_from_id(id, token)
44
54
  post.load_all_comments
45
55
 
46
56
  comments = post.comments
@@ -52,4 +62,4 @@ class FBScrape::CLI < Thor
52
62
 
53
63
  end
54
64
 
55
- IGScrape::CLI.start(ARGV)
65
+ FBScrape::CLI.start(ARGV)
@@ -4,12 +4,13 @@ class FBScrape::Client
4
4
 
5
5
  attr_accessor :page_name, :id, :name, :posts
6
6
 
7
- def initialize(page_name, token_secret, id=nil)
7
+ def initialize(page_name, token_secret, id=nil, limit=nil)
8
8
  @page_name = page_name
9
9
  @token_secret = token_secret
10
10
  @id = id
11
11
  @posts = []
12
12
  @loaded_initial = false
13
+ @limit = limit
13
14
  if @id
14
15
  load_initial_posts
15
16
  else
@@ -17,14 +18,24 @@ class FBScrape::Client
17
18
  end
18
19
  end
19
20
 
20
- def load
21
+ def load(limit=nil)
21
22
  load_initial_posts
22
- while has_more_posts? do
23
+ @limit = limit if limit != @limit
24
+
25
+ while has_more_posts? && is_under_limit? do
23
26
  # load more posts
24
27
  load_more_posts
25
28
  end
26
29
  end
27
30
 
31
+ def is_under_limit?
32
+ !is_limited? || @posts.count < @limit.to_i
33
+ end
34
+
35
+ def is_limited?
36
+ !@limit.nil?
37
+ end
38
+
28
39
  def has_more_posts?
29
40
  @page_info && next_cursor
30
41
  end
@@ -33,14 +44,14 @@ class FBScrape::Client
33
44
 
34
45
  def load_initial_posts
35
46
  if !@loaded_initial
36
- url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/posts?access_token=#{@token_secret}"
47
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/posts?fields=link,message,created_time&access_token=#{@token_secret}"
37
48
  load_posts_from_url url
38
49
  @loaded_initial = true
39
50
  end
40
51
  end
41
52
 
42
53
  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}"
54
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{id}/posts?fields=link,message,created_time&access_token=#{@token_secret}&limit=25&after=#{next_cursor}"
44
55
  load_posts_from_url url
45
56
  end
46
57
 
@@ -52,9 +63,17 @@ class FBScrape::Client
52
63
  more_posts = response["data"].collect { |p| FBScrape::Post.new(p) }
53
64
  @posts = @posts.concat(more_posts)
54
65
  @page_info = response["paging"]
66
+ when 400
67
+ handle_error(resp)
55
68
  end
56
69
  end
57
70
 
71
+ def handle_error resp
72
+ response = JSON.parse(resp.body)
73
+ error = response["error"]["message"]
74
+ raise ArgumentError.new(error)
75
+ end
76
+
58
77
  def next_cursor
59
78
  @page_info["cursors"]["after"]
60
79
  end
@@ -69,9 +88,7 @@ class FBScrape::Client
69
88
  @name = response["name"]
70
89
  @id = response["id"]
71
90
  when 400
72
- response = JSON.parse(resp.body)
73
- error = response["error"]["message"]
74
- raise ArgumentError.new(error)
91
+ handle_error(resp)
75
92
  end
76
93
  end
77
94
 
@@ -2,10 +2,10 @@ require 'json'
2
2
 
3
3
  class FBScrape::Comment
4
4
 
5
- attr_accessor :id, :created_at, :from_name, :from_id, :message, :comments
5
+ attr_accessor :id, :created_at, :from_name, :from_id, :page_id, :message, :replies
6
6
 
7
7
  def initialize(payload, access_token=nil, page_id=nil)
8
- @comments = []
8
+ @replies = []
9
9
  @access_token = access_token
10
10
  @page_id = page_id
11
11
  load_from_payload payload
@@ -31,6 +31,17 @@ class FBScrape::Comment
31
31
  end
32
32
  end
33
33
 
34
+ def to_json(*args)
35
+ JSON.pretty_generate({
36
+ id: @id,
37
+ created_at: @created_at,
38
+ from_name: @from_name,
39
+ from_id: @from_id,
40
+ page_id: @page_id,
41
+ message: @message
42
+ })
43
+ end
44
+
34
45
  private
35
46
 
36
47
  def next_cursor
@@ -48,16 +59,20 @@ class FBScrape::Comment
48
59
 
49
60
  case resp.code
50
61
  when 200
51
- response = JSON.parse(resp)
52
- @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c, @access_token, @page_id) })
62
+ response = JSON.parse(resp.body)
63
+ @replies = @replies.concat(response["data"].collect{ |c| FBScrape::Comment.new(c, @access_token, @page_id) })
53
64
  @page_info = response["paging"]
65
+ when 400
66
+ response = JSON.parse(resp.body)
67
+ error = response["error"]["message"]
68
+ raise ArgumentError.new(error)
54
69
  end
55
70
  end
56
71
 
57
72
  def load_from_payload payload
58
73
  if payload
59
74
  @id = payload['id']
60
- @created_at = payload['created_at']
75
+ @created_at = payload['created_time']
61
76
  @message = payload['message']
62
77
 
63
78
  if payload['from']
@@ -2,24 +2,25 @@ require 'json'
2
2
 
3
3
  class FBScrape::Post
4
4
 
5
- attr_accessor :id, :created_at, :message, :comments
5
+ attr_accessor :id, :created_at, :message, :comments, :link
6
6
 
7
- def initialize payload
7
+ def initialize payload, page_id=nil, token=nil
8
8
  @comments = []
9
+ @page_id = page_id
10
+ @token = token
9
11
 
10
12
  if payload
11
13
  load_from_payload(payload)
12
14
  end
13
15
  end
14
16
 
15
- def self.load_from_id id, access_token
16
- post = FBScrape::Post.new({ 'id' => id })
17
- post.load_comments(access_token)
17
+ def self.load_from_id id, access_token, page_id=nil
18
+ post = FBScrape::Post.new({ 'id' => id }, page_id, access_token)
19
+ post.load_comments
18
20
  post
19
21
  end
20
22
 
21
- def load_comments token
22
- @token = token
23
+ def load_comments
23
24
  url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@token}"
24
25
  load_from_url url
25
26
  end
@@ -34,16 +35,29 @@ class FBScrape::Post
34
35
  end
35
36
  end
36
37
 
38
+ def to_json(*args)
39
+ JSON.pretty_generate({
40
+ id: @id,
41
+ created_at: @created_at,
42
+ message: @message,
43
+ link: @link
44
+ })
45
+ end
46
+
47
+
37
48
  private
38
49
 
39
50
  def load_from_url url
40
51
  resp = HTTParty.get(url)
41
-
42
52
  case resp.code
43
53
  when 200
44
54
  response = JSON.parse(resp.body)
45
- @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c) })
55
+ @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c, @token, @page_id) })
46
56
  @page_info = response["paging"]
57
+ when 400
58
+ response = JSON.parse(resp.body)
59
+ error = response["error"]["message"]
60
+ raise ArgumentError.new(error)
47
61
  end
48
62
  end
49
63
 
@@ -59,7 +73,8 @@ class FBScrape::Post
59
73
 
60
74
  def load_from_payload payload
61
75
  @id = payload["id"]
62
- @created_at = payload["created_at"]
76
+ @created_at = payload["created_time"]
63
77
  @message = payload["message"]
78
+ @link = payload["link"]
64
79
  end
65
80
  end
@@ -1,3 +1,3 @@
1
1
  module FBScrape
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fb_scrape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Kimenye
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler