var Promise = require('promise') var BasePlugin = require('./base.js') class Twitter extends BasePlugin { constructor (config) { super(config) this.name = 'Twitter' this.config = config } test (input) { // Kernel version if (input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) { return true } return false } getTweet (statusId) { var Twitter = require('twitter') var client = new Twitter({ consumer_key: this.config.keys.twitter.consumerKey, consumer_secret: this.config.keys.twitter.consumerSecret, access_token_key: '', access_token_secret: '' }) var promise = new Promise(function (resolve, reject) { client.get('statuses/show/' + statusId, function (error, tweet, response) { if (!error) { var user = tweet.user.screen_name var time = tweet.created_at var dateTime = new Date(time) var dateTimeLocale = dateTime.toLocaleDateString('fi-FI') + ' ' + dateTime.getHours() + '.' + dateTime.getMinutes() var str = '@' + user + ': "' + tweet.text + '" (' + dateTimeLocale + ')' str = str.replace(/(?:\r\n|\r|\n)/g, ' ') resolve(str) } else { reject(new Error()) } }) }) return promise } result (input) { var that = this var resultPromise = new Promise(function (resolve, reject) { var res = input.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/) if (res !== null) { var status = res[6] that.getTweet(status).then(function (tweet) { resolve('Twiitti: ' + tweet) }, function () { reject(new Error('Twiitti: Jokin virhe tapahtui')) }) } else { reject(new Error('Twiitti: Jokin virhe tapahtui')) } }) return resultPromise } } module.exports = Twitter