Added twitter url support

This commit is contained in:
lanxu 2015-10-02 20:14:31 +03:00
parent ce96232b11
commit 89a5df9e2b
10 changed files with 144 additions and 11 deletions

18
.brackets.json Executable file
View File

@ -0,0 +1,18 @@
{
"language": {
"javascript": {
"linting.prefer": "JSHint",
"linting.usePreferredOnly": true
}
},
"jwolfe.file-tree-exclude.list": [
"node_modules",
"bower_components",
".git",
"dist",
"vendor",
"deploy",
".hg",
"build"
]
}

3
.jshintrc Executable file
View File

@ -0,0 +1,3 @@
{
"predef": ["angular", "define", "require", "requirejs", "module", "Phaser", "console"]
}

19
app.js
View File

@ -2,11 +2,8 @@
var irc = require('irc');
var fs = require('fs');
var winston = require('winston');
var owmapi = require('./owmapi')();
var path = require('path');
var googletin = require('./googleapi.js')();
var iconCodeToText = {
'01d': 'selkeää',
'02d': 'puolipilvistä',
@ -31,10 +28,13 @@ var iconCodeToText = {
var config = require('./config');
// Load plugins
var getTemperatureString = require('./plugins/getWeather')();
var googletin = require('./googleapi.js')(config);
var owmapi = require('./owmapi')(config);
var getTemperatureString = require('./plugins/getWeather')(config);
var getDnDCharacter = require('./plugins/getCharacter')();
var getDiceString = require('./plugins/getDice')();
var getUrlString = require('./plugins/getUrl')();
var getTweet = require('./plugins/getTweet')(config);
// Configure logger
// create a write stream (in append mode)
@ -176,12 +176,19 @@ var handleMessage = function(from, to, message) {
console.log('error');
client.say(to, 'Youtube: jokin meni pieleen...');
});
} else if (res = message.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) {
var status = res[6];
getTweet(status).then(function(tweet) {
client.say(to, 'Twiitti: ' + tweet);
});
} else if (res = message.match(/http(s)?:\/\/([^ ]*)/i)) {
var url = res[0];
// Not liked. Disabled for now
/*var url = res[0];
console.log('Get url: ' + url);
getUrlString(url).then(function(str) {
client.say(to, 'Otsikko: ' + str);
});
});*/
} else {
// Do nothing? :D
}

View File

@ -6,9 +6,59 @@ var regExp = /http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\
var res = text.match(regExp);
console.log(res[5]);
console.log(res[7]);
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');

View File

@ -41,6 +41,10 @@ var config = {
user: /Palin$/,
host: /(.*)\.elisa-mobile\.fi$/
},
'palinKoti': {
user: /Palin$/,
host: /(.*)\.elisa-laajakaista\.fi$/
},
'rootmer': {
user: /ror$/,
host: /(.*)\.inet\.fi$/
@ -63,5 +67,17 @@ var config = {
Karinainen: 654059,
Lappeenranta: 648900,
Helsinki: 658225
},
keys: {
twitter: {
consumerKey: '',
consumerSecret: ''
},
google: {
apiKey: ''
},
owm: {
apiKey: ''
}
}
};

View File

@ -1,7 +1,6 @@
var request = require('request');
var Promise = require('promise');
var google = require('googleapis');
var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg';
var youtube = google.youtube('v3');
@ -24,6 +23,7 @@ var requestPromise = function (url, urlArgs) {
module.exports = function(config) {
'use strict';
var apikey = config.keys.google.apiKey;
return {
getYoutubeVideoById: function(videoId) {
var promise = new Promise(function(resolve, reject) {

View File

@ -27,6 +27,8 @@ var requestPromise = function(url, urlArgs) {
*/
module.exports = function(config) {
'use strict';
var apikey = config.keys.owm.apiKey;
return {
/** Get temperatures from Pena API (Aura) */
getTemperature: function(city, code) {
@ -35,7 +37,7 @@ module.exports = function(config) {
var urlArgs = {
'q': city,
'lang': language,
'APPID': '4e3b7bbd3da07052c4fed6d342e48707'
'APPID': apikey
};
var url = 'http://api.openweathermap.org/data/2.5/weather';
var promise = requestPromise(url, urlArgs);
@ -58,7 +60,7 @@ module.exports = function(config) {
var urlArgs = {
'id': idList,
'lang': language,
'APPID': '4e3b7bbd3da07052c4fed6d342e48707'
'APPID': apikey
};
var url = 'http://api.openweathermap.org/data/2.5/group';
var promise = requestPromise(url, urlArgs);

View File

@ -4,6 +4,8 @@
"description": "Kulmapaikka",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/nodemon app.js",
"test": "./node_modules/.bin/nodemon appTest.js"
},
"author": "Jukka Lankinen <jukka.lankinen@gmail.com>",
"license": "ISC",
@ -24,7 +26,8 @@
"finalhandler": "latest",
"node-uuid": "latest",
"irc": "latest",
"googleapis": "latest"
"googleapis": "latest",
"twitter": "latest"
},
"devDependencies": {
"nodemon": "^1.2.1"

33
plugins/getTweet.js Normal file
View File

@ -0,0 +1,33 @@
var Promise = require('promise');
var Twitter = require('twitter');
module.exports = function(config) {
var client = new Twitter({
consumer_key: config.keys.twitter.consumerKey,
consumer_secret: config.keys.twitter.consumerSecret,
access_token_key: '',
access_token_secret: ''
});
var getTweet = function(statusId) {
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 +')';
resolve(str);
} else {
reject();
}
});
});
return promise;
};
return getTweet;
};

View File

@ -1,5 +1,4 @@
var penaapi = require('./../penaapi')();
var owmapi = require('./../owmapi')();
var Promise = require('promise');
var iconCodeToText = {
@ -24,6 +23,8 @@ var iconCodeToText = {
};
module.exports = function(config) {
var owmapi = require('./../owmapi')(config);
var getTemperatureString = function(owmCityIds) {
var promise = new Promise(function(resolve, reject) {