fb_scrape 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1a9ddf896ea4be8199af9719cad12ac839bcdea
4
- data.tar.gz: 27174503c585c683b16a91afa8a7e85b9261b994
3
+ metadata.gz: 1ab5def24d779332f6b0cef2b22cad529d688965
4
+ data.tar.gz: 933385cdea599dcd0e03cba569bc2b69c1c38e4e
5
5
  SHA512:
6
- metadata.gz: 511e6752cf92cff996cf2592223696cdd9a165ffb109ab54728bb9c43840de695a242ae60c970f042f0fde09fdd2260e88001f0d7717c0008463b137bbfa8a21
7
- data.tar.gz: 9bec19ff984f8641fe08ee04826136be28e69648a7b0d0c9583581e5346ee4b55bebe72f0c93350a23834bc260411aefa4fe306ed2908dc221e832e321cafdde
6
+ metadata.gz: 0df57427570ab8c6b6a9a690590169217f7c2de8371ff02a60dfbe096a1087691dd6012b7911fa988e67f8c99c69024d8cbd6e70a89ac02d9347afb873cec090
7
+ data.tar.gz: 77ee341b0732ef947985e05dd385a921f0259bfa7d63bd1eb1fa77cfcd98ab51423478f7e39649a58a2415143171d936a1f8da459c2f7b25c7566244ef986f45
data/README.md CHANGED
@@ -80,6 +80,15 @@ Or install it yourself as:
80
80
  puts comment.comments.count
81
81
  ```
82
82
 
83
+ ### Loading an entire conversation
84
+
85
+ ```ruby
86
+ require 'fb_scrape'
87
+
88
+ conversation = FBScrape::Conversation.new(conversation_id, page_id, token)
89
+ puts conversation.messages.count
90
+ ```
91
+
83
92
  ### Using the CLI
84
93
 
85
94
  You can use the CLI to get a dump in JSON of posts and comments
@@ -2,18 +2,21 @@ require 'httparty'
2
2
 
3
3
  class FBScrape::Client
4
4
 
5
- attr_accessor :page_name, :id, :name, :posts
5
+ attr_accessor :page_name, :id, :name, :posts, :conversations
6
6
 
7
- def initialize(page_name, token_secret, id=nil, limit=nil)
7
+ def initialize(page_name, token_secret, id=nil, limit=nil, load_on_init=true)
8
8
  @page_name = page_name
9
9
  @token_secret = token_secret
10
10
  @id = id
11
11
  @posts = []
12
+ @conversations = []
12
13
  @loaded_initial = false
14
+ @loaded_initial_conversations = false
13
15
  @limit = limit
14
- if @id
16
+ @conversations_page_info = nil
17
+ if @id && load_on_init
15
18
  load_initial_posts
16
- else
19
+ elsif !@id
17
20
  get_page_id
18
21
  end
19
22
  end
@@ -40,8 +43,38 @@ class FBScrape::Client
40
43
  @page_info && next_cursor
41
44
  end
42
45
 
46
+ def can_load_more_conversations?
47
+ !is_limited? || (@conversations.count < @limit.to_i && has_more_conversations?)
48
+ end
49
+
50
+ def has_more_conversations?
51
+ @conversations_page_info && next_conversation_cursor
52
+ end
53
+
54
+ def load_conversations(limit=nil)
55
+ load_initial_conversations
56
+ @limit = limit if limit != @limit
57
+
58
+ while has_more_conversations? && can_load_more_conversations? do
59
+ load_more_conversations
60
+ end
61
+ end
62
+
43
63
  private
44
64
 
65
+ def load_initial_conversations
66
+ if !@loaded_initial_conversations
67
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/conversations?access_token=#{@token_secret}"
68
+ load_conversations_from_url url
69
+ @loaded_initial_conversations = true
70
+ end
71
+ end
72
+
73
+ def load_more_conversations
74
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/conversations?access_token=#{@token_secret}&limit=25&after=#{next_conversation_cursor}"
75
+ load_conversations_from_url url
76
+ end
77
+
45
78
  def load_initial_posts
46
79
  if !@loaded_initial
47
80
  url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/posts?fields=link,message,created_time&access_token=#{@token_secret}"
@@ -55,6 +88,19 @@ class FBScrape::Client
55
88
  load_posts_from_url url
56
89
  end
57
90
 
91
+ def load_conversations_from_url url
92
+ resp = HTTParty.get(url)
93
+ case resp.code
94
+ when 200
95
+ response = JSON.parse(resp.body)
96
+ response['data'].collect { |c| FBScrape::Conversation.new(c['id'], @id, @token_secret, false) }
97
+ @conversations = @conversations.concat(response['data'].collect { |c| FBScrape::Conversation.new(c['id'], @id, @token_secret, false) })
98
+ @conversations_page_info = response["paging"]
99
+ when 400
100
+ handle_error(resp)
101
+ end
102
+ end
103
+
58
104
  def load_posts_from_url url
59
105
  resp = HTTParty.get(url)
60
106
  case resp.code
@@ -78,6 +124,10 @@ class FBScrape::Client
78
124
  @page_info["cursors"]["after"]
79
125
  end
80
126
 
127
+ def next_conversation_cursor
128
+ @conversations_page_info["cursors"]["after"]
129
+ end
130
+
81
131
  def get_page_id
82
132
  url = "https://graph.facebook.com/#{@page_name}?access_token=#{@token_secret}"
83
133
  resp = HTTParty.get(url)
@@ -0,0 +1,51 @@
1
+ class FBScrape::Conversation
2
+
3
+ attr_accessor :id, :page_id, :messages, :updated_at
4
+
5
+ def initialize id, page_id, token, load_on_init=true
6
+ @id = id
7
+ @page_id = page_id
8
+ @token = token
9
+ @page_info = nil
10
+ @messages = []
11
+
12
+ if load_on_init
13
+ load_messages
14
+ end
15
+ end
16
+
17
+ def load_messages
18
+ load_initial_messages
19
+ end
20
+
21
+ def has_more_messages?
22
+ @page_info && next_cursor
23
+ end
24
+
25
+ private
26
+
27
+ def next_cursor
28
+ @page_info["cursors"]["after"]
29
+ end
30
+
31
+ def load_initial_messages
32
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}?access_token=#{@token}&fields=messages{message,to,from}"
33
+ load_from_url url
34
+ end
35
+
36
+ def load_more_messages
37
+ url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}?access_token=#{@token}&fields=messages{message,to,from}&limit=25&after=#{next_cursor}"
38
+ load_from_url url
39
+ end
40
+
41
+ def load_from_url url
42
+ resp = HTTParty.get(url)
43
+ case resp.code
44
+ when 200
45
+ response = JSON.parse(resp.body)
46
+ @messages = @messages.concat(response['messages']['data'].collect { |m| FBScrape::Message.new(m, @page_id) })
47
+ @page_info = response['messages']['paging']
48
+ when 400
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ class FBScrape::Message
2
+ attr_accessor :id, :created_at, :from_name, :from_id, :text, :reply_id, :to_id, :to_name
3
+
4
+
5
+ def initialize(payload, page_id=nil)
6
+ @page_id = page_id
7
+
8
+ @id = payload['id']
9
+ @text = payload['message']
10
+ @created_at = payload['created_time']
11
+
12
+ @from_id = payload['from']['id']
13
+ @from_name = payload['from']['name']
14
+ @to_id = payload['to']['data'].first['id']
15
+ @to_name = payload['to']['data'].first['name']
16
+ end
17
+
18
+ def is_incoming?
19
+ @to_id == @page_id
20
+ end
21
+
22
+ def is_reply?
23
+ !is_incoming?
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module FBScrape
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/fb_scrape.rb CHANGED
@@ -2,6 +2,8 @@ require "fb_scrape/version"
2
2
  require "fb_scrape/client"
3
3
  require "fb_scrape/post"
4
4
  require "fb_scrape/comment"
5
+ require "fb_scrape/message"
6
+ require "fb_scrape/conversation"
5
7
 
6
8
  module FBScrape
7
9
  GRAPH_VERSION = "2.10"
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.6
4
+ version: 0.0.7
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-16 00:00:00.000000000 Z
11
+ date: 2017-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,6 +128,8 @@ files:
128
128
  - lib/fb_scrape.rb
129
129
  - lib/fb_scrape/client.rb
130
130
  - lib/fb_scrape/comment.rb
131
+ - lib/fb_scrape/conversation.rb
132
+ - lib/fb_scrape/message.rb
131
133
  - lib/fb_scrape/post.rb
132
134
  - lib/fb_scrape/version.rb
133
135
  homepage: https://github.com/ongair/fb_scrape