Commit 1b537eb03f6d88ecd3cab007c512760b5bbdb22d

Authored by Evandro Junior
1 parent b3cdaa1b

Fix for facebook api

facebook_stream/codfish_facebook_api.rb
@@ -3,7 +3,7 @@ require 'open-uri' @@ -3,7 +3,7 @@ require 'open-uri'
3 require 'json' 3 require 'json'
4 4
5 token = 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' 5 token = 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
6 -hashtag = "#nba" 6 +hashtag = "love"
7 pooling_time = 10 7 pooling_time = 10
8 8
9 9
@@ -20,9 +20,9 @@ def not_blank(v) @@ -20,9 +20,9 @@ def not_blank(v)
20 end 20 end
21 end 21 end
22 22
23 -if hashtag[0]='#'  
24 - hashtag = hashtag[1,hashtag.length-1]  
25 -end 23 +#if hashtag[0]='#'
  24 +# hashtag = hashtag[1,hashtag.length-1]
  25 +#end
26 26
27 extractedComments = [] 27 extractedComments = []
28 initialComments = [] 28 initialComments = []
facebook_stream/lib_facebook_stream.rb
@@ -2,8 +2,7 @@ require 'rubygems' @@ -2,8 +2,7 @@ require 'rubygems'
2 require 'open-uri' 2 require 'open-uri'
3 require 'json' 3 require 'json'
4 4
5 -  
6 -def not_blank(v) 5 +def not_blank?(v)
7 if v == nil || v == "" 6 if v == nil || v == ""
8 false 7 false
9 else 8 else
@@ -11,88 +10,108 @@ def not_blank(v) @@ -11,88 +10,108 @@ def not_blank(v)
11 end 10 end
12 end 11 end
13 12
  13 +def save_comment(hub, comment, author_id)
  14 + noosferoComment = Comment.new
  15 + noosferoComment.title = 'hub-message-facebook'
  16 + noosferoComment.source = hub
  17 + noosferoComment.body = comment[:message]
  18 + noosferoComment.author_id = author_id
  19 + noosferoComment.name = comment[:from]
  20 + noosferoComment.email = 'admin@localhost.local'
  21 + noosferoComment.save!
  22 +end
  23 +
14 24
15 def facebook_comments(hub, author_id, hashtag, pooling_time, token) 25 def facebook_comments(hub, author_id, hashtag, pooling_time, token)
16 -  
17 -  
18 - puts "entrou"  
19 - 26 +
20 pooling_time ||= 10 27 pooling_time ||= 10
21 token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' 28 token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
22 hashtag ||= "#nba" 29 hashtag ||= "#nba"
23 -  
24 -  
25 #Aviso 12/04/2014 30 #Aviso 12/04/2014
26 #token que só deverá expirar em 59 dias 31 #token que só deverá expirar em 59 dias
27 - #@graph = Koala::Facebook::API.new('CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH')  
28 - # https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH  
29 -  
30 - #removes extra #  
31 - if hashtag[0]='#' 32 + #https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH
  33 + #
  34 +
  35 + #removes extra '#'
  36 + if hashtag[0,1]=='#'
32 hashtag = hashtag[1,hashtag.length-1] 37 hashtag = hashtag[1,hashtag.length-1]
33 end 38 end
34 39
35 - extractedComments = []  
36 initialComments = [] 40 initialComments = []
37 firstTime = true 41 firstTime = true
38 read = 1 42 read = 1
39 - 43 + url = "https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}"
  44 + mostRecent = ""
  45 +
40 while true 46 while true
41 - file = open("https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}") 47 + connected = false
  48 + tries = 0
  49 + while !connected
  50 + begin
  51 + tries += 1
  52 + file = open(url)
  53 + connected = true
  54 + tries = 0
  55 + rescue => e
  56 + puts "Error connecting to facebook: #{e.inspect} "
  57 + puts file
  58 + sleep (10 + 2 ** tries)
  59 + end
  60 + end
  61 +
  62 + extractedComments = []
42 itens = JSON.parse(file.read)['data'] 63 itens = JSON.parse(file.read)['data']
43 - mostRecent = ""  
44 itens.each{|i| 64 itens.each{|i|
45 from = "" 65 from = ""
46 message = "" 66 message = ""
47 - if not_blank(i['from']['name']) 67 + if not_blank?(i['from']['name'])
48 from = i['from']['name'] 68 from = i['from']['name']
49 - if not_blank(i['message']) 69 + if not_blank?(i['message'])
50 message += i['message'] 70 message += i['message']
  71 + else
  72 + if not_blank?(i['description'])
  73 + message += i['description']
  74 + else
  75 + if not_blank?(i['caption'])
  76 + message += i['caption']
  77 + end
  78 + end
51 end 79 end
52 - if not_blank(message) 80 + if not_blank?(message)
53 if mostRecent == "" or mostRecent < i["created_time"] 81 if mostRecent == "" or mostRecent < i["created_time"]
54 mostRecent = i["created_time"] 82 mostRecent = i["created_time"]
55 end 83 end
56 -  
57 - extractedComments.push("#{from} said: #{message}")  
58 - # puts "#{from} said: #{message}" 84 + extractedComments.push({:from=>from, :message=>message})
59 end 85 end
60 end 86 end
61 } 87 }
62 - 88 +
63 extractedComments = extractedComments.uniq 89 extractedComments = extractedComments.uniq
64 if firstTime 90 if firstTime
65 initialComments = extractedComments.clone 91 initialComments = extractedComments.clone
66 firstTime = false 92 firstTime = false
  93 + extractedComments.each{|comment|
  94 + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}"
  95 + save_comment(hub, comment, author_id)
  96 + }
67 end 97 end
68 -  
69 - # extractedComments.each{|comment|  
70 - # puts comment  
71 - # }  
72 -  
73 - # extractedComments.each{|comment|  
74 - # puts comment  
75 - # }  
76 - 98 +
  99 +# if read == 2
  100 +# extractedComments.push({:from=>"Evandro", :message=>"teste"})
  101 +# end
  102 +
77 newComments = extractedComments - initialComments 103 newComments = extractedComments - initialComments
78 newComments = newComments.uniq 104 newComments = newComments.uniq
79 initialComments += newComments 105 initialComments += newComments
80 initialComments = initialComments.uniq 106 initialComments = initialComments.uniq
  107 + #y newComments
81 newComments.each{|comment| 108 newComments.each{|comment|
82 - puts comment  
83 - puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}"  
84 - noosferoComment = Comment.new  
85 - noosferoComment.title = 'hub-message-facebook'  
86 - noosferoComment.source = hub  
87 - noosferoComment.body = comment['message']  
88 - noosferoComment.author_id = author_id  
89 - noosferoComment.name = comment['from']['name']  
90 - noosferoComment.email = 'admin@localhost.local'  
91 - noosferoComment.save! 109 + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}"
  110 + save_comment(hub, comment, author_id)
92 } 111 }
93 - puts "****************************************************************************************************************"  
94 - puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}" 112 +# puts url
  113 +# puts "Read: #{read} last post #{mostRecent} newComments: #{newComments.length} initialComments: #{initialComments.length} extractedComments: #{extractedComments.length}"
95 read+=1 114 read+=1
96 sleep(pooling_time) 115 sleep(pooling_time)
97 end 116 end
98 -end 117 -end
  118 +end
99 \ No newline at end of file 119 \ No newline at end of file