3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +01:00

Merge pull request #669 from Scritches/master

Bunch of spotify, youtube, and lastfm improvements
This commit is contained in:
Luke Slater 2018-03-25 03:28:22 +01:00 committed by GitHub
commit f5c9a275a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 30 deletions

View File

@ -280,22 +280,41 @@ var lastfm = function(dbot) {
'artist': track.artist['#text'] 'artist': track.artist['#text']
}); });
} }
dbot.api.youtube.search(term, function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) { async.parallel({
var link = body.items[0].id.videoId youtube: function(cb) {
if(link) { dbot.api.youtube.search(term, function(body) {
output += ' - http://youtu.be/' + link; if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
} var link = body.items[0].id.videoId
if(link) {
cb(null,"https://youtu.be/" + link);
} else {
cb(null, undefined);
}
}
});
},
spotify: function(cb) {
dbot.api.spotify.spotifySearch(term, function(body, url, uri) {
if(body) {
cb(null, { url:url, uri:uri });
} else {
cb(null, undefined);
}
});
} }
}, function(err, results) {
/* dbot.api.spotify.spotifySearch(term, function(body, t) { if (results.youtube || results.spotify) output += " - "
if(body) {
output += ' - ' + t; if (results.youtube) output += results.youtube;
} if (results.spotify) {
if (results.youtube) output += " | ";
});*/ output += results.spotify.url + " - " + results.spotify.uri;
}
event.reply(output); event.reply(output);
}); });
} else { } else {
if(err == 'no_user') { if(err == 'no_user') {
event.reply('Unknown LastFM user.'); event.reply('Unknown LastFM user.');

View File

@ -6,6 +6,10 @@ Various Spotify functionality.
This module posts information on Spotify links, as well as providing Spotify This module posts information on Spotify links, as well as providing Spotify
search functionality. search functionality.
### config.json
Edit the "api_key_clientid" setting with your Spotify API client ID. Edit the
"api_key_clientsecret" setting with your Spotify API client secret.
## Commands ## Commands
### ~spotify [query] ### ~spotify [query]

View File

@ -1,5 +1,7 @@
{ {
"dependencies": [ "link" ], "dependencies": [ "link" ],
"ignorable": true, "ignorable": true,
"outputPrefix": "\u00039spotify\u000f" "outputPrefix": "\u00039spotify\u000f",
"api_key_clientid": "blah",
"api_key_clientsecret": "blah"
} }

View File

@ -21,16 +21,15 @@ var spotify = function(dbot) {
this.spotifyText = '\u00039spotify\u000f'; this.spotifyText = '\u00039spotify\u000f';
this.spotifyAuthUrl = 'https://accounts.spotify.com/api/token'; this.spotifyAuthUrl = 'https://accounts.spotify.com/api/token';
// ClientID and ClientSecret come from the spotify developer center; you will need to supply your own. this.auth = false;
this.spotifyClientID = 'e2491c50879a4d7f900dcefcc74b7c90';
this.spotifyClientSecret = 'b29da299612e4e659099ab3367ffa3f4';
this.spotifyAuth = new Buffer(this.spotifyClientID + ":" + this.spotifyClientSecret).toString("base64");
this.authenticate = function(callback) { this.authenticate = function(callback) {
this.auth = this.auth || new Buffer(this.config.api_key_clientid + ":" + this.config.api_key_clientsecret).toString("base64");
request({ request({
url: this.spotifyAuthUrl, url: this.spotifyAuthUrl,
method: "POST", method: "POST",
headers: { Authorization: "Basic " + this.spotifyAuth }, headers: { Authorization: "Basic " + this.auth },
form: { grant_type: "client_credentials" } form: { grant_type: "client_credentials" }
}, function(error, response, body) { }, function(error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
@ -83,11 +82,9 @@ var spotify = function(dbot) {
}, function(error, response, body) { }, function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) { if(_.has(body, 'tracks') && body.tracks.items[0] && _.has(body.tracks.items[0], 'href')) {
var t = body.tracks.items[0].href; var url = body.tracks.items[0].href;
///*t = t.replace(/:/g, '/'); url = url.replace(/api.spotify.com\/v1\/tracks/, 'open.spotify.com/track');
t = t.replace(/api.spotify.com\/v1\/tracks/, callback(body, url, body.tracks.items[0].uri);
'open.spotify.com/track');
callback(body, t);
} else { } else {
callback(false); callback(false);
} }
@ -132,7 +129,8 @@ var spotify = function(dbot) {
function(a) { return a.name }).join(', '), function(a) { return a.name }).join(', '),
'album': body.tracks.items[0].album.name, 'album': body.tracks.items[0].album.name,
'track': body.tracks.items[0].name, 'track': body.tracks.items[0].name,
'url': t 'url': t,
'uri': body.tracks.items[0].uri
})); }));
} else { } else {
event.reply(dbot.t('not-found')); event.reply(dbot.t('not-found'));

View File

@ -4,6 +4,11 @@
"it": "[{title} di {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]", "it": "[{title} di {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]",
"de": "[{title} von {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]" "de": "[{title} von {author} — \u000312▶\u000f{plays} ({minutes}:{seconds}) (\u00039▲{likes}\u000f|\u000312{dislikes}▼\u000f)]"
}, },
"yt_playlist": {
"en": "[{title} by {author} - \u000312▶\u000f{videos} videos\u000f)]"
},
"yt_noresults": { "yt_noresults": {
"en": "No results found.", "en": "No results found.",
"it": "Nessun risultato.", "it": "Nessun risultato.",

View File

@ -10,14 +10,16 @@ var youtube = function(dbot) {
this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
this.api = { this.api = {
'search': function(term, callback) { 'search': function(term, callback, type) {
type = type || "video"
var qs = _.clone(this.params); var qs = _.clone(this.params);
request.get(this.ApiRoot + 'search', { request.get(this.ApiRoot + 'search', {
'qs': { 'qs': {
'key': this.config.api_key, 'key': this.config.api_key,
'q': term, 'q': term,
'maxResults': 1, 'maxResults': 1,
'part': "snippet" 'part': "snippet",
'type': type
}, },
'json': true 'json': true
}, function(error, response, body) { }, function(error, response, body) {
@ -58,10 +60,25 @@ var youtube = function(dbot) {
} }
return res; return res;
}.bind(this) }.bind(this),
'formatPlaylistLink': function(v) {
var res = dbot.t('yt_playlist', {
'title': v.snippet.title,
'author': v.snippet.channelTitle,
'videos': v.contentDetails.itemCount
});
if (v.id) {
res += " - https://www.youtube.com/playlist?list=" + v.id;
}
return res;
}
}; };
this.commands = { this.commands = {
// search for a youtube video
'~yt': function(event) { '~yt': function(event) {
this.api.search(event.input[1], function(body) { this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) { if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
@ -81,10 +98,34 @@ var youtube = function(dbot) {
} else { } else {
event.reply(dbot.t('yt_noresults')); event.reply(dbot.t('yt_noresults'));
} }
}.bind(this)); }.bind(this), "video");
},
// search for a youtube playlist
'~ytpl': function(event) {
this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
request.get(this.ApiRoot + 'playlists' , {
'qs': {
'key': this.config.api_key,
'id': body.items[0].id.playlistId,
'maxResults': 1,
'part': "snippet,contentDetails"
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
event.reply(this.internalAPI.formatPlaylistLink(body.items[0]));
}
}.bind(this));
} else {
event.reply(dbot.t('yt_noresults'));
}
}.bind(this), "playlist");
} }
}; };
this.commands['~yt'].regex = [/^yt (.+)$/, 2]; this.commands['~yt'].regex = [/^yt (.+)$/, 2];
this.commands['~ytpl'].regex = [/^ytpl (.+)$/, 2];
this.onLoad = function() { this.onLoad = function() {
dbot.api.link.addHandler(this.name, this.LinkRegex, dbot.api.link.addHandler(this.name, this.LinkRegex,