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