Commit 74c3d7030324a52222d49b11cc968e886f22a094
1 parent
ecbb4963
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Fix for facebook api
Showing
2 changed files
with
69 additions
and
50 deletions
Show diff stats
plugins/community_hub/facebook_stream/codfish_facebook_api.rb
... | ... | @@ -3,7 +3,7 @@ require 'open-uri' |
3 | 3 | require 'json' |
4 | 4 | |
5 | 5 | token = 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' |
6 | -hashtag = "#nba" | |
6 | +hashtag = "love" | |
7 | 7 | pooling_time = 10 |
8 | 8 | |
9 | 9 | |
... | ... | @@ -20,9 +20,9 @@ def not_blank(v) |
20 | 20 | end |
21 | 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 | 27 | extractedComments = [] |
28 | 28 | initialComments = [] | ... | ... |
plugins/community_hub/facebook_stream/lib_facebook_stream.rb
... | ... | @@ -2,8 +2,7 @@ require 'rubygems' |
2 | 2 | require 'open-uri' |
3 | 3 | require 'json' |
4 | 4 | |
5 | - | |
6 | -def not_blank(v) | |
5 | +def not_blank?(v) | |
7 | 6 | if v == nil || v == "" |
8 | 7 | false |
9 | 8 | else |
... | ... | @@ -11,88 +10,108 @@ def not_blank(v) |
11 | 10 | end |
12 | 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 | 25 | def facebook_comments(hub, author_id, hashtag, pooling_time, token) |
16 | - | |
17 | - | |
18 | - puts "entrou" | |
19 | - | |
26 | + | |
20 | 27 | pooling_time ||= 10 |
21 | 28 | token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH' |
22 | 29 | hashtag ||= "#nba" |
23 | - | |
24 | - | |
25 | 30 | #Aviso 12/04/2014 |
26 | 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 | 37 | hashtag = hashtag[1,hashtag.length-1] |
33 | 38 | end |
34 | 39 | |
35 | - extractedComments = [] | |
36 | 40 | initialComments = [] |
37 | 41 | firstTime = true |
38 | 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 | 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 | 63 | itens = JSON.parse(file.read)['data'] |
43 | - mostRecent = "" | |
44 | 64 | itens.each{|i| |
45 | 65 | from = "" |
46 | 66 | message = "" |
47 | - if not_blank(i['from']['name']) | |
67 | + if not_blank?(i['from']['name']) | |
48 | 68 | from = i['from']['name'] |
49 | - if not_blank(i['message']) | |
69 | + if not_blank?(i['message']) | |
50 | 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 | 79 | end |
52 | - if not_blank(message) | |
80 | + if not_blank?(message) | |
53 | 81 | if mostRecent == "" or mostRecent < i["created_time"] |
54 | 82 | mostRecent = i["created_time"] |
55 | 83 | end |
56 | - | |
57 | - extractedComments.push("#{from} said: #{message}") | |
58 | - # puts "#{from} said: #{message}" | |
84 | + extractedComments.push({:from=>from, :message=>message}) | |
59 | 85 | end |
60 | 86 | end |
61 | 87 | } |
62 | - | |
88 | + | |
63 | 89 | extractedComments = extractedComments.uniq |
64 | 90 | if firstTime |
65 | 91 | initialComments = extractedComments.clone |
66 | 92 | firstTime = false |
93 | + extractedComments.each{|comment| | |
94 | + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}" | |
95 | + save_comment(hub, comment, author_id) | |
96 | + } | |
67 | 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 | 103 | newComments = extractedComments - initialComments |
78 | 104 | newComments = newComments.uniq |
79 | 105 | initialComments += newComments |
80 | 106 | initialComments = initialComments.uniq |
107 | + #y newComments | |
81 | 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 | 114 | read+=1 |
96 | 115 | sleep(pooling_time) |
97 | 116 | end |
98 | 117 | -end |
118 | +end | |
99 | 119 | \ No newline at end of file | ... | ... |