Mercurial > pumpbridge
annotate src/twitter.coffee @ 28:faeb3b96bdeb
use html-to-text
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Tue, 29 Jul 2014 12:14:02 +0200 |
parents | f27ddfa42e94 |
children | 3e3fa35e3ce2 |
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") |
28 | 32 htmlToText = require('html-to-text') |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
33 twitter = require("twitter") |
3 | 34 |
35 module.exports = (config) -> | |
5 | 36 bridgeid = config.bridgeid |
3 | 37 client_id = config.twclient_id |
38 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
|
39 interval = config.interval |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
40 if not (interval?) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
41 interval = 15 * 60 * 1000 |
3 | 42 request_token_endpoint = "https://api.twitter.com/oauth/request_token" |
43 access_token_endpoint = "https://api.twitter.com/oauth/access_token" | |
44 authorization_endpoint = "https://api.twitter.com/oauth/authorize" | |
45 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json" | |
46 hostname = "twitter.com" | |
47 | |
48 Twitter = | |
49 getRequestToken: (site, callback) -> | |
50 oa = Twitter.getOAuth(site) | |
51 async.waterfall [ | |
52 (callback) -> | |
53 oa.getOAuthRequestToken callback | |
54 (token, secret, other, callback) -> | |
55 RequestToken.create | |
56 token: token | |
57 secret: secret | |
58 hostname: hostname | |
59 , callback | |
60 ], callback | |
61 return | |
62 | |
63 authorizeURL: (rt) -> | |
64 separator = undefined | |
65 if _.contains(authorization_endpoint, "?") | |
66 separator = "&" | |
67 else | |
68 separator = "?" | |
69 authorization_endpoint + separator + "oauth_token=" + rt.token | |
70 | |
71 getAccessToken: (site, rt, verifier, callback) -> | |
72 oa = Twitter.getOAuth(site) | |
73 oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback | |
74 return | |
75 | |
76 whoami: (site, token, secret, callback) -> | |
77 oa = Twitter.getOAuth(site) | |
78 async.waterfall [(callback) -> | |
79 oa.get whoami_endpoint, token, secret, callback | |
80 return | |
81 ], (err, doc, response) -> | |
82 obj = undefined | |
83 if err | |
84 callback err, null | |
85 else | |
86 try | |
87 obj = JSON.parse(doc) | |
88 callback null, obj | |
89 catch e | |
90 callback e, null | |
91 return | |
92 | |
93 return | |
94 | |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
95 syncToESN: (user) -> |
3 | 96 me = user.user_pumpio |
5 | 97 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) |
3 | 98 token = user.oauth_token |
99 secret = user.extra_token | |
100 twit = new twitter( | |
101 consumer_key: client_id | |
102 consumer_secret: client_secret | |
103 access_token_key: token | |
104 access_token_secret: secret | |
105 ) | |
106 | |
107 # GET PUBLIC PUMP POSTS AND POST THEM | |
108 async.waterfall [ | |
109 (callback) -> | |
110 Usermap.search {id: me + '_to_' + me}, callback | |
111 (user, callback) -> | |
8
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
112 if (user[0]?) |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
113 console.log "get feed from" + me |
8
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
114 Pump.getUserFeed(user[0],callback) |
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
115 else |
6fd0a307850f
check empty strings and users
Mathias Gebbe <mgebbe@intevation.de>
parents:
7
diff
changeset
|
116 return |
3 | 117 (feed, callback) -> |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
118 return if not feed? |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
119 ti = new Date().getTime() - interval |
28 | 120 async.eachLimit feed.items, 5, ((post, callback) -> |
3 | 121 # do for each post |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
122 ts = Date.parse(post.updated) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
123 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 | 124 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) -> |
125 if result.length is 0 | |
4
f352c74a6a5b
remove typeof, corrected facebooks toESN entrys
Mathias Gebbe <mgebbe@intevation.de>
parents:
3
diff
changeset
|
126 # post to twitter |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
127 status = "" |
28 | 128 text = post.object.content |
129 text = htmlToText.fromString(text,wordwrap: 140) | |
130 #text = post.object.content.replace(/<(?:.|\n)*?>/gm, '') | |
131 #re = new RegExp(String.fromCharCode(160), "g") | |
132 #text = text.replace(re, " ") | |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
133 if post.verb is "share" |
24 | 134 status = post.object.author.url + " writes: " |
3 | 135 if text.length <= 140 |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
136 status += text |
3 | 137 else |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
138 status += text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url |
3 | 139 twit.verifyCredentials((data) -> |
140 #console.log util.inspect(data) | |
141 return | |
142 ).updateStatus status, (data) -> | |
143 async.waterfall [ | |
144 (callback) -> | |
145 savePost = new ToESN() | |
146 savePost.uid = post.object.id + "@twitter" | |
147 savePost.sourceUser = post.actor.id | |
148 savePost.sourcePost = post.object.id | |
149 savePost.targetUser = user.user_ESN | |
150 savePost.targetPost = data.id | |
151 savePost.recipientUser = 'public' | |
152 savePost.updated = Date.now() | |
153 savePost.save callback | |
154 ], (err, result) -> | |
155 #console.log util.inspect(data) | |
156 return | |
157 callback null, 'done' | |
158 ), (err) -> | |
159 callback null, 'done' | |
160 ],(err, result) -> | |
28 | 161 console.log "done ToESN twitter user " + me |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
162 return |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
163 return |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
164 |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
165 syncFromESN: (user) -> |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
166 me = user.user_pumpio |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
167 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
168 token = user.oauth_token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
169 secret = user.extra_token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
170 twit = new twitter( |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
171 consumer_key: client_id |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
172 consumer_secret: client_secret |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
173 access_token_key: token |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
174 access_token_secret: secret |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
175 ) |
5 | 176 ###################################### |
177 # get tweets and post them to pump.io# | |
178 ###################################### | |
179 twit.verifyCredentials((data) -> | |
180 #console.log util.inspect(data) | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
181 console.log "get twitter HomeTimeline " + me |
5 | 182 return |
28 | 183 ).getHomeTimeline {include_rts: true, count: 15 }, (data) -> |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
184 console.log "got twitter HomeTimeline " + me |
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
185 return if not data? or data.length is 0 |
28 | 186 console.log "data isnt empty for " + me |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
187 ti = (new Date().getTime() - interval) |
28 | 188 async.eachLimit data, 1, ((tweet, cb) -> |
5 | 189 async.waterfall [ |
190 (callback) -> | |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
191 FromESN.search {uid: tweet.id + "@twitter_to_" + me}, callback |
5 | 192 (result, callback) -> |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
193 return if result.length isnt 0 or tweet.user.id_str is id |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
194 console.log "new tweet found" |
7
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
195 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
|
196 ts = Date.parse(twitterdate) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
197 if (ts >= ti) |
2123f355ab68
go back interval or 15 min, delete credentials, layout
Mathias Gebbe <mgebbe@intevation.de>
parents:
5
diff
changeset
|
198 Sync.postParser tweet, null, 'twitter', callback |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
199 else |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
200 return |
5 | 201 (parsed, callback) -> |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
202 console.log "try to post new tweet" |
5 | 203 Pump.postUser bridgeid, me, parsed, callback |
204 (pumppost, callback) -> | |
27
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
205 try |
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
206 pumppost = JSON.parse(pumppost) |
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
207 catch err |
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
208 console.log "parse err " + err |
f27ddfa42e94
try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents:
24
diff
changeset
|
209 return |
5 | 210 FromESN.create |
211 postid: tweet.id + "@twitter" | |
212 sourceUser: tweet.user.id | |
213 sourcePost: 'https://twitter.com/'+tweet.user.name+"/status/" + tweet.id | |
214 pumpPost: pumppost.object.id | |
215 recipientUser: me | |
216 created: Date.now() | |
217 , callback | |
218 ], (err, result) -> | |
28 | 219 console.log "post a tweet to user " + me |
17
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
220 cb() |
a52b5b244e51
removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents:
8
diff
changeset
|
221 ), (err) -> |
22
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
222 if (err) |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
223 console.log "Error: " + err |
b90e6df48d2d
eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents:
17
diff
changeset
|
224 else |
28 | 225 console.log "done FromESN twitter user " + me |
3 | 226 return |
227 | |
228 getOAuth: (site) -> | |
229 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 | |
230 "User-Agent": site.userAgent() | |
231 ) | |
232 | |
233 Twitter |