diff --git a/plugins/community_hub/facebook_stream/codfish_facebook_api.rb b/plugins/community_hub/facebook_stream/codfish_facebook_api.rb index a7ebc7b..ab18a08 100644 --- a/plugins/community_hub/facebook_stream/codfish_facebook_api.rb +++ b/plugins/community_hub/facebook_stream/codfish_facebook_api.rb @@ -3,7 +3,7 @@ require 'open-uri' require 'json' token = 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' -hashtag = "#nba" +hashtag = "love" pooling_time = 10 @@ -20,9 +20,9 @@ def not_blank(v) end end -if hashtag[0]='#' - hashtag = hashtag[1,hashtag.length-1] -end +#if hashtag[0]='#' +# hashtag = hashtag[1,hashtag.length-1] +#end extractedComments = [] initialComments = [] diff --git a/plugins/community_hub/facebook_stream/lib_facebook_stream.rb b/plugins/community_hub/facebook_stream/lib_facebook_stream.rb index 307967c..e00da13 100644 --- a/plugins/community_hub/facebook_stream/lib_facebook_stream.rb +++ b/plugins/community_hub/facebook_stream/lib_facebook_stream.rb @@ -2,8 +2,7 @@ require 'rubygems' require 'open-uri' require 'json' - -def not_blank(v) +def not_blank?(v) if v == nil || v == "" false else @@ -11,88 +10,108 @@ def not_blank(v) end end +def save_comment(hub, comment, author_id) + noosferoComment = Comment.new + noosferoComment.title = 'hub-message-facebook' + noosferoComment.source = hub + noosferoComment.body = comment[:message] + noosferoComment.author_id = author_id + noosferoComment.name = comment[:from] + noosferoComment.email = 'admin@localhost.local' + noosferoComment.save! +end + def facebook_comments(hub, author_id, hashtag, pooling_time, token) - - - 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 - - #removes extra # - if hashtag[0]='#' + #https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH + # + + #removes extra '#' + if hashtag[0,1]=='#' hashtag = hashtag[1,hashtag.length-1] end - extractedComments = [] initialComments = [] firstTime = true read = 1 - + url = "https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}" + mostRecent = "" + while true - file = open("https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}") + connected = false + tries = 0 + while !connected + begin + tries += 1 + file = open(url) + connected = true + tries = 0 + rescue => e + puts "Error connecting to facebook: #{e.inspect} " + puts file + sleep (10 + 2 ** tries) + end + end + + extractedComments = [] itens = JSON.parse(file.read)['data'] - mostRecent = "" itens.each{|i| from = "" message = "" - if not_blank(i['from']['name']) + if not_blank?(i['from']['name']) from = i['from']['name'] - if not_blank(i['message']) + if not_blank?(i['message']) message += i['message'] + else + if not_blank?(i['description']) + message += i['description'] + else + if not_blank?(i['caption']) + message += i['caption'] + end + end end - if not_blank(message) + 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}" + extractedComments.push({:from=>from, :message=>message}) end end } - + extractedComments = extractedComments.uniq if firstTime initialComments = extractedComments.clone firstTime = false + extractedComments.each{|comment| + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}" + save_comment(hub, comment, author_id) + } end - - # extractedComments.each{|comment| - # puts comment - # } - - # extractedComments.each{|comment| - # puts comment - # } - + +# if read == 2 +# extractedComments.push({:from=>"Evandro", :message=>"teste"}) +# end + newComments = extractedComments - initialComments newComments = newComments.uniq initialComments += newComments initialComments = initialComments.uniq + #y newComments newComments.each{|comment| - puts 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! + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}" + save_comment(hub, comment, author_id) } - puts "****************************************************************************************************************" - puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}" +# puts url +# puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}" read+=1 sleep(pooling_time) end -end +end \ No newline at end of file -- libgit2 0.21.2