Mercurial > pumpbridge
annotate src/twitter.coffee @ 22:b90e6df48d2d
eachLimit instead of each
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Mon, 16 Jun 2014 17:31:05 +0200 |
parents | a52b5b244e51 |
children | 8c56fdbac0fb |
rev | line source |
---|---|
3 | 1 # twitter.js |
2 # | |
3 # data object representing twitter.com | |
4 # | |
5 # Copyright 2013, E14N (https://e14n.com/) | |
6 # all changes Copyright 2014, Intevation GmbH (https://intevation.org) | |
7 # | |
8 # Licensed under the Apache License, Version 2.0 (the "License"); | |
9 # you may not use this file except in compliance with the License. | |
10 # You may obtain a copy of the License at | |
11 # | |
12 # http://www.apache.org/licenses/LICENSE-2.0 | |
13 # | |
14 # Unless required by applicable law or agreed to in writing, software | |
15 # distributed under the License is distributed on an "AS IS" BASIS, | |
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
17 # See the License for the specific language governing permissions and | |
18 # limitations under the License. | |
19 | |
20 _ = require("underscore") | |
21 async = require("async") | |
22 OAuth = require("oauth").OAuth | |
23 PumpIOClientApp = require("pump.io-client-app") | |
24 DatabankObject = require("databank").DatabankObject | |
25 RequestToken = PumpIOClientApp.RequestToken | |
26 Usermap = require("./usermap") | |
27 Pump = require("./pumpio") | |
5 | 28 FromESN = require("./fromESN") |
3 | 29 ToESN = require("./toESN") |
5 | 30 Sync = require("./sync") |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
31 util = require("util") |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
32 twitter = require("twitter") |
3 | 33 |
34 module.exports = (config) -> | |
5 | 35 bridgeid = config.bridgeid |
3 | 36 client_id = config.twclient_id |
37 client_secret = config.twclient_secret | |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
38 interval = config.interval |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
39 if not (interval?) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
40 interval = 15 * 60 * 1000 |
3 | 41 request_token_endpoint = "https://api.twitter.com/oauth/request_token" |
42 access_token_endpoint = "https://api.twitter.com/oauth/access_token" | |
43 authorization_endpoint = "https://api.twitter.com/oauth/authorize" | |
44 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json" | |
45 hostname = "twitter.com" | |
46 | |
47 Twitter = | |
48 getRequestToken: (site, callback) -> | |
49 oa = Twitter.getOAuth(site) | |
50 async.waterfall [ | |
51 (callback) -> | |
52 oa.getOAuthRequestToken callback | |
53 (token, secret, other, callback) -> | |
54 RequestToken.create | |
55 token: token | |
56 secret: secret | |
57 hostname: hostname | |
58 , callback | |
59 ], callback | |
60 return | |
61 | |
62 authorizeURL: (rt) -> | |
63 separator = undefined | |
64 if _.contains(authorization_endpoint, "?") | |
65 separator = "&" | |
66 else | |
67 separator = "?" | |
68 authorization_endpoint + separator + "oauth_token=" + rt.token | |
69 | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
70 return |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
71 |
3 | 72 getAccessToken: (site, rt, verifier, callback) -> |
73 oa = Twitter.getOAuth(site) | |
74 oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback | |
75 return | |
76 | |
77 whoami: (site, token, secret, callback) -> | |
78 oa = Twitter.getOAuth(site) | |
79 async.waterfall [(callback) -> | |
80 oa.get whoami_endpoint, token, secret, callback | |
81 return | |
82 ], (err, doc, response) -> | |
83 obj = undefined | |
84 if err | |
85 callback err, null | |
86 else | |
87 try | |
88 obj = JSON.parse(doc) | |
89 callback null, obj | |
90 catch e | |
91 callback e, null | |
92 return | |
93 | |
94 return | |
95 | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
96 syncFromESN: (user) -> |
3 | 97 me = user.user_pumpio |
5 | 98 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) |
3 | 99 token = user.oauth_token |
100 secret = user.extra_token | |
101 twit = new twitter( | |
102 consumer_key: client_id | |
103 consumer_secret: client_secret | |
104 access_token_key: token | |
105 access_token_secret: secret | |
106 ) | |
107 | |
108 # GET PUBLIC PUMP POSTS AND POST THEM | |
109 async.waterfall [ | |
110 (callback) -> | |
111 Usermap.search {id: me + '_to_' + me}, callback | |
112 (user, callback) -> | |
8
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
113 if (user[0]?) |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
114 console.log "get feed from" + me |
8
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
115 Pump.getUserFeed(user[0],callback) |
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
116 else |
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
117 return |
3 | 118 (feed, callback) -> |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
119 return if not feed? |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
120 ti = new Date().getTime() - interval |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
121 async.eachLimit feed.items, 10, ((post, callback) -> |
3 | 122 # do for each post |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
123 ts = Date.parse(post.updated) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
124 if (ts >= ti and post.verb is "post" or post.verb is "share") and (post.object.objectType is "note" or post.object.objectType is "image") and (Pump.isPublicActivity(post)) and (typeof post.object.deleted is "undefined") |
3 | 125 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) -> |
126 if result.length is 0 | |
4
f352c74a6a5b
remove typeof, corrected facebooks toESN entrys
Mathias Gebbe <mgebbe@intevation.de>
parents:
3
diff
changeset
|
127 # post to twitter |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
128 status = "" |
3 | 129 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '') |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
130 re = new RegExp(String.fromCharCode(160), "g") |
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
131 text = text.replace(re, " ") |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
132 if post.verb is "share" |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
133 status = post.object.author.url + " wrotes: " |
3 | 134 if text.length <= 140 |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
135 status += text |
3 | 136 else |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
137 status += text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url |
3 | 138 twit.verifyCredentials((data) -> |
139 #console.log util.inspect(data) | |
140 return | |
141 ).updateStatus status, (data) -> | |
142 async.waterfall [ | |
143 (callback) -> | |
144 savePost = new ToESN() | |
145 savePost.uid = post.object.id + "@twitter" | |
146 savePost.sourceUser = post.actor.id | |
147 savePost.sourcePost = post.object.id | |
148 savePost.targetUser = user.user_ESN | |
149 savePost.targetPost = data.id | |
150 savePost.recipientUser = 'public' | |
151 savePost.updated = Date.now() | |
152 savePost.save callback | |
153 ], (err, result) -> | |
154 #console.log util.inspect(data) | |
155 return | |
156 callback null, 'done' | |
157 ), (err) -> | |
158 callback null, 'done' | |
159 ],(err, result) -> | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
160 console.log "done ToESN twitter user" + me |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
161 return |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
162 return |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
163 |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
164 syncToESN: (user) -> |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
165 me = user.user_pumpio |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
166 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
167 token = user.oauth_token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
168 secret = user.extra_token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
169 twit = new twitter( |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
170 consumer_key: client_id |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
171 consumer_secret: client_secret |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
172 access_token_key: token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
173 access_token_secret: secret |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
174 ) |
5 | 175 ###################################### |
176 # get tweets and post them to pump.io# | |
177 ###################################### | |
178 twit.verifyCredentials((data) -> | |
179 #console.log util.inspect(data) | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
180 console.log "get twitter HomeTimeline " + me |
5 | 181 return |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
182 ).getHomeTimeline {include_rts: true, count: 20 }, (data) -> |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
183 ti = (new Date().getTime() - interval) |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
184 return if not data? or data.length is 0 |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
185 async.eachLimit data, 10, ((tweet, cb) -> |
5 | 186 async.waterfall [ |
187 (callback) -> | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
188 FromESN.search {uid: tweet.id + "@twitter_to_" + me}, callback |
5 | 189 (result, callback) -> |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
190 return if result.length isnt 0 or tweet.user.id_str is id |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
191 twitterdate = new Date(Date.parse(tweet.created_at.replace(/( +)/, " UTC$1"))) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
192 ts = Date.parse(twitterdate) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
193 if (ts >= ti) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
194 Sync.postParser tweet, null, 'twitter', callback |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
195 else |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
196 return |
5 | 197 (parsed, callback) -> |
198 Pump.postUser bridgeid, me, parsed, callback | |
199 (pumppost, callback) -> | |
200 pumppost = JSON.parse(pumppost) | |
201 FromESN.create | |
202 postid: tweet.id + "@twitter" | |
203 sourceUser: tweet.user.id | |
204 sourcePost: 'https://twitter.com/'+tweet.user.name+"/status/" + tweet.id | |
205 pumpPost: pumppost.object.id | |
206 recipientUser: me | |
207 created: Date.now() | |
208 , callback | |
209 ], (err, result) -> | |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
210 cb() |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
211 return |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
212 ), (err) -> |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
213 if (err) |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
214 console.log "Error: " + err |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
215 else |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
216 console.log "done ToESN twitter user" + me |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
217 return |
3 | 218 return |
219 | |
220 getOAuth: (site) -> | |
221 new OAuth(request_token_endpoint, access_token_endpoint, client_id, client_secret, "1.0", site.url("/authorized-for-twitter"), "HMAC-SHA1", null, # nonce size; use default | |
222 "User-Agent": site.userAgent() | |
223 ) | |
224 | |
225 Twitter |