Skip to content

Commit

Permalink
Add meta-data to plugins allowing data to carry over between plugin m…
Browse files Browse the repository at this point in the history
…atches.
  • Loading branch information
evl committed Jan 4, 2012
1 parent bf6c870 commit c6be6da
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/oaz.js
Expand Up @@ -28,9 +28,10 @@ exports.start = function(config) {
client.match = function(pattern, callback) {
client.on('message', function(from, to, message) {
var matches;
var meta = {};

while ((matches = pattern.exec(message)) !== null) {
if (callback(from, to, message, matches) === false) {
if (callback(from, to, message, matches, meta) === false) {
break;
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/plugins/spotify.js
@@ -1,7 +1,7 @@
var request = require('request');

exports.configure = function(client) {
var matchSpotify = function(from, to, message, matches) {
var matchSpotify = function(from, to, message, matches, meta) {
var url = 'http://ws.spotify.com/lookup/1/.json?uri=http://' + matches[0];

request(url, function(err, response, body) {
Expand All @@ -26,6 +26,8 @@ exports.configure = function(client) {
break;
}

meta.title = title;

client.say(to, ('Spotify ' + type).bold + ': ' + title);
} catch (e) {
console.error('Error parsing Spotify response:', e.message);
Expand All @@ -38,13 +40,13 @@ exports.configure = function(client) {
client.match(/open\.spotify\.com\/[\w\/]+/, matchSpotify);

// Spotify URI
client.match(/spotify:(\w+):(\w+)/g, function(from, to, message, matches) {
client.match(/spotify:(\w+):(\w+)/g, function(from, to, message, matches, meta) {
var type = matches[1];
var id = matches[2];
var uri = 'open.spotify.com/' + type + '/'+ id;

client.say(to, from + ': That\'s probably supposed to be http://' + uri);

matchSpotify(from, to, message, [uri]);
matchSpotify(from, to, message, [uri], meta);
});
};
5 changes: 3 additions & 2 deletions lib/plugins/urldb.js
Expand Up @@ -9,12 +9,13 @@ exports.configure = function(client, config) {

var urls = db.collection('urls');

client.match(/http:\/\/\S+/g, function(from, to, message, matches) {
client.match(/http:\/\/\S+/g, function(from, to, message, matches, meta) {
var entry = {
date: new Date(),
from: from,
to: to,
url: matches[0]
url: matches[0],
meta: meta
};

urls.insert(entry, function(err) {
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/vimeo.js
@@ -1,15 +1,17 @@
var request = require('request');

exports.configure = function(client) {
client.match(/vimeo\.com\/(\d+)/g, function(from, to, message, matches) {
client.match(/vimeo\.com\/(\d+)/g, function(from, to, message, matches, meta) {
var url = 'http://vimeo.com/api/v2/video/' + matches[1] + '.json';

request(url, function(err, response, body) {
try {
var data = JSON.parse(body);

if (data) {
client.say(to, 'Vimeo'.bold + ': ' + data[0].title);
meta.title = data[0].title;

client.say(to, 'Vimeo'.bold + ': ' + meta.title);
}
} catch (e) {
console.error('Error parsing Vimeo response:', e.message);
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/wimp.js
@@ -1,7 +1,7 @@
var request = require('request');

exports.configure = function(client) {
client.match(/wimp\.no\/(artist|album|track)\/\d+/g, function(from, to, message, matches) {
client.match(/wimp\.no\/(artist|album|track)\/\d+/g, function(from, to, message, matches, meta) {
var url = 'http://' + matches[0];
var type = matches[1];

Expand All @@ -10,9 +10,9 @@ exports.configure = function(client) {
var titleMatches = body.match(/<title>(.+)\s\-\s/);

if (titleMatches) {
var title = titleMatches[0];
meta.title = titleMatches[0];

client.say(to, ('WiMP ' + type.substr(0, 1).toUpperCase() + type.substr(1)).bold + ': ' + title);
client.say(to, ('WiMP ' + type.substr(0, 1).toUpperCase() + type.substr(1)).bold + ': ' + meta.title);
}
}
});
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/youtube.js
@@ -1,7 +1,7 @@
var request = require('request');

exports.configure = function(client) {
client.match(/youtube.*v=([\w\-]+)/g, function(from, to, message, matches) {
client.match(/youtube.*v=([\w\-]+)/g, function(from, to, message, matches, meta) {
var url = 'http://gdata.youtube.com/feeds/api/videos/' + matches[1] + '?alt=json';

request(url, function(err, response, body) {
Expand All @@ -10,7 +10,9 @@ exports.configure = function(client) {
var entry = data.entry;

if (entry) {
client.say(to, 'YouTube'.bold + ': ' + entry.title.$t);
meta.title = entry.title.$t;

client.say(to, 'YouTube'.bold + ': ' + meta.title);
}
} catch (e) {
console.error('Error parsing YouTube response:', e.message);
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "oaz",
"version": "1.1.0",
"version": "1.2.0",
"description": "The infamous öaz bot",
"repository": {
"type": "git",
Expand Down

0 comments on commit c6be6da

Please sign in to comment.