Added Mastodon support

This commit is contained in:
lanxu 2017-10-18 23:17:20 +03:00
parent 3fec503fda
commit 480249e3ee
3 changed files with 105 additions and 211 deletions

View File

@ -1,193 +0,0 @@
var getKernelData = require('./plugins/getKernel')();
var res = 'longterm';
getKernelData(res).then(function(data) {
var version = res;
var text = 'kernel: ';
// Store current versions!
var releaseToVersion = {};
data.releases.forEach(function(val) {
var regex = /([0-9]+?)(\.([0-9]+))?(\.([0-9]+))?/;
var result = val.version.match(regex);
var major = result[1];
var minor = result[3];
if(typeof major !== 'undefined' && typeof minor !== 'undefined') {
var releaseWithVersion = val.moniker + '-' + major + '-' + minor;
var release = val.moniker + '';
if(typeof releaseToVersion[releaseWithVersion] !== 'undefined') {
if(releaseToVersion[releaseWithVersion] !== val.version) {
// new release
}
}
if(typeof releaseToVersion[release] === 'undefined') {
releaseToVersion[release] = val.version;
}
releaseToVersion[releaseWithVersion] = val.version;
}
});
if(typeof releaseToVersion[version] !== 'undefined') {
text += 'Viimeisin versio on ' + releaseToVersion[version] + ' (' + version + ')';
} else {
text += 'Viimeisin vakaa versio on ' + data.latest_stable.version;
}
});
/*
var text = 'Hei! https://youtu.be/TLW6eTV8F_I hienoo';
var text = 'Hei! https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo';
var text = 'Hei! https://www.youtu.be/TLW6eTV8F_I https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo';
var regExp = /http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/;
var res = text.match(regExp);
console.log(res[5]);
console.log(res[7]);
var DDG = require('node-ddg-api').DDG;
var ddg = new DDG('my-app-name');
ddg.instantAnswer('dw', {skip_disambig: '0'}, function(err, response) {
console.log(response);
console.log('"' + response.RelatedTopics[0].Text + '" | ' + response.RelatedTopics[0].FirstURL);
});
*/
/*
var tweet = "https://twitter.com/lanxu/status/266184139324157952";
var config = require('./config');
var getTweet = require('./plugins/getTweet')(config);
var tRegExp = /http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/;
var res = tweet.match(tRegExp);
var sta = console.log(res[6]);
getTweet(sta).then(
function(tweet) {
console.log(tweet);
}
);
*/
/*
var Twitter = require('twitter');
*/
/*client.get('statuses/user_timeline', params, function(error, tweets, response){
if (!error) {
console.log(tweets);
}
});*/
/*client.stream('statuses/filter', {track: 'twitter'}, function(stream){
stream.on('data', function(tweet) {
console.log(tweet.text);
});
stream.on('error', function(error) {
console.log(error);
});
});
*/
/*
client.get('statuses/show/266184139324157952', 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 = 'Twiitti: @' + user + ': "' + tweet.text + '" ('+ dateTimeLocale +')';
console.log(str);
}
});
*/
/*
var getYoutubeTitle = function(query) {
var google = require('googleapis');
var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg';
var youtube = google.youtube('v3');
var params = {-
part: 'snippet, statistics',
q: 'https://www.youtube.com/watch?v=XNwKqvCZ4kU',
type: 'video',
key: apikey
};
var result = youtube.search.list(params, function(err, resp) {
if (err) {
console.log('An error occured', err);
return;
}
console.log(resp.items[0].snippet.title);
console.log(resp.items[0].snippet.channelTitle);
console.log(resp);
});
console.log(result);
//var resultString = 'Title: '+;
};
*/
//console.log('YouTube!', getYoutubeTitle());
/*
getTemperatureString().then(function(result) {
console.log(result);
});
*/
/*
var res = null;
var message= '.heita 4d6';
if (res = message.match(/^\.heita ([0-9]+)d([0-9]+)$/i)) {
console.log(getDiceString(res[1], res[2]));
}
*/
/*
var message = '.saa Tampere'; var res = null;
if (res = message.match(/^\.saa ([^0-9 ]+)$/i)) {
console.log('kaupunki', res[1]);
owmapi.getTemperature(res[1]).then(function(result) {
console.log(result);
if(result.cod === '404') {
console.log('Eioo');
} else {
var city = result.name;
var country = result.sys.country;
var temp = (result.main.temp-273.15).toFixed(1);
var sunriseDate = new Date(result.sys.sunrise*1000);
var sunsetDate = new Date(result.sys.sunset*1000);
var sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes();
var sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes();
var humidity = result.main.humidity;
var pressure = result.main.pressure;
console.log(city, temp, country, sunrise, sunset, humidity, pressure);
}
}, function(error) {
console.log('fas',error);
});
}
*/

View File

@ -14,23 +14,25 @@
"url": "https://lanxu@bitbucket.org/lanxu/kulmapaikka-ircbot" "url": "https://lanxu@bitbucket.org/lanxu/kulmapaikka-ircbot"
}, },
"dependencies": { "dependencies": {
"node-dev": "3.1.3", "chance": "=1.0.6",
"cron": "1.2.1", "cron": "1.2.1",
"file-stream-rotator": "0.1.0",
"finalhandler": "1.0.0",
"googleapis": "18.0.0",
"html-to-text": "^3.3.0",
"irc": "0.5.2",
"mkdirp": "~0.5.1", "mkdirp": "~0.5.1",
"morgan": "1.8.1",
"node-ddg-api": "0.1.4",
"node-dev": "3.1.3",
"node-uuid": "1.4.7",
"promise": "^7.0.0",
"request": "^2.55.0",
"request-promise": "^4.1.1",
"twitter": "1.7.0",
"winston": "2.3.1", "winston": "2.3.1",
"winston-daily-rotate-file": "1.4.6", "winston-daily-rotate-file": "1.4.6",
"file-stream-rotator": "0.1.0", "html-to-text": "3.3.0"
"morgan": "1.8.1",
"request-promise": "^4.1.1",
"request": "^2.55.0",
"promise": "^7.0.0",
"finalhandler": "1.0.0",
"node-uuid": "1.4.7",
"irc": "0.5.2",
"googleapis": "18.0.0",
"node-ddg-api": "0.1.4",
"twitter": "1.7.0",
"chance": "=1.0.6"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^1.2.1" "nodemon": "^1.2.1"

85
plugins/mastodon.js Normal file
View File

@ -0,0 +1,85 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
var htmlToText = require('html-to-text');
class Mastodon extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Mastodon';
}
test(input) {
var res = null;
if (res = input.match(/http(s)?:\/\/(www\.)?((.*\..*\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/)) {
return true;
}
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/@([a-zA-Z0-9_-]*)\/([0-9_-]*)))/)) {
return true;
}
return false;
}
getToot(host, statusId) {
var promise = new Promise(function(resolve, reject) {
var url = 'https://' + host + '/api/v1/statuses/' + statusId;
requestPromise(url, {}, {
'Accept': 'application/json',
}).then(function(toot) {
if(!toot.error) {
var user = toot.account.username;
var time = toot.created_at;
var dateTime = new Date(time);
var dateTimeLocale = dateTime.toLocaleDateString('fi-FI') + ' ' + dateTime.getHours() + '.' + dateTime.getMinutes();
var tootContent = toot.content;
var tootContent = htmlToText.fromString(tootContent, {
wordwrap: false,
ignoreHref: true,
preserveNewlines: false,
});
var str = '@' + user + ': "' + tootContent + '" ('+ dateTimeLocale +')';
str = str.replace(/(?:\r\n|\r|\n)/g, ' ');
resolve(str);
} else {
reject();
}
});
});
return promise;
}
result(input) {
var that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
var host = null;
var tootId = null;
var res = null;
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/)) {
host = res[5];
tootId = res[7];
}
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/@([a-zA-Z0-9_-]*)\/([0-9_-]*)))/)) {
host = res[5];
tootId = res[7];
}
if (host !== null && tootId !== null) {
that.getToot(host, tootId).then(function(toot) {
resultResolve('Töötti: ' + toot);
}, function(error) {
resultReject('Töötti: Jokin virhe tapahtui');
});
}
});
return resultPromise;
}
}
module.exports = Mastodon;