Files
Iris/src/js/services/genius/actions.js

220 lines
6.9 KiB
JavaScript
Raw Normal View History

var coreActions = require('../core/actions')
var uiActions = require('../ui/actions')
var helpers = require('../../helpers')
export function set(data){
return {
type: 'GENIUS_SET',
data: data
}
}
/**
2018-08-15 16:52:24 +12:00
* Send an ajax request to the Genius API
*
* @param dispatch obj
* @param getState obj
2018-08-15 16:52:24 +12:00
* @param endpoint string = the url to query (ie /song/:id)
* @param method string
* @param data mixed = request payload
* @return Promise
**/
var sendRequest = (dispatch, getState, endpoint, method = 'GET', data = false) => {
2017-10-10 13:05:28 +13:00
return new Promise((resolve, reject) => {
2018-08-20 08:48:46 +12:00
if (endpoint.startsWith('https://') || endpoint.startsWith('http://')){
var url = endpoint;
} else {
var url = 'https://api.genius.com/'+endpoint+'?access_token='+getState().genius.access_token;
}
// create our ajax request config
var config = {
method: method,
2018-08-20 08:48:46 +12:00
url: url,
timeout: 30000,
crossDomain: true
}
2018-08-15 16:52:24 +12:00
// only if we've got data do we add it to the request (this prevents appending of "&false" to the URL)
if (data){
if (typeof(data) === 'string'){
config.data = data
} else {
config.data = JSON.stringify(data)
}
}
// add reference to loader queue
var loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint));
2018-08-15 16:52:24 +12:00
$.ajax(config).then(
response => {
dispatch(uiActions.stopLoading(loader_key));
if (response.meta && response.meta.status >= 200 && response.meta.status < 300 && response.response){
resolve(response.response);
} else {
reject({
config: config,
xhr: xhr,
status: status,
error: error
});
}
2018-08-15 16:52:24 +12:00
},
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
reject({
config: config,
xhr: xhr,
status: status,
error: error
});
2018-08-15 16:52:24 +12:00
}
)
});
2018-08-15 16:52:24 +12:00
}
/**
* Handle authorization process
**/
export function authorizationGranted(data){
return {
type: 'GENIUS_AUTHORIZATION_GRANTED',
data: data
}
}
export function revokeAuthorization(){
return {
type: 'GENIUS_AUTHORIZATION_REVOKED'
}
}
/**
* Get current user
**/
export function getMe(){
return (dispatch, getState) => {
sendRequest(dispatch, getState, 'account')
2018-08-15 16:52:24 +12:00
.then(
response => {
dispatch({
type: 'GENIUS_ME_LOADED',
me: response.user
2018-08-15 16:52:24 +12:00
});
},
error => {
dispatch(coreActions.handleException(
'Could not load your Genius profile',
2018-08-15 16:52:24 +12:00
error
));
}
);
}
}
/**
* Extract lyrics from a page
* We don't get the lyrics in the API, so we need to 'scrape' the HTML page instead
*
* @param uri = track uri
2018-07-04 21:10:51 +12:00
* @param path = String, the relative API path for the HTML lyrics
**/
2018-07-04 21:10:51 +12:00
export function getTrackLyrics(uri, path){
2017-10-06 15:19:15 +13:00
return (dispatch, getState) => {
2018-08-20 08:48:46 +12:00
dispatch(coreActions.trackLoaded({
2017-11-09 09:12:23 +13:00
uri: uri,
lyrics: null,
2018-07-04 21:10:51 +12:00
lyrics_path: null
2018-08-20 08:48:46 +12:00
}));
2017-10-06 15:19:15 +13:00
2018-08-20 08:48:46 +12:00
var config = {
method: 'GET',
url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http/get_lyrics',
timeout: 10000
}
2018-06-28 08:12:03 +12:00
2018-08-20 08:48:46 +12:00
$.ajax(config)
.then(
(response, status, xhr) => {
2017-10-09 16:56:09 +13:00
var html = $(response);
var lyrics = html.find('.lyrics');
if (lyrics.length > 0){
2017-10-06 15:19:15 +13:00
2017-10-09 16:56:09 +13:00
lyrics = lyrics.first();
2018-08-15 16:52:24 +12:00
lyrics.find('a').replaceWith(function(){
2017-10-09 16:56:09 +13:00
return this.innerHTML;
});
2017-10-06 15:19:15 +13:00
2017-10-09 16:56:09 +13:00
var lyrics_html = lyrics.html();
lyrics_html = lyrics_html.replace(/(\[)/g, '<span class="grey-text">[');
lyrics_html = lyrics_html.replace(/(\])/g, ']</span>');
2017-10-06 15:19:15 +13:00
2018-08-20 08:48:46 +12:00
dispatch(coreActions.trackLoaded({
2017-11-09 09:12:23 +13:00
uri: uri,
lyrics: lyrics_html,
2018-07-04 21:10:51 +12:00
lyrics_path: path
2018-08-20 08:48:46 +12:00
}));
2017-10-09 16:56:09 +13:00
}
2018-08-20 08:48:46 +12:00
2017-10-09 16:56:09 +13:00
},
2018-08-20 08:48:46 +12:00
(xhr, status, error) => {
2017-10-09 16:56:09 +13:00
dispatch(coreActions.handleException(
2018-08-20 08:48:46 +12:00
'Could not get track lyrics',
2017-10-09 16:56:09 +13:00
error
));
2017-10-06 15:19:15 +13:00
}
2017-10-09 16:56:09 +13:00
);
2017-10-06 15:19:15 +13:00
}
}
export function findTrackLyrics(track){
return (dispatch, getState) => {
var query = '';
query += track.artists[0].name+' ';
query += track.name;
query = query.toLowerCase();
query = query.replace(/\([^)]*\) */g, ''); // anything in circle-braces
query = query.replace(/\([^[]*\] */g, ''); // anything in square-braces
2018-08-20 08:48:46 +12:00
sendRequest(dispatch, getState, 'search', 'GET', 'q='+encodeURIComponent(query))
.then(
response => {
2018-08-20 08:48:46 +12:00
if (response.hits && response.hits.length > 0){
var lyrics_results = [];
2018-08-20 08:48:46 +12:00
for (var i = 0; i < response.hits.length; i++){
lyrics_results.push({
2018-08-20 08:48:46 +12:00
title: response.hits[i].result.full_title,
url: response.hits[i].result.url,
path: response.hits[i].result.path
});
}
2018-08-20 08:48:46 +12:00
dispatch(coreActions.trackLoaded({
2017-11-09 09:12:23 +13:00
uri: track.uri,
lyrics_results: lyrics_results
2018-08-20 08:48:46 +12:00
}));
2018-07-04 21:10:51 +12:00
// Immediately go and get the first result's lyrics
var lyrics_result = lyrics_results[0];
dispatch(getTrackLyrics(track.uri, lyrics_result.path));
}
},
error => {
dispatch(coreActions.handleException(
2018-08-20 08:48:46 +12:00
'Could not search for track lyrics',
error
));
}
);
}
}