Commit 30f50a9fbdbb3a31e5771652d56b3d289a8a6c23

Authored by Evandro Junior
1 parent 2d7c6dcb
Exists in master

const missing to be analised

facebook_stream/lib_facebook_koala_stream.rb 0 → 100644
@@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
  1 +require 'rubygems'
  2 +require 'koala'
  3 +require 'json'
  4 +
  5 +#Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!!
  6 +#token will expire at 12/04/2014 (Brazilian date format) + 59 days
  7 +#'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
  8 +# BACKUP TOKEN 'CAAEhsewl0ZAcBAHhipXszZCURSwWLmgvceDbs9mB5baJdLriFxYMEzywmF2fvZBuThuA2Mm7QF8wPd3E6R5pVqVEnC2VhcBb4VrfAnkZC73ZC5g1NRUnKZCB2e6CaRiUBDatR2nf505PeKp7Aj5XxvTdfSqdZCsXxQFYZApPNSUUgkUWm6HwL4rp21MRJXb612sZD'
  9 +
  10 +def facebook_comments(hub, author_id, page_id, pooling_time, token, proxy_url)
  11 + pooling_time ||= 5
  12 + Koala.http_service.http_options = { :proxy => proxy_url } unless proxy_url.blank?
  13 +
  14 + @graph = Koala::Facebook::API.new(token)
  15 + initialComments = []
  16 + firstTime = true
  17 + while true
  18 + feed = @graph.get_connections(page_id, "posts")
  19 + array = []
  20 + extractedComments = []
  21 + feed.each {|f|
  22 + if f['comments'] != nil && f['comments']['data'] != nil
  23 + array.push(f['comments']['data'])
  24 + end
  25 + }
  26 + extractedComments = array.flatten.uniq
  27 + if firstTime
  28 + initialComments = extractedComments.clone
  29 + firstTime = false
  30 + end
  31 + newComments = extractedComments - initialComments
  32 + newComments = newComments.uniq
  33 + initialComments += newComments
  34 + initialComments = initialComments.uniq
  35 + newComments.each{|comment|
  36 + puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}"
  37 + noosferoComment = Comment.new
  38 + noosferoComment.title = 'hub-message-facebook'
  39 + noosferoComment.source = hub
  40 + noosferoComment.body = comment['message']
  41 + noosferoComment.author_id = author_id
  42 + noosferoComment.name = comment['from']['name']
  43 + noosferoComment.email = 'admin@localhost.local'
  44 + noosferoComment.save!
  45 + }
  46 + sleep(pooling_time)
  47 + end
  48 +end
facebook_stream/lib_facebook_stream.rb
1 require 'rubygems' 1 require 'rubygems'
2 -require 'koala' 2 +require 'open-uri'
3 require 'json' 3 require 'json'
4 4
5 -#Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!!  
6 -#token will expire at 12/04/2014 (Brazilian date format) + 59 days  
7 -#'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'  
8 -# BACKUP TOKEN 'CAAEhsewl0ZAcBAHhipXszZCURSwWLmgvceDbs9mB5baJdLriFxYMEzywmF2fvZBuThuA2Mm7QF8wPd3E6R5pVqVEnC2VhcBb4VrfAnkZC73ZC5g1NRUnKZCB2e6CaRiUBDatR2nf505PeKp7Aj5XxvTdfSqdZCsXxQFYZApPNSUUgkUWm6HwL4rp21MRJXb612sZD'  
9 5
10 -def facebook_comments(hub, author_id, page_id, pooling_time, token, proxy_url)  
11 - pooling_time ||= 5  
12 - Koala.http_service.http_options = { :proxy => proxy_url } unless proxy_url.blank? 6 + def not_blank(v)
  7 + if v == nil || v == ""
  8 + false
  9 + else
  10 + true
  11 + end
  12 + end
  13 +
  14 +
  15 +def facebook_comments(hub, author_id, hashtag, pooling_time, token, proxy_url)
  16 +
  17 + puts "entrou"
  18 +
  19 + pooling_time ||= 10
  20 + token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
  21 + hashtag ||= "#nba"
  22 +
  23 +
  24 + #Aviso 12/04/2014
  25 + #token que só deverá expirar em 59 dias
  26 + #@graph = Koala::Facebook::API.new('CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH')
  27 + # https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH
13 28
14 - @graph = Koala::Facebook::API.new(token) 29 + #removes extra #
  30 + if hashtag[0]='#'
  31 + hashtag = hashtag[1,hashtag.length-1]
  32 + end
  33 +
  34 + extractedComments = []
15 initialComments = [] 35 initialComments = []
16 firstTime = true 36 firstTime = true
  37 + read = 1
  38 +
17 while true 39 while true
18 - feed = @graph.get_connections(page_id, "posts")  
19 - array = []  
20 - extractedComments = []  
21 - feed.each {|f|  
22 - if f['comments'] != nil && f['comments']['data'] != nil  
23 - array.push(f['comments']['data'])  
24 - end 40 + file = open("https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}")
  41 + itens = JSON.parse(file.read)['data']
  42 + mostRecent = ""
  43 + itens.each{|i|
  44 + from = ""
  45 + message = ""
  46 + if not_blank(i['from']['name'])
  47 + from = i['from']['name']
  48 + if not_blank(i['message'])
  49 + message += i['message']
  50 + end
  51 + if not_blank(message)
  52 + if mostRecent == "" or mostRecent < i["created_time"]
  53 + mostRecent = i["created_time"]
  54 + end
  55 +
  56 + extractedComments.push("#{from} said: #{message}")
  57 + # puts "#{from} said: #{message}"
  58 + end
  59 + end
25 } 60 }
26 - extractedComments = array.flatten.uniq 61 +
  62 + extractedComments = extractedComments.uniq
27 if firstTime 63 if firstTime
28 initialComments = extractedComments.clone 64 initialComments = extractedComments.clone
29 firstTime = false 65 firstTime = false
30 end 66 end
  67 +
  68 + # extractedComments.each{|comment|
  69 + # puts comment
  70 + # }
  71 +
  72 + # extractedComments.each{|comment|
  73 + # puts comment
  74 + # }
  75 +
31 newComments = extractedComments - initialComments 76 newComments = extractedComments - initialComments
32 newComments = newComments.uniq 77 newComments = newComments.uniq
33 initialComments += newComments 78 initialComments += newComments
34 initialComments = initialComments.uniq 79 initialComments = initialComments.uniq
35 newComments.each{|comment| 80 newComments.each{|comment|
  81 + puts comment
36 puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}" 82 puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}"
37 noosferoComment = Comment.new 83 noosferoComment = Comment.new
38 noosferoComment.title = 'hub-message-facebook' 84 noosferoComment.title = 'hub-message-facebook'
39 - noosferoComment.source = hub 85 + noosferoComment.source = Article.last
  86 + #noosferoComment.source = hub
40 noosferoComment.body = comment['message'] 87 noosferoComment.body = comment['message']
41 noosferoComment.author_id = author_id 88 noosferoComment.author_id = author_id
42 noosferoComment.name = comment['from']['name'] 89 noosferoComment.name = comment['from']['name']
43 noosferoComment.email = 'admin@localhost.local' 90 noosferoComment.email = 'admin@localhost.local'
44 noosferoComment.save! 91 noosferoComment.save!
45 } 92 }
  93 + puts "****************************************************************************************************************"
  94 + puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}"
  95 + read+=1
46 sleep(pooling_time) 96 sleep(pooling_time)
47 end 97 end
48 end 98 end
  99 +
  100 +
  101 +
  102 +facebook_comments(nil, 54, "nba", 5, nil, nil)
  103 +
  104 +
  105 +puts "ola"
49 \ No newline at end of file 106 \ No newline at end of file
lib/community_hub_plugin/hub.rb
@@ -10,7 +10,7 @@ class CommunityHubPlugin::Hub &lt; Folder @@ -10,7 +10,7 @@ class CommunityHubPlugin::Hub &lt; Folder
10 settings_items :twitter_access_token, :type => :string, :default => "" 10 settings_items :twitter_access_token, :type => :string, :default => ""
11 settings_items :twitter_access_token_secret, :type => :string, :default => "" 11 settings_items :twitter_access_token_secret, :type => :string, :default => ""
12 settings_items :facebook_enabled, :type => :boolean, :default => false 12 settings_items :facebook_enabled, :type => :boolean, :default => false
13 - settings_items :facebook_page_id, :type => :string, :default => "" 13 + settings_items :facebook_hashtag, :type => :string, :default => ""
14 settings_items :facebook_pooling_time, :type => :integer, :default => 5 # Time in seconds 14 settings_items :facebook_pooling_time, :type => :integer, :default => 5 # Time in seconds
15 settings_items :facebook_access_token, :type => :string, :default => '' 15 settings_items :facebook_access_token, :type => :string, :default => ''
16 settings_items :pinned_messages, :type => Array, :default => [] 16 settings_items :pinned_messages, :type => Array, :default => []
lib/community_hub_plugin/listener.rb
@@ -9,7 +9,7 @@ class CommunityHubPlugin::Listener @@ -9,7 +9,7 @@ class CommunityHubPlugin::Listener
9 end 9 end
10 10
11 def facebook_service(hub) 11 def facebook_service(hub)
12 - facebook_comments(hub, nil, hub.facebook_page_id, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url) 12 + facebook_comments(hub, nil, hub.facebook_hashtag, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url)
13 end 13 end
14 14
15 def run 15 def run
@@ -39,6 +39,7 @@ class CommunityHubPlugin::Listener @@ -39,6 +39,7 @@ class CommunityHubPlugin::Listener
39 end 39 end
40 40
41 def log(message) 41 def log(message)
  42 + #puts message
42 initialize_logger unless @initiated 43 initialize_logger unless @initiated
43 @initiated ||= true 44 @initiated ||= true
44 @logger << "[#{Time.now.strftime('%F %T %z')}] #{message}\n" 45 @logger << "[#{Time.now.strftime('%F %T %z')}] #{message}\n"
views/cms/community_hub_plugin/_hub.rhtml
@@ -5,10 +5,10 @@ @@ -5,10 +5,10 @@
5 <%= required labelled_form_field(_('Title'), text_field(:article, 'name', :size => '64', :maxlength => 150)) %> 5 <%= required labelled_form_field(_('Title'), text_field(:article, 'name', :size => '64', :maxlength => 150)) %>
6 </div> 6 </div>
7 <div> 7 <div>
8 - <%= render :file => 'shared/tiny_mce' %>  
9 - <%= required labelled_form_field(_('Description'), text_area(:article, 'body', :style => 'width: 100%;', :class => 'mceEditor')) %> 8 + <%= required labelled_form_field(_('Description'), text_area(:article, 'body', :style => 'width: 99%;')) %>
10 </div> 9 </div>
11 <br /> 10 <br />
  11 +
12 <div> 12 <div>
13 <%= _('General Streaming Settings:') %> 13 <%= _('General Streaming Settings:') %>
14 </div> 14 </div>
@@ -18,20 +18,19 @@ @@ -18,20 +18,19 @@
18 </div> 18 </div>
19 <br /> 19 <br />
20 <%= check_box(:article, :twitter_enabled) %> <span><%= _("Turn on TWITTER") %></span> 20 <%= check_box(:article, :twitter_enabled) %> <span><%= _("Turn on TWITTER") %></span>
21 - <br />  
22 - <br /> 21 + <br /><br />
23 <span><%= required labelled_form_field(_('Twitter\'s Hashtags, comma separated words<br>(example: participa.br,participabr,arenanetmundial,netmundial'), text_field(:article, :twitter_hashtags)) %></span> 22 <span><%= required labelled_form_field(_('Twitter\'s Hashtags, comma separated words<br>(example: participa.br,participabr,arenanetmundial,netmundial'), text_field(:article, :twitter_hashtags)) %></span>
24 <br /> 23 <br />
  24 + <div>
  25 + <%= _('Facebook Settings:') %>
  26 + </div>
25 <br /> 27 <br />
26 - <span><%= required labelled_form_field(_('Twitter\'s consumer key'), text_field(:article, :twitter_consumer_key)) %></span>  
27 - <br />  
28 - <br />  
29 - <span><%= required labelled_form_field(_('Twitter\'s consumer secret'), text_field(:article, :twitter_consumer_secret)) %></span>  
30 - <br />  
31 - <br />  
32 - <span><%= required labelled_form_field(_('Twitter\'s access token'), text_field(:article, :twitter_access_token)) %></span>  
33 - <br /> 28 + <%= check_box(:article, :facebook_enabled) %> <span><%= _("Turn on FACEBOOK") %></span>
  29 + <br /><br />
  30 + <span><%= required labelled_form_field(_('Facebook\'s hashtag (example: #participabr)'), text_field(:article, :facebook_hashtag)) %></span>
34 <br /> 31 <br />
35 - <span><%= required labelled_form_field(_('Twitter\'s access token secret'), text_field(:article, :twitter_access_token_secret)) %></span> 32 + <span><%= required labelled_form_field(_('Facebook\'s access token'), text_field(:article, :facebook_access_token)) %></span>
  33 + <br />
  34 + <a href='https://smashballoon.com/custom-facebook-feed/access-token/' ><%= _('How to get a new access token?') %><a>
36 <br /> 35 <br />
37 -</div> 36 -</div>
  37 +</div>
38 \ No newline at end of file 38 \ No newline at end of file