mgebbe@3: # twitter.js mgebbe@3: # mgebbe@3: # data object representing twitter.com mgebbe@3: # mgebbe@3: # Copyright 2013, E14N (https://e14n.com/) mgebbe@3: # all changes Copyright 2014, Intevation GmbH (https://intevation.org) mgebbe@3: # mgebbe@3: # Licensed under the Apache License, Version 2.0 (the "License"); mgebbe@3: # you may not use this file except in compliance with the License. mgebbe@3: # You may obtain a copy of the License at mgebbe@3: # mgebbe@3: # http://www.apache.org/licenses/LICENSE-2.0 mgebbe@3: # mgebbe@3: # Unless required by applicable law or agreed to in writing, software mgebbe@3: # distributed under the License is distributed on an "AS IS" BASIS, mgebbe@3: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. mgebbe@3: # See the License for the specific language governing permissions and mgebbe@3: # limitations under the License. mgebbe@3: mgebbe@3: _ = require("underscore") mgebbe@3: async = require("async") mgebbe@3: OAuth = require("oauth").OAuth mgebbe@3: PumpIOClientApp = require("pump.io-client-app") mgebbe@3: DatabankObject = require("databank").DatabankObject mgebbe@3: RequestToken = PumpIOClientApp.RequestToken mgebbe@3: Usermap = require("./usermap") mgebbe@3: Pump = require("./pumpio") mgebbe@5: FromESN = require("./fromESN") mgebbe@3: ToESN = require("./toESN") mgebbe@5: Sync = require("./sync") mgebbe@3: mgebbe@3: module.exports = (config) -> mgebbe@5: bridgeid = config.bridgeid mgebbe@3: client_id = config.twclient_id mgebbe@3: client_secret = config.twclient_secret mgebbe@7: interval = config.interval mgebbe@7: if not (interval?) mgebbe@7: interval = 15 * 60 * 1000 mgebbe@3: request_token_endpoint = "https://api.twitter.com/oauth/request_token" mgebbe@3: access_token_endpoint = "https://api.twitter.com/oauth/access_token" mgebbe@3: authorization_endpoint = "https://api.twitter.com/oauth/authorize" mgebbe@3: whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json" mgebbe@3: hostname = "twitter.com" mgebbe@3: mgebbe@3: Twitter = mgebbe@3: getRequestToken: (site, callback) -> mgebbe@3: oa = Twitter.getOAuth(site) mgebbe@3: async.waterfall [ mgebbe@3: (callback) -> mgebbe@3: oa.getOAuthRequestToken callback mgebbe@3: (token, secret, other, callback) -> mgebbe@3: RequestToken.create mgebbe@3: token: token mgebbe@3: secret: secret mgebbe@3: hostname: hostname mgebbe@3: , callback mgebbe@3: ], callback mgebbe@3: return mgebbe@3: mgebbe@3: authorizeURL: (rt) -> mgebbe@3: separator = undefined mgebbe@3: if _.contains(authorization_endpoint, "?") mgebbe@3: separator = "&" mgebbe@3: else mgebbe@3: separator = "?" mgebbe@3: authorization_endpoint + separator + "oauth_token=" + rt.token mgebbe@3: mgebbe@3: getAccessToken: (site, rt, verifier, callback) -> mgebbe@3: oa = Twitter.getOAuth(site) mgebbe@3: oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback mgebbe@3: return mgebbe@3: mgebbe@3: whoami: (site, token, secret, callback) -> mgebbe@3: oa = Twitter.getOAuth(site) mgebbe@3: async.waterfall [(callback) -> mgebbe@3: oa.get whoami_endpoint, token, secret, callback mgebbe@3: return mgebbe@3: ], (err, doc, response) -> mgebbe@3: obj = undefined mgebbe@3: if err mgebbe@3: callback err, null mgebbe@3: else mgebbe@3: try mgebbe@3: obj = JSON.parse(doc) mgebbe@3: callback null, obj mgebbe@3: catch e mgebbe@3: callback e, null mgebbe@3: return mgebbe@3: mgebbe@3: return mgebbe@3: mgebbe@3: sync: (user) -> mgebbe@3: me = user.user_pumpio mgebbe@5: id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) mgebbe@3: token = user.oauth_token mgebbe@3: secret = user.extra_token mgebbe@3: util = require("util") mgebbe@3: twitter = require("twitter") mgebbe@3: twit = new twitter( mgebbe@3: consumer_key: client_id mgebbe@3: consumer_secret: client_secret mgebbe@3: access_token_key: token mgebbe@3: access_token_secret: secret mgebbe@3: ) mgebbe@3: mgebbe@3: # GET PUBLIC PUMP POSTS AND POST THEM mgebbe@3: async.waterfall [ mgebbe@3: (callback) -> mgebbe@3: Usermap.search {id: me + '_to_' + me}, callback mgebbe@3: (user, callback) -> mgebbe@8: if (user[0]?) mgebbe@8: Pump.getUserFeed(user[0],callback) mgebbe@8: else mgebbe@8: return mgebbe@3: (feed, callback) -> mgebbe@7: ti = new Date().getTime() - interval mgebbe@3: async.eachSeries feed.items, ((post, callback) -> mgebbe@3: # do for each post mgebbe@7: ts = Date.parse(post.updated) mgebbe@7: 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") mgebbe@3: ToESN.search {uid: post.object.id + "@twitter"}, (err, result) -> mgebbe@3: if result.length is 0 mgebbe@4: # post to twitter mgebbe@7: status = "" mgebbe@3: text = post.object.content.replace(/<(?:.|\n)*?>/gm, '') mgebbe@7: if post.verb is "share" mgebbe@7: status = post.object.author.url + " wrotes: " mgebbe@3: if text.length <= 140 mgebbe@7: status += text mgebbe@3: else mgebbe@7: status += text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url mgebbe@3: twit.verifyCredentials((data) -> mgebbe@3: #console.log util.inspect(data) mgebbe@3: return mgebbe@3: ).updateStatus status, (data) -> mgebbe@3: async.waterfall [ mgebbe@3: (callback) -> mgebbe@3: savePost = new ToESN() mgebbe@3: savePost.uid = post.object.id + "@twitter" mgebbe@3: savePost.sourceUser = post.actor.id mgebbe@3: savePost.sourcePost = post.object.id mgebbe@3: savePost.targetUser = user.user_ESN mgebbe@3: savePost.targetPost = data.id mgebbe@3: savePost.recipientUser = 'public' mgebbe@3: savePost.updated = Date.now() mgebbe@3: savePost.save callback mgebbe@3: ], (err, result) -> mgebbe@3: #console.log util.inspect(data) mgebbe@3: return mgebbe@3: callback null, 'done' mgebbe@3: ), (err) -> mgebbe@3: callback null, 'done' mgebbe@3: ],(err, result) -> mgebbe@3: mgebbe@5: ###################################### mgebbe@5: # get tweets and post them to pump.io# mgebbe@5: ###################################### mgebbe@5: twit.verifyCredentials((data) -> mgebbe@5: #console.log util.inspect(data) mgebbe@5: return mgebbe@7: ).getHomeTimeline {include_rts: true, count: 20 }, (data) -> mgebbe@5: #console.log util.inspect(data) mgebbe@7: ti = (new Date().getTime() - interval) mgebbe@5: _.each data, (tweet) -> mgebbe@5: async.waterfall [ mgebbe@5: (callback) -> mgebbe@5: FromESN.search {uid: tweet.id + "@twitter_to_" + me, recipientUser: me}, callback mgebbe@5: (result, callback) -> mgebbe@7: return if result.length isnt 0 or tweet.user.id_str is id mgebbe@7: twitterdate = new Date(Date.parse(tweet.created_at.replace(/( +)/, " UTC$1"))) mgebbe@7: ts = Date.parse(twitterdate) mgebbe@7: if (ts >= ti) mgebbe@7: Sync.postParser tweet, null, 'twitter', callback mgebbe@7: return mgebbe@5: (parsed, callback) -> mgebbe@5: Pump.postUser bridgeid, me, parsed, callback mgebbe@5: (pumppost, callback) -> mgebbe@5: pumppost = JSON.parse(pumppost) mgebbe@5: FromESN.create mgebbe@5: postid: tweet.id + "@twitter" mgebbe@5: sourceUser: tweet.user.id mgebbe@5: sourcePost: 'https://twitter.com/'+tweet.user.name+"/status/" + tweet.id mgebbe@5: pumpPost: pumppost.object.id mgebbe@5: recipientUser: me mgebbe@5: created: Date.now() mgebbe@5: , callback mgebbe@5: ], (err, result) -> mgebbe@5: #console.log result mgebbe@5: # end mgebbe@5: return mgebbe@3: return mgebbe@3: mgebbe@5: return mgebbe@7: mgebbe@7: parseTwitterDate : (text) -> mgebbe@7: mgebbe@3: getOAuth: (site) -> mgebbe@3: 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 mgebbe@3: "User-Agent": site.userAgent() mgebbe@3: ) mgebbe@3: mgebbe@3: Twitter