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