Mercurial > pumpbridge
comparison src/google.coffee @ 0:b73191efc65b
Initial import of pumpbridge (bloody bloody alpha)
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Thu, 05 Jun 2014 10:35:15 +0200 |
parents | |
children | 98a070c98982 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b73191efc65b |
---|---|
1 # Copyright (C) 2014 by Intevation GmbH | |
2 # Author: Mathias Gebbe <mgebbe@intevation.de> | |
3 # | |
4 # This file is Free Software under the Apache License, Version 2.0 | |
5 # (the "License"); and comes with ABSOLUTELY NO WARRANTY! | |
6 # You may not use this file except in compliance with the License. | |
7 # See LICENSE for details. | |
8 | |
9 https = require("https") | |
10 async = require("async") | |
11 _ = require("underscore") | |
12 Sync = require("./sync") | |
13 Routes = require("./routes") | |
14 querystring = require("querystring") | |
15 EdgeControl = require("./edgecontrol") | |
16 Edge = require("./edge") | |
17 Pump = require("./pumpio") | |
18 FromESN = require("./fromESN") | |
19 Config = require("./config") | |
20 querystring = require('querystring') | |
21 | |
22 config = Config.config | |
23 | |
24 bridgeid = config.bridgeid | |
25 API_KEY = config.gpAPI_KEY | |
26 CLIENTID = config.gpCLIENTID | |
27 CLIENTSECRET = config.gpCLIENTSECRET | |
28 | |
29 ############### | |
30 # google sync # | |
31 ############### | |
32 sync = (user) -> | |
33 return if not (user?) | |
34 me = user.user_pumpio | |
35 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
36 | |
37 async.waterfall [ | |
38 (callback) -> | |
39 updateToken(user,callback) | |
40 (updateuser,callback) -> | |
41 user = updateuser | |
42 getUser(user) | |
43 getFriends(user,callback) | |
44 ], (err, friends) -> | |
45 # für jeden Freunde tue dies: hier bewusst von async.eachSeries friends, ((friend, callback) -> auf async.each gewechselt | |
46 async.each friends, ((friend, callback) -> | |
47 async.waterfall [ | |
48 (callback) -> | |
49 getStream(user, friend, callback) | |
50 (stream, callback) -> | |
51 _.each stream.items, (post) -> | |
52 async.waterfall [ | |
53 (callback) -> | |
54 FromESN.search {uid: post.id + "@google_to_" + me, recipientUser: me}, callback | |
55 (result, callback) -> | |
56 return if result.length isnt 0 | |
57 Sync.postParser post, null, 'google', callback | |
58 (parsed, callback) -> | |
59 Pump.postUser bridgeid, me, parsed, callback | |
60 (pumppost, callback) -> | |
61 pumppost = JSON.parse(pumppost) | |
62 FromESN.create | |
63 postid: post.id + "@google" | |
64 sourceUser: post.actor.id | |
65 sourcePost: post.url | |
66 pumpPost: pumppost.object.id | |
67 recipientUser: me | |
68 created: Date.now() | |
69 , callback | |
70 ], (err, result) -> | |
71 #console.log err | |
72 #console.log result | |
73 return | |
74 callback null, null | |
75 ], (err, result) -> | |
76 #console.log 'done.' | |
77 callback null, 'done' | |
78 ), (err) -> | |
79 if err | |
80 console.log 'one post fail to process' | |
81 else | |
82 console.log 'all google friends processed' | |
83 return | |
84 return | |
85 | |
86 | |
87 ####################################### | |
88 ###### get user info googleplus ###### | |
89 ####################################### | |
90 getUser = (user) -> | |
91 data = "" | |
92 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
93 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
94 options = | |
95 host: "www.googleapis.com" | |
96 port: 443 | |
97 path: "/plus/v1/people/" + id + '?access_token=' + token | |
98 | |
99 https.get(options, (res) -> | |
100 #console.log "Got response: " + res.statusCode | |
101 res.on "data", (chunk) -> | |
102 data += chunk | |
103 return | |
104 | |
105 res.on "end", () -> | |
106 user = JSON.parse(data) | |
107 Routes.updateUserDB(user.id+'@google',user.displayName,user.displayName,user.url,user.image.url) unless typeof user.id is "undefined" | |
108 | |
109 ).on "error", (e) -> | |
110 console.log "Got error: " + e.message | |
111 return | |
112 return | |
113 | |
114 | |
115 ####################################### | |
116 ###### get google friends ###### | |
117 ####################################### | |
118 getFriends = (user, callback) -> | |
119 return if typeof user is "undefined" | |
120 data = "" | |
121 friends = new Array() | |
122 me = user.user_pumpio | |
123 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
124 if user.oauth_token.indexOf(';') isnt -1 | |
125 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
126 else | |
127 token = user.oauth_token | |
128 options = | |
129 host: "www.googleapis.com" | |
130 port: 443 | |
131 path: "/plus/v1/people/me/people/visible?key=" + API_KEY + "&" | |
132 headers: | |
133 "Authorization": "Bearer " + token | |
134 | |
135 https.get(options, (res) -> | |
136 #console.log "Got response: " + res.statusCode | |
137 res.on "data", (chunk) -> | |
138 data +=chunk | |
139 | |
140 res.on "end", () -> | |
141 async.waterfall [ | |
142 (callback) -> | |
143 users = JSON.parse(decodeURI(data)) | |
144 return if (users.error?) | |
145 callback null,users | |
146 (users, callback) -> | |
147 EdgeControl.removeEdges(me,'@google') | |
148 _.each users.items, (user) -> | |
149 friends.push(user.id) | |
150 Routes.updateUserDB(user.id+'@google',user.displayName,user.displayName,user.url,user.image.url) unless typeof user.id is "undefined" | |
151 EdgeControl.addEdge(me,user.id+'@google') unless typeof user.id is "undefined" | |
152 callback null, friends | |
153 ], (err, friends) -> | |
154 callback null, friends | |
155 return | |
156 | |
157 ).on "error", (e) -> | |
158 console.log "Got error: " + e.message | |
159 | |
160 return | |
161 | |
162 ####################################### | |
163 ###### get google stream ###### | |
164 ####################################### | |
165 getStream = (user, targetid, callback) -> | |
166 data = "" | |
167 max_results = 3 | |
168 me = user.user_pumpio | |
169 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
170 if user.oauth_token.indexOf(';') isnt -1 | |
171 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
172 else | |
173 token = user.oauth_token | |
174 options = | |
175 host: "www.googleapis.com" | |
176 port: 443 | |
177 path: "/plus/v1/people/" + targetid + "/activities/public?maxResults="+max_results+"&key=" + API_KEY | |
178 headers: | |
179 "Authorization": "Bearer " + token | |
180 | |
181 https.get(options, (res) -> | |
182 #console.log "Got response: " + res.statusCode | |
183 res.on "data", (chunk) -> | |
184 data +=chunk | |
185 | |
186 res.on "end", () -> | |
187 data = JSON.parse(data) | |
188 callback null, data | |
189 | |
190 ).on "error", (e) -> | |
191 console.log "Got error: " + e.message | |
192 | |
193 return | |
194 | |
195 getRefreshToken = (user, callback) -> | |
196 if user.oauth_token.indexOf(';') isnt -1 | |
197 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
198 else | |
199 token = user.oauth_token | |
200 code = user.oauth_token.substr(user.oauth_token.indexOf(';')+1 ,user.oauth_token.lenght) | |
201 data = "" | |
202 post_data = querystring.stringify( | |
203 "code": code | |
204 "client_id": CLIENTID | |
205 "client_secret": CLIENTSECRET | |
206 "grant_type": "authorization_code" | |
207 "redirect_uri": "postmessage" | |
208 ) | |
209 | |
210 headers = | |
211 "Authorization": "Bearer " + token | |
212 "Content-Type": "application/x-www-form-urlencoded" | |
213 'Content-Length': post_data.length | |
214 | |
215 options = | |
216 host: "accounts.google.com" | |
217 port: 443 | |
218 path: "/o/oauth2/token" | |
219 method: "POST" | |
220 headers: headers | |
221 | |
222 req = https.request(options, (res) -> | |
223 res.setEncoding "utf8" | |
224 res.on "data", (d) -> | |
225 data += d | |
226 | |
227 res.on "end", () -> | |
228 #console.log data | |
229 data = decodeURI(data) | |
230 data = JSON.parse(data) | |
231 callback null, data | |
232 #console.log '\n\n' + JSON.stringify(data.refresh_token) | |
233 # usermap updaten und später mit extra arbeiten | |
234 return | |
235 ) | |
236 req.write post_data | |
237 req.end() | |
238 | |
239 | |
240 return | |
241 | |
242 updateToken = (user, callback) -> | |
243 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
244 extra = user.extra_token | |
245 data = "" | |
246 post_data = querystring.stringify( | |
247 "refresh_token": extra | |
248 "client_id": CLIENTID | |
249 "client_secret": CLIENTSECRET | |
250 "grant_type": "refresh_token" | |
251 ) | |
252 | |
253 headers = | |
254 "Authorization": "Bearer " + token | |
255 "Content-Type": "application/x-www-form-urlencoded" | |
256 'Content-Length': post_data.length | |
257 | |
258 options = | |
259 host: "accounts.google.com" | |
260 port: 443 | |
261 path: "/o/oauth2/token" | |
262 method: "POST" | |
263 headers: headers | |
264 | |
265 req = https.request(options, (res) -> | |
266 res.setEncoding "utf8" | |
267 res.on "data", (d) -> | |
268 data += d | |
269 | |
270 res.on "end", () -> | |
271 data = decodeURI(data) | |
272 data = JSON.parse(data) | |
273 Routes.saveUsermap(user.user_pumpio,user.user_ESN,data.access_token, user.extra_token, callback) | |
274 #console.log '\n\n' + JSON.stringify(data.refresh_token) | |
275 # usermap updaten und später mit extra arbeiten | |
276 return | |
277 ) | |
278 req.write post_data | |
279 req.end() | |
280 | |
281 | |
282 return | |
283 | |
284 exports.sync = sync | |
285 exports.getUser = getUser | |
286 exports.getRefreshToken = getRefreshToken | |
287 exports.getFriends = getFriends |