annotate src/twitter.coffee @ 32:ca0b44c1a870 tip

Readme: twitter in the summary, it is best working currently.
author Bernhard Reiter <bernhard@intevation.de>
date Mon, 13 Oct 2014 13:22:17 +0200
parents 8238d312e281
children
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")
22
b90e6df48d2d eachLimit instead of each
Mathias Gebbe <mgebbe@intevation.de>
parents: 17
diff changeset
31 util = require("util")
28
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
34
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
35 module.exports = (config) ->
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
36 bridgeid = config.bridgeid
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
37 client_id = config.twclient_id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
42 request_token_endpoint = "https://api.twitter.com/oauth/request_token"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
43 access_token_endpoint = "https://api.twitter.com/oauth/access_token"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
44 authorization_endpoint = "https://api.twitter.com/oauth/authorize"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
45 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
46 hostname = "twitter.com"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
47
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
48 Twitter =
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
49 getRequestToken: (site, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
50 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
51 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
52 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
53 oa.getOAuthRequestToken callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
54 (token, secret, other, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
55 RequestToken.create
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
56 token: token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
57 secret: secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
58 hostname: hostname
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
59 , callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
60 ], callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
61 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
62
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
63 authorizeURL: (rt) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
64 separator = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
65 if _.contains(authorization_endpoint, "?")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
66 separator = "&"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
67 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
68 separator = "?"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
69 authorization_endpoint + separator + "oauth_token=" + rt.token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
70
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
71 getAccessToken: (site, rt, verifier, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
72 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
73 oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
74 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
75
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
76 whoami: (site, token, secret, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
77 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
78 async.waterfall [(callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
79 oa.get whoami_endpoint, token, secret, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
80 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
81 ], (err, doc, response) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
82 obj = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
83 if err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
84 callback err, null
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
85 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
86 try
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
87 obj = JSON.parse(doc)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
88 callback null, obj
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
89 catch e
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
90 callback e, null
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
91 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
92
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
93 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
94
27
f27ddfa42e94 try/catch; From/To/-ESN changes; google probs
Mathias Gebbe <mgebbe@intevation.de>
parents: 24
diff changeset
95 syncToESN: (user) ->
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
96 me = user.user_pumpio
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
97 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@'))
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
98 token = user.oauth_token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
99 secret = user.extra_token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
100 twit = new twitter(
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
101 consumer_key: client_id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
102 consumer_secret: client_secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
103 access_token_key: token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
104 access_token_secret: secret
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
107 # GET PUBLIC PUMP POSTS AND POST THEM
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
108 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
109 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
110 Usermap.search {id: me + '_to_' + me}, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
120 async.eachLimit feed.items, 1, ((post, callback) ->
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
124 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
128 text = post.object.content
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
129 text = htmlToText.fromString(text,wordwrap: 140)
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
130 #text = post.object.content.replace(/<(?:.|\n)*?>/gm, '')
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
131 #re = new RegExp(String.fromCharCode(160), "g")
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
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
08d9679da0f7 s/wrotes/writes/
Mathias Gebbe <mgebbe@intevation.de>
parents: 23
diff changeset
134 status = post.object.author.url + " writes: "
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
137 else
31
8238d312e281 var checks
Mathias Gebbe <mgebbe@intevation.de>
parents: 30
diff changeset
138 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
139 twit.verifyCredentials((data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
140 #console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
141 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
142 ).updateStatus status, (data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
143 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
144 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
145 savePost = new ToESN()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
146 savePost.uid = post.object.id + "@twitter"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
147 savePost.sourceUser = post.actor.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
148 savePost.sourcePost = post.object.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
149 savePost.targetUser = user.user_ESN
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
150 savePost.targetPost = data.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
151 savePost.recipientUser = 'public'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
152 savePost.updated = Date.now()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
153 savePost.save callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
154 ], (err, result) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
155 #console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
156 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
157 callback null, 'done'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
158 ), (err) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
159 callback null, 'done'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
160 ],(err, result) ->
28
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
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
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
176 ######################################
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
177 # get tweets and post them to pump.io#
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
178 ######################################
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
179 twit.verifyCredentials((data) ->
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
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
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
182 return
28
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
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
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
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)
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
188 async.eachLimit data, 3, ((tweet, cb) ->
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
189 async.waterfall [
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
190 (callback) ->
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
191 FromESN.search {uid: tweet.id_str + "@twitter_to_" + me}, callback
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
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
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
194 console.log "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
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
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
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
203 Pump.postUser bridgeid, me, parsed, callback
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
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
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
210 FromESN.create
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
211 postid: tweet.id_str + "@twitter"
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
212 sourceUser: tweet.user.id
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
213 sourcePost: 'https://twitter.com/'+tweet.user.screen_name+"/status/" + tweet.id_str
5
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
214 pumpPost: pumppost.object.id
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
215 recipientUser: me
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
216 created: Date.now()
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
217 , callback
af9f5b94c019 receive tweets
Mathias Gebbe <mgebbe@intevation.de>
parents: 4
diff changeset
218 ], (err, result) ->
28
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
219 console.log "post a tweet to user " + me
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
220 cb
17
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
faeb3b96bdeb use html-to-text
Mathias Gebbe <mgebbe@intevation.de>
parents: 27
diff changeset
225 console.log "done FromESN twitter user " + me
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
226 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
227
30
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
228 postLike: (user) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
229 i = 0
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
230 me = user.user_pumpio
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
231 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@'))
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
232 token = user.oauth_token
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
233 secret = user.extra_token
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
234 twit = new twitter(
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
235 consumer_key: client_id
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
236 consumer_secret: client_secret
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
237 access_token_key: token
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
238 access_token_secret: secret
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
239 )
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
240 console.log "try 2 find twitter likes for " + me
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
241 async.waterfall [
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
242 (callback) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
243 Usermap.search {id: me + '_to_' + me}, callback
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
244 (pumpuser, callback) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
245 Pump.getLikes(pumpuser[0], callback)
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
246 (likearray, callback) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
247 return if not (likearray?) or likearray.length is 0
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
248 likes = JSON.parse(likearray)
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
249 return if not (likes?) or likes.items.length is 0
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
250 async.each likes.items,((note, cb) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
251 async.waterfall [
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
252 (callback) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
253 FromESN.search {recipientUser: me, pumpPost: note.id}, callback
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
254 (fromesn, callback) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
255 if (fromesn?) and fromesn.length isnt 0 and fromesn[0].sourcePost.indexOf('twitter.com') isnt -1
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
256 #console.log fromesn[0]
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
257 obj = fromesn[0].postid.substr(0,fromesn[0].postid.indexOf('@'))
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
258 twit.post "/favorites/create.json",
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
259 id: obj
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
260 , (data) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
261 console.log "Twitter " + obj + " liked"
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
262 ], (err, result) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
263 ), (err) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
264 ], (err, result) ->
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
265 return
3e3fa35e3ce2 twitter sync likes, recommend mongodb
Mathias Gebbe <mgebbe@intevation.de>
parents: 28
diff changeset
266
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
267 getOAuth: (site) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
268 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
269 "User-Agent": site.userAgent()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
270 )
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
271
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
272 Twitter
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)