diff --git a/modules/link/link.js b/modules/link/link.js
index 837b3fa..ac91083 100644
--- a/modules/link/link.js
+++ b/modules/link/link.js
@@ -11,27 +11,7 @@ var link = function(dbot) {
this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
this.links = {};
this.handlers = [];
- this.fetchTitle = function(event, link) {
- var limit = 1000000,
- size = 0,
- page = request(link.replace('https', 'http'), function(error, response, body) {
- if(!error && response.statusCode == 200) {
- body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
- var title = body.valMatch(/
(.*)<\/title>/, 2);
- if(title && title.length < 140) {
- event.reply(ent.decode(title[1]).trim());
- }
- }
- });
-
- page.on('data', function(chunk) {
- size += chunk.length;
- if(size > limit) {
- page.abort();
- }
- });
- };
-
+
this.api = {
'addHandler': function(name, regex, handler) {
this.handlers.push({
@@ -39,6 +19,27 @@ var link = function(dbot) {
'regex': regex,
'callback': handler
});
+ },
+
+ 'getTitle': function(link, callback) {
+ var limit = 1000000,
+ size = 0,
+ page = request(link.replace('https', 'http'), function(error, response, body) {
+ if(!error && response.statusCode == 200) {
+ body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
+ var title = body.valMatch(/(.*)<\/title>/, 2);
+ if(title && title.length < 140) {
+ callback(ent.decode(title[1]).trim());
+ }
+ }
+ });
+
+ page.on('data', function(chunk) {
+ size += chunk.length;
+ if(size > limit) {
+ page.abort();
+ }
+ });
}
};
@@ -51,7 +52,9 @@ var link = function(dbot) {
link = urlMatches[0];
}
}
- this.fetchTitle(event, link);
+ this.fetchTitle(link, function(title) {
+ event.reply(title);
+ });
},
'~xkcd': function(event) {
@@ -119,7 +122,11 @@ var link = function(dbot) {
handlerFound = true; break;
}
}
- if(!handlerFound) this.fetchTitle(event, urlMatches[0]);
+ if(!handlerFound) {
+ this.fetchTitle(urlMatches[0], function(title) {
+ event.reply(title);
+ });
+ }
}
}
}.bind(this);
diff --git a/modules/spotify/spotify.js b/modules/spotify/spotify.js
index f309e9f..f937ff9 100644
--- a/modules/spotify/spotify.js
+++ b/modules/spotify/spotify.js
@@ -13,6 +13,8 @@ var spotify = function(dbot) {
this.spotifyRegex = /(\b(https?:\/\/open.spotify.com\/(artist|track|album)\/\w*|spotify:(artist|track|album):\w*)\b)/ig;
this.spotifyLookup = 'http://ws.spotify.com/lookup/1/.json';
this.spotifySearch = 'http://ws.spotify.com/search/1/track.json';
+ this.youtubeRegex = /^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$/
+ this.spotifyText = "\u00039spotify\u000f";
this.lookup = function(event, link) {
request({
@@ -35,28 +37,75 @@ var spotify = function(dbot) {
});
};
- var commands = {
- '~spotify': function(event) {
- var query = event.input[1];
+ this.api = {
+ 'spotifySearch': function(query, callback) {
request({
url: this.spotifySearch,
qs: {q: query},
json: true
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
- var spotify = "\u00039spotify\u000f";
if (_.has(body, 'tracks') && body.tracks[0] && _.has(body.tracks[0], 'href')) {
var t = body.tracks[0].href;
t = t.replace(/:/g, '/');
t = t.replace(/spotify/, 'http://open.spotify.com');
- event.reply(dbot.t("found", {s: spotify, artist: _.map(body.tracks[0].artists, function(a) { return a.name }).join(', '), album: body.tracks[0].album.name, track: body.tracks[0].name, url: t}));
+ callback(body, t);
} else {
- event.reply(dbot.t("not-found", {s: spotify}));
+ callback(false);
}
}
});
}
};
+
+ var commands = {
+ '~spotify': function(event) {
+ var query = event.input[1];
+ this.api.spotifySearch(query, function(body, t) {
+ if(body) {
+ event.reply(dbot.t("found", {
+ s: this.spotifyText,
+ artist: _.map(body.tracks[0].artists, function(a) {
+ return a.name }).join(', '),
+ album: body.tracks[0].album.name,
+ track: body.tracks[0].name,
+ url: t
+ }));
+ } else {
+ event.reply(dbot.t("not-found", {s: spotify}));
+ }
+ }.bind(this));
+ },
+
+ '~syt': function(event) {
+ var lastLink = dbot.modules.link.links[event.channel.name];
+ if(!_.isUndefined(event.params[1])) {
+ lastLink = event.params[1];
+ }
+
+ if(lastLink.match(this.youtubeRegex)) {
+ dbot.api.link.getTitle(lastLink, function(title) {
+ name = title.replace(' - YouTube', '');
+ this.api.spotifySearch(name, function(body, t) {
+ if(body) {
+ event.reply(dbot.t("found", {
+ s: this.spotifyText,
+ artist: _.map(body.tracks[0].artists, function(a) {
+ return a.name }).join(', '),
+ album: body.tracks[0].album.name,
+ track: body.tracks[0].name,
+ url: t
+ }));
+ } else {
+ event.reply('No results');
+ }
+ }.bind(this));
+ }.bind(this));
+ } else {
+ event.reply("That's not a YouTube link");
+ }
+ }
+ };
commands['~spotify'].regex = [/^~spotify (.*)/, 2];
this.commands = commands;