Commit 20f1efe51d3a349a23700d8e2cf250003b5392ff

Authored by Evandro Junior
1 parent 988f4a83

new facebook library

plugins/community_hub/facebook_stream/codfish_facebook_api.rb 0 → 100644
... ... @@ -0,0 +1,76 @@
  1 +require 'rubygems'
  2 +require 'open-uri'
  3 +require 'json'
  4 +
  5 +token = 'CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH'
  6 +hashtag = "#nba"
  7 +pooling_time = 5
  8 +
  9 +
  10 +#Aviso 12/04/2014
  11 +#token que só deverá expirar em 59 dias
  12 +#@graph = Koala::Facebook::API.new('CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH')
  13 +# https://graph.facebook.com/v1.0/search?q=%23nba&type=post&access_token=CAAD8cd4tMVkBAO3sh2DrzwZCDfeQq9ZAvTz7Jz24ZC26KtMfBoljqaXhD2vBV1zpP0bjrpxXUBzJvKKcFzOm6rMG9Sok7iNVUaxt5iwr7dfMqCvHpMboKpqrqgeLrfCH5ITVTAdezA6ZBSr9iOJrqyCSOYfui0zTmbXJ3FqtshwNRrRy4NPH
  14 +
  15 +def not_blank(v)
  16 + if v == nil || v == ""
  17 + false
  18 + else
  19 + true
  20 + end
  21 +end
  22 +
  23 +if hashtag[0]='#'
  24 + hashtag = hashtag[1,hashtag.length-1]
  25 +end
  26 +
  27 +file = open("https://graph.facebook.com/v1.0/search?q=%23#{hashtag}&type=post&access_token=#{token}")
  28 +itens = JSON.parse(file.read)['data']
  29 +
  30 +extractedComments = []
  31 +initialComments = []
  32 +firstTime = true
  33 +while true
  34 + mostRecent = ""
  35 + itens.each{|i|
  36 + from = ""
  37 + message = ""
  38 + if not_blank(i['from']['name'])
  39 + from = i['from']['name']
  40 + if not_blank(i['message'])
  41 + message += i['message']
  42 + end
  43 + if not_blank(message)
  44 + if mostRecent == "" or mostRecent > i["updated_time"]
  45 + mostRecent = i["updated_time"]
  46 + end
  47 +
  48 + extractedComments.push("#{from} said: #{message}")
  49 + # puts "#{from} said: #{message}"
  50 + end
  51 + end
  52 + }
  53 +
  54 + extractedComments = extractedComments.uniq
  55 + if firstTime
  56 + initialComments = extractedComments.clone
  57 + firstTime = false
  58 + end
  59 +
  60 +# extractedComments.each{|comment|
  61 +# puts comment
  62 +# }
  63 +
  64 + newComments = extractedComments - initialComments
  65 + newComments = newComments.uniq
  66 + initialComments += newComments
  67 + initialComments = initialComments.uniq
  68 + newComments.each{|comment|
  69 + puts comment
  70 + }
  71 + puts "most recent post at #{mostRecent}"
  72 + sleep(pooling_time)
  73 +end
  74 +
  75 +
  76 +
... ...