Commit f498cdb456575387d963a23bcdcb3147be9c0229

Authored by Francisco Marcelo de Araújo Lima Júnior
2 parents 714ead6a 725bf103

Merge branch 'AI3074-community_dashboard' into rails3_community_dashboard

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,104 +2,116 @@ require 'rubygems'
2 2 require 'open-uri'
3 3 require 'json'
4 4  
5   -
6   - def not_blank(v)
7   - if v == nil || v == ""
8   - false
9   - else
10   - true
11   - end
  5 +def not_blank?(v)
  6 + if v == nil || v == ""
  7 + false
  8 + else
  9 + true
12 10 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 +
  24 +
  25 +def facebook_comments(hub, author_id, hashtag, pooling_time, token)
14 26  
15   -def facebook_comments(hub, author_id, hashtag, pooling_time, token, proxy_url)
16   -
17   - puts "entrou"
18   -
19 27 pooling_time ||= 10
20 28 token ||= 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
21 29 hashtag ||= "#nba"
22   -
23   -
24 30 #Aviso 12/04/2014
25 31 #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
28   -
29   - #removes extra #
30   - 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]=='#'
31 37 hashtag = hashtag[1,hashtag.length-1]
32 38 end
33 39  
34   - extractedComments = []
35 40 initialComments = []
36 41 firstTime = true
37 42 read = 1
38   -
  43 + url = "https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}"
  44 + mostRecent = ""
  45 +
39 46 while true
40   - 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 = []
41 63 itens = JSON.parse(file.read)['data']
42   - mostRecent = ""
43 64 itens.each{|i|
44 65 from = ""
45 66 message = ""
46   - if not_blank(i['from']['name'])
  67 + if not_blank?(i['from']['name'])
47 68 from = i['from']['name']
48   - if not_blank(i['message'])
  69 + if not_blank?(i['message'])
49 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
50 79 end
51   - if not_blank(message)
  80 + if not_blank?(message)
52 81 if mostRecent == "" or mostRecent < i["created_time"]
53 82 mostRecent = i["created_time"]
54 83 end
55   -
56   - extractedComments.push("#{from} said: #{message}")
57   - # puts "#{from} said: #{message}"
  84 + extractedComments.push({:from=>from, :message=>message})
58 85 end
59 86 end
60 87 }
61   -
  88 +
62 89 extractedComments = extractedComments.uniq
63 90 if firstTime
64 91 initialComments = extractedComments.clone
65 92 firstTime = false
  93 + extractedComments.each{|comment|
  94 + puts "#{comment[:from]} " + _("said") + ": #{comment[:message]}"
  95 + save_comment(hub, comment, author_id)
  96 + }
66 97 end
67   -
68   - # extractedComments.each{|comment|
69   - # puts comment
70   - # }
71   -
72   - # extractedComments.each{|comment|
73   - # puts comment
74   - # }
75   -
  98 +
  99 +# if read == 2
  100 +# extractedComments.push({:from=>"Evandro", :message=>"teste"})
  101 +# end
  102 +
76 103 newComments = extractedComments - initialComments
77 104 newComments = newComments.uniq
78 105 initialComments += newComments
79 106 initialComments = initialComments.uniq
  107 + #y newComments
80 108 newComments.each{|comment|
81   - puts comment
82   - puts "#{comment['from']['name']} " + _("said") + ": #{comment['message']}"
83   - noosferoComment = Comment.new
84   - noosferoComment.title = 'hub-message-facebook'
85   - noosferoComment.source = Article.last
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   -end
99   -
100   -
101   -
102   -facebook_comments(nil, 54, "nba", 5, nil, nil)
103   -
104   -
105   -puts "ola"
106 117 \ No newline at end of file
  118 +end
107 119 \ No newline at end of file
... ...
plugins/community_hub/lib/community_hub_plugin/listener.rb
... ... @@ -9,7 +9,7 @@ class CommunityHubPlugin::Listener
9 9 end
10 10  
11 11 def facebook_service(hub)
12   - facebook_comments(hub, nil, hub.facebook_hashtag, 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)
13 13 end
14 14  
15 15 def run
... ...