Mercurial > pumpbridge > pumpbridge
view src/google.coffee @ 10:ccf1d9e741bd
corrected google apikey query, removed facebook query language
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Wed, 11 Jun 2014 23:43:28 +0200 |
parents | 2123f355ab68 |
children | a52b5b244e51 |
line wrap: on
line source
# Copyright (C) 2014 by Intevation GmbH # Author: Mathias Gebbe <mgebbe@intevation.de> # # This file is Free Software under the Apache License, Version 2.0; # and comes with NO WARRANTY! # See the documentation coming with pumpbridge for details. https = require("https") async = require("async") _ = require("underscore") Sync = require("./sync") Routes = require("./routes") querystring = require("querystring") EdgeControl = require("./edgecontrol") Edge = require("./edge") Pump = require("./pumpio") FromESN = require("./fromESN") Config = require("./config") querystring = require('querystring') config = Config.config bridgeid = config.bridgeid interval = config.interval CLIENTID = config.gpCLIENTID CLIENTSECRET = config.gpCLIENTSECRET ############### # google sync # ############### sync = (user) -> return if not (user?) me = user.user_pumpio id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) if not (interval?) interval = 15 * 60 * 1000 # 900 000 ms (15min) async.waterfall [ (callback) -> updateToken(user,callback) (updateuser,callback) -> user = updateuser getUser(user) getFriends(user,callback) ], (err, friends) -> # für jeden Freunde tue dies: hier bewusst von async.eachSeries friends, ((friend, callback) -> auf async.each gewechselt async.each friends, ((friend, callback) -> async.waterfall [ (callback) -> getStream(user, friend, callback) (stream, callback) -> _.each stream.items, (post) -> async.waterfall [ (callback) -> FromESN.search {uid: post.id + "@google_to_" + me, recipientUser: me}, callback (result, callback) -> return if result.length isnt 0 ti = new Date().getTime() - interval ts = Date.parse(post.published) if (ts >= ti) Sync.postParser post, null, 'google', callback else return (parsed, callback) -> Pump.postUser bridgeid, me, parsed, callback (pumppost, callback) -> pumppost = JSON.parse(pumppost) FromESN.create postid: post.id + "@google" sourceUser: post.actor.id sourcePost: post.url pumpPost: pumppost.object.id recipientUser: me created: Date.now() , callback ], (err, result) -> #console.log err #console.log result return callback null, null ], (err, result) -> #console.log 'done.' callback null, 'done' ), (err) -> if err console.log 'one post fail to process' else console.log 'all google friends processed' return return ####################################### ###### get user info googleplus ###### ####################################### getUser = (user) -> data = "" id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) options = host: "www.googleapis.com" port: 443 path: "/plus/v1/people/" + id + '?access_token=' + token https.get(options, (res) -> #console.log "Got response: " + res.statusCode res.on "data", (chunk) -> data += chunk return res.on "end", () -> user = JSON.parse(data) Routes.updateUserDB(user.id+'@google',user.displayName,user.displayName,user.url,user.image.url) if user.id? ).on "error", (e) -> console.log "Got error: " + e.message return return ####################################### ###### get google friends ###### ####################################### getFriends = (user, callback) -> return if not user? data = "" friends = new Array() me = user.user_pumpio id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) if user.oauth_token.indexOf(';') isnt -1 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) else token = user.oauth_token options = host: "www.googleapis.com" port: 443 path: "/plus/v1/people/me/people/visible?key=" + CLIENTID + "&" headers: "Authorization": "Bearer " + token https.get(options, (res) -> #console.log "Got response: " + res.statusCode res.on "data", (chunk) -> data +=chunk res.on "end", () -> async.waterfall [ (callback) -> users = JSON.parse(decodeURI(data)) return if (users.error?) callback null,users (users, callback) -> EdgeControl.removeEdges(me,'@google') _.each users.items, (user) -> if user.id? friends.push(user.id) Routes.updateUserDB(user.id+'@google',user.displayName,user.displayName,user.url,user.image.url) EdgeControl.addEdge(me,user.id+'@google') callback null, friends ], (err, friends) -> callback null, friends return ).on "error", (e) -> console.log "Got error: " + e.message return ####################################### ###### get google stream ###### ####################################### getStream = (user, targetid, callback) -> data = "" max_results = 3 me = user.user_pumpio id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) if user.oauth_token.indexOf(';') isnt -1 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) else token = user.oauth_token options = host: "www.googleapis.com" port: 443 path: "/plus/v1/people/" + targetid + "/activities/public?maxResults="+max_results+"&key=" + CLIENTID headers: "Authorization": "Bearer " + token https.get(options, (res) -> #console.log "Got response: " + res.statusCode res.on "data", (chunk) -> data +=chunk res.on "end", () -> data = JSON.parse(data) callback null, data ).on "error", (e) -> console.log "Got error: " + e.message return getRefreshToken = (user, callback) -> if user.oauth_token.indexOf(';') isnt -1 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) code = user.oauth_token.substr(user.oauth_token.indexOf(';')+1 ,user.oauth_token.length) else token = user.oauth_token data = "" post_data = querystring.stringify( "code": code "client_id": CLIENTID "client_secret": CLIENTSECRET "grant_type": "authorization_code" "redirect_uri": "postmessage" ) headers = "Authorization": "Bearer " + token "Content-Type": "application/x-www-form-urlencoded" 'Content-Length': post_data.length options = host: "accounts.google.com" port: 443 path: "/o/oauth2/token" method: "POST" headers: headers req = https.request(options, (res) -> res.setEncoding "utf8" res.on "data", (d) -> data += d res.on "end", () -> #console.log data data = decodeURI(data) data = JSON.parse(data) callback null, data #console.log '\n\n' + JSON.stringify(data.refresh_token) # usermap updaten und später mit extra arbeiten return ) req.write post_data req.end() return updateToken = (user, callback) -> token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) extra = user.extra_token data = "" post_data = querystring.stringify( "refresh_token": extra "client_id": CLIENTID "client_secret": CLIENTSECRET "grant_type": "refresh_token" ) headers = "Authorization": "Bearer " + token "Content-Type": "application/x-www-form-urlencoded" 'Content-Length': post_data.length options = host: "accounts.google.com" port: 443 path: "/o/oauth2/token" method: "POST" headers: headers req = https.request(options, (res) -> res.setEncoding "utf8" res.on "data", (d) -> data += d res.on "end", () -> data = decodeURI(data) data = JSON.parse(data) Routes.saveUsermap(user.user_pumpio,user.user_ESN,data.access_token, user.extra_token, callback) #console.log '\n\n' + JSON.stringify(data.refresh_token) # usermap updaten und später mit extra arbeiten return ) req.write post_data req.end() return exports.sync = sync exports.getUser = getUser exports.getRefreshToken = getRefreshToken exports.getFriends = getFriends