From ddce1cba46afe4db0d693dbcfa19437d39bfea6f Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Thu, 22 May 2014 21:30:57 +0000 Subject: [PATCH] const missing to be analised --- plugins/community_hub/facebook_stream/lib_facebook_koala_stream.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ plugins/community_hub/facebook_stream/lib_facebook_stream.rb | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ plugins/community_hub/lib/community_hub_plugin/hub.rb | 2 +- plugins/community_hub/lib/community_hub_plugin/listener.rb | 3 ++- plugins/community_hub/views/cms/community_hub_plugin/_hub.rhtml | 27 +++++++++++++-------------- 5 files changed, 139 insertions(+), 34 deletions(-) create mode 100644 plugins/community_hub/facebook_stream/lib_facebook_koala_stream.rb diff --git a/plugins/community_hub/facebook_stream/lib_facebook_koala_stream.rb b/plugins/community_hub/facebook_stream/lib_facebook_koala_stream.rb new file mode 100644 index 0000000..a7e394f --- /dev/null +++ b/plugins/community_hub/facebook_stream/lib_facebook_koala_stream.rb @@ -0,0 +1,48 @@ +require 'rubygems' +require 'koala' +require 'json' + +#Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! +#token will expire at 12/04/2014 (Brazilian date format) + 59 days +#'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' +# BACKUP TOKEN 'CAAEhsewl0ZAcBAHhipXszZCURSwWLmgvceDbs9mB5baJdLriFxYMEzywmF2fvZBuThuA2Mm7QF8wPd3E6R5pVqVEnC2VhcBb4VrfAnkZC73ZC5g1NRUnKZCB2e6CaRiUBDatR2nf505PeKp7Aj5XxvTdfSqdZCsXxQFYZApPNSUUgkUWm6HwL4rp21MRJXb612sZD' + +def facebook_comments(hub, author_id, page_id, pooling_time, token, proxy_url) + pooling_time ||= 5 + Koala.http_service.http_options = { :proxy => proxy_url } unless proxy_url.blank? + + @graph = Koala::Facebook::API.new(token) + initialComments = [] + firstTime = true + while true + feed = @graph.get_connections(page_id, "posts") + array = [] + extractedComments = [] + feed.each {|f| + if f['comments'] != nil && f['comments']['data'] != nil + array.push(f['comments']['data']) + end + } + extractedComments = array.flatten.uniq + if firstTime + initialComments = extractedComments.clone + firstTime = false + end + newComments = extractedComments - initialComments + newComments = newComments.uniq + initialComments += newComments + initialComments = initialComments.uniq + newComments.each{|comment| + puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}" + noosferoComment = Comment.new + noosferoComment.title = 'hub-message-facebook' + noosferoComment.source = hub + noosferoComment.body = comment['message'] + noosferoComment.author_id = author_id + noosferoComment.name = comment['from']['name'] + noosferoComment.email = 'admin@localhost.local' + noosferoComment.save! + } + sleep(pooling_time) + end +end diff --git a/plugins/community_hub/facebook_stream/lib_facebook_stream.rb b/plugins/community_hub/facebook_stream/lib_facebook_stream.rb index a7e394f..b85f459 100644 --- a/plugins/community_hub/facebook_stream/lib_facebook_stream.rb +++ b/plugins/community_hub/facebook_stream/lib_facebook_stream.rb @@ -1,48 +1,105 @@ require 'rubygems' -require 'koala' +require 'open-uri' require 'json' -#Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! Warning!!! -#token will expire at 12/04/2014 (Brazilian date format) + 59 days -#'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' -# BACKUP TOKEN 'CAAEhsewl0ZAcBAHhipXszZCURSwWLmgvceDbs9mB5baJdLriFxYMEzywmF2fvZBuThuA2Mm7QF8wPd3E6R5pVqVEnC2VhcBb4VrfAnkZC73ZC5g1NRUnKZCB2e6CaRiUBDatR2nf505PeKp7Aj5XxvTdfSqdZCsXxQFYZApPNSUUgkUWm6HwL4rp21MRJXb612sZD' -def facebook_comments(hub, author_id, page_id, pooling_time, token, proxy_url) - pooling_time ||= 5 - Koala.http_service.http_options = { :proxy => proxy_url } unless proxy_url.blank? + def not_blank(v) + if v == nil || v == "" + false + else + true + end + end + + +def facebook_comments(hub, author_id, hashtag, pooling_time, token, proxy_url) + + puts "entrou" + + pooling_time ||= 10 + token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' + hashtag ||= "#nba" + + + #Aviso 12/04/2014 + #token que só deverá expirar em 59 dias + #@graph = Koala::Facebook::API.new('CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH') + # https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH - @graph = Koala::Facebook::API.new(token) + #removes extra # + if hashtag[0]='#' + hashtag = hashtag[1,hashtag.length-1] + end + + extractedComments = [] initialComments = [] firstTime = true + read = 1 + while true - feed = @graph.get_connections(page_id, "posts") - array = [] - extractedComments = [] - feed.each {|f| - if f['comments'] != nil && f['comments']['data'] != nil - array.push(f['comments']['data']) - end + file = open("https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}") + itens = JSON.parse(file.read)['data'] + mostRecent = "" + itens.each{|i| + from = "" + message = "" + if not_blank(i['from']['name']) + from = i['from']['name'] + if not_blank(i['message']) + message += i['message'] + end + if not_blank(message) + if mostRecent == "" or mostRecent < i["created_time"] + mostRecent = i["created_time"] + end + + extractedComments.push("#{from} said: #{message}") + # puts "#{from} said: #{message}" + end + end } - extractedComments = array.flatten.uniq + + extractedComments = extractedComments.uniq if firstTime initialComments = extractedComments.clone firstTime = false end + + # extractedComments.each{|comment| + # puts comment + # } + + # extractedComments.each{|comment| + # puts comment + # } + newComments = extractedComments - initialComments newComments = newComments.uniq initialComments += newComments initialComments = initialComments.uniq newComments.each{|comment| + puts comment puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}" noosferoComment = Comment.new noosferoComment.title = 'hub-message-facebook' - noosferoComment.source = hub + noosferoComment.source = Article.last + #noosferoComment.source = hub noosferoComment.body = comment['message'] noosferoComment.author_id = author_id noosferoComment.name = comment['from']['name'] noosferoComment.email = 'admin@localhost.local' noosferoComment.save! } + puts "****************************************************************************************************************" + puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}" + read+=1 sleep(pooling_time) end end + + + +facebook_comments(nil, 54, "nba", 5, nil, nil) + + +puts "ola" \ No newline at end of file diff --git a/plugins/community_hub/lib/community_hub_plugin/hub.rb b/plugins/community_hub/lib/community_hub_plugin/hub.rb index 5aa0069..5712698 100644 --- a/plugins/community_hub/lib/community_hub_plugin/hub.rb +++ b/plugins/community_hub/lib/community_hub_plugin/hub.rb @@ -10,7 +10,7 @@ class CommunityHubPlugin::Hub < Folder settings_items :twitter_access_token, :type => :string, :default => "" settings_items :twitter_access_token_secret, :type => :string, :default => "" settings_items :facebook_enabled, :type => :boolean, :default => false - settings_items :facebook_page_id, :type => :string, :default => "" + settings_items :facebook_hashtag, :type => :string, :default => "" settings_items :facebook_pooling_time, :type => :integer, :default => 5 # Time in seconds settings_items :facebook_access_token, :type => :string, :default => '' settings_items :pinned_messages, :type => Array, :default => [] diff --git a/plugins/community_hub/lib/community_hub_plugin/listener.rb b/plugins/community_hub/lib/community_hub_plugin/listener.rb index 737be86..a99555d 100644 --- a/plugins/community_hub/lib/community_hub_plugin/listener.rb +++ b/plugins/community_hub/lib/community_hub_plugin/listener.rb @@ -9,7 +9,7 @@ class CommunityHubPlugin::Listener end def facebook_service(hub) - facebook_comments(hub, nil, hub.facebook_page_id, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url) + facebook_comments(hub, nil, hub.facebook_hashtag, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url) end def run @@ -39,6 +39,7 @@ class CommunityHubPlugin::Listener end def log(message) + #puts message initialize_logger unless @initiated @initiated ||= true @logger << "[#{Time.now.strftime('%F %T %z')}] #{message}\n" diff --git a/plugins/community_hub/views/cms/community_hub_plugin/_hub.rhtml b/plugins/community_hub/views/cms/community_hub_plugin/_hub.rhtml index 50b66c2..2a97928 100644 --- a/plugins/community_hub/views/cms/community_hub_plugin/_hub.rhtml +++ b/plugins/community_hub/views/cms/community_hub_plugin/_hub.rhtml @@ -5,10 +5,10 @@ <%= required labelled_form_field(_('Title'), text_field(:article, 'name', :size => '64', :maxlength => 150)) %>
- <%= render :file => 'shared/tiny_mce' %> - <%= required labelled_form_field(_('Description'), text_area(:article, 'body', :style => 'width: 100%;', :class => 'mceEditor')) %> + <%= required labelled_form_field(_('Description'), text_area(:article, 'body', :style => 'width: 99%;')) %>

+
<%= _('General Streaming Settings:') %>
@@ -18,20 +18,19 @@
<%= check_box(:article, :twitter_enabled) %> <%= _("Turn on TWITTER") %> -
-
+

<%= required labelled_form_field(_('Twitter\'s Hashtags, comma separated words
(example: participa.br,participabr,arenanetmundial,netmundial'), text_field(:article, :twitter_hashtags)) %>

+
+ <%= _('Facebook Settings:') %> +

- <%= required labelled_form_field(_('Twitter\'s consumer key'), text_field(:article, :twitter_consumer_key)) %> -
-
- <%= required labelled_form_field(_('Twitter\'s consumer secret'), text_field(:article, :twitter_consumer_secret)) %> -
-
- <%= required labelled_form_field(_('Twitter\'s access token'), text_field(:article, :twitter_access_token)) %> -
+ <%= check_box(:article, :facebook_enabled) %> <%= _("Turn on FACEBOOK") %> +

+ <%= required labelled_form_field(_('Facebook\'s hashtag (example: #participabr)'), text_field(:article, :facebook_hashtag)) %>
- <%= required labelled_form_field(_('Twitter\'s access token secret'), text_field(:article, :twitter_access_token_secret)) %> + <%= required labelled_form_field(_('Facebook\'s access token'), text_field(:article, :facebook_access_token)) %> +
+ <%= _('How to get a new access token?') %>
- + \ No newline at end of file -- libgit2 0.21.2