annotate src/twitter.coffee @ 7:2123f355ab68

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