comparison src/twitter.coffee @ 3:98a070c98982

add Twitter support
author Mathias Gebbe <mgebbe@intevation.de>
date Thu, 05 Jun 2014 18:02:25 +0200
parents
children f352c74a6a5b
comparison
equal deleted inserted replaced
2:e942a968cb52 3:98a070c98982
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")
28 ToESN = require("./toESN")
29
30
31 module.exports = (config) ->
32 client_id = config.twclient_id
33 client_secret = config.twclient_secret
34 request_token_endpoint = "https://api.twitter.com/oauth/request_token"
35 access_token_endpoint = "https://api.twitter.com/oauth/access_token"
36 authorization_endpoint = "https://api.twitter.com/oauth/authorize"
37 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json"
38 hostname = "twitter.com"
39
40 Twitter =
41 getRequestToken: (site, callback) ->
42 oa = Twitter.getOAuth(site)
43 async.waterfall [
44 (callback) ->
45 oa.getOAuthRequestToken callback
46 (token, secret, other, callback) ->
47 RequestToken.create
48 token: token
49 secret: secret
50 hostname: hostname
51 , callback
52 ], callback
53 return
54
55 authorizeURL: (rt) ->
56 separator = undefined
57 if _.contains(authorization_endpoint, "?")
58 separator = "&"
59 else
60 separator = "?"
61 authorization_endpoint + separator + "oauth_token=" + rt.token
62
63 getAccessToken: (site, rt, verifier, callback) ->
64 oa = Twitter.getOAuth(site)
65 oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback
66 return
67
68 whoami: (site, token, secret, callback) ->
69 oa = Twitter.getOAuth(site)
70 async.waterfall [(callback) ->
71 oa.get whoami_endpoint, token, secret, callback
72 return
73 ], (err, doc, response) ->
74 obj = undefined
75 if err
76 callback err, null
77 else
78 try
79 obj = JSON.parse(doc)
80 callback null, obj
81 catch e
82 callback e, null
83 return
84
85 return
86
87 sync: (user) ->
88 me = user.user_pumpio
89 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@'))
90 token = user.oauth_token
91 secret = user.extra_token
92 util = require("util")
93 twitter = require("twitter")
94 twit = new twitter(
95 consumer_key: client_id
96 consumer_secret: client_secret
97 access_token_key: token
98 access_token_secret: secret
99 )
100
101 #twit.verifyCredentials((data) ->
102 # console.log util.inspect(data)
103 # return
104 #).updateStatus "Test tweet from pumpbridge using " + twitter.VERSION, (data) ->
105 # console.log util.inspect(data)
106 # return
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) ->
113 Pump.getUserFeed(user[0],callback)
114 (feed, callback) ->
115 async.eachSeries feed.items, ((post, callback) ->
116 # do for each post
117 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")
118 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) ->
119 if result.length is 0
120 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '')
121 status = ""
122 if text.length <= 140
123 status = text
124 else
125 status = text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url
126 twit.verifyCredentials((data) ->
127 #console.log util.inspect(data)
128 return
129 ).updateStatus status, (data) ->
130 async.waterfall [
131 (callback) ->
132 savePost = new ToESN()
133 savePost.uid = post.object.id + "@twitter"
134 savePost.sourceUser = post.actor.id
135 savePost.sourcePost = post.object.id
136 savePost.targetUser = user.user_ESN
137 savePost.targetPost = data.id
138 savePost.recipientUser = 'public'
139 savePost.updated = Date.now()
140 savePost.save callback
141 ], (err, result) ->
142 #console.log util.inspect(data)
143 return
144 callback null, 'done'
145 ), (err) ->
146 callback null, 'done'
147 ],(err, result) ->
148 #console.log 'done.'
149
150 return
151
152 getOAuth: (site) ->
153 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
154 "User-Agent": site.userAgent()
155 )
156
157 Twitter
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)