Finally finished giant ES6 refactor
This commit is contained in:
parent
c6d48cc424
commit
03e2666958
39 changed files with 553 additions and 635 deletions
|
@ -1,148 +1,135 @@
|
|||
"use strict";
|
||||
import { parse } from 'url';
|
||||
import bluebird from 'bluebird';
|
||||
import PlayMusic from 'playmusic';
|
||||
import { match as urlMatch } from './url';
|
||||
|
||||
var parse = require("url").parse;
|
||||
var Promise = require("bluebird");
|
||||
var PlayMusic = require("playmusic");
|
||||
var pm = Promise.promisifyAll(new PlayMusic());
|
||||
const pm = bluebird.promisifyAll(new PlayMusic());
|
||||
|
||||
module.exports.id = "google";
|
||||
export let id = 'google';
|
||||
|
||||
if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
|
||||
console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Google Play Music.");
|
||||
console.warn('GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Google Play Music.');
|
||||
}
|
||||
|
||||
var ready = pm.initAsync({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}).catch(function(err) {
|
||||
let ready = pm.initAsync({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}).catch(function(err) {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
module.exports.match = require("./url").match;
|
||||
export const match = urlMatch;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
return ready.then(function() {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
if (hash) {
|
||||
var parts = hash.split("/");
|
||||
var type = parts[1];
|
||||
var id = parts[2];
|
||||
var artist = decodeURIComponent(parts[3]);
|
||||
var album = decodeURIComponent(parts[4]);
|
||||
export function* parseUrl(url) {
|
||||
yield ready;
|
||||
const parsed = parse(url.replace(/\+/g, '%20'));
|
||||
const path = parsed.path;
|
||||
const hash = parsed.hash;
|
||||
if (hash) {
|
||||
const parts = hash.split('/');
|
||||
const type = parts[1];
|
||||
const id = parts[2];
|
||||
const artist = decodeURIComponent(parts[3]);
|
||||
const album = decodeURIComponent(parts[4]);
|
||||
|
||||
if (type !== "album" && type !== "track") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (id.length > 0) {
|
||||
return {id: id, type: type};
|
||||
} else {
|
||||
return module.exports.search({type: type, name: album, artist: {name: artist}});
|
||||
}
|
||||
} else if(path) {
|
||||
var matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
type = matches[1][0] === "T" ? "track" : "album";
|
||||
return module.exports.lookupId(matches[1], type);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
return ready.then(function() {
|
||||
if (type === "album") {
|
||||
return pm.getAlbumAsync(id, false).then(function(album) {
|
||||
return {
|
||||
service: "google",
|
||||
type: "album",
|
||||
id: album.albumId,
|
||||
name: album.name,
|
||||
streamUrl: "https://play.google.com/music/m/" + album.albumId + "?signup_if_needed=1",
|
||||
purchaseUrl: "https://play.google.com/store/music/album?id=" + album.albumId,
|
||||
artwork: {
|
||||
small: album.albumArtRef.replace("http:", "https:"),
|
||||
large: album.albumArtRef.replace("http:", "https:")
|
||||
},
|
||||
artist: {
|
||||
name: album.artist
|
||||
}
|
||||
};
|
||||
}, function(error) {
|
||||
throw error;
|
||||
});
|
||||
} else if (type === "track") {
|
||||
return pm.getAllAccessTrackAsync(id).then(function(track) {
|
||||
return {
|
||||
service: "google",
|
||||
type: "track",
|
||||
id: track.nid,
|
||||
name: track.title,
|
||||
streamUrl: "https://play.google.com/music/m/" + track.nid + "?signup_if_needed=1",
|
||||
purchaseUrl: "https://play.google.com/store/music/album?id=" + track.albumId,
|
||||
artwork: {
|
||||
small: track.albumArtRef[0].url.replace("http:", "https:"),
|
||||
large: track.albumArtRef[0].url.replace("http:", "https:")
|
||||
},
|
||||
album: {
|
||||
name: track.album
|
||||
},
|
||||
artist: {
|
||||
name: track.artist
|
||||
}
|
||||
};
|
||||
}, function(error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
return ready.then(function() {
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
if (type === "album") {
|
||||
query = data.artist.name + " " + data.name;
|
||||
album = data.name;
|
||||
} else if (type === "track") {
|
||||
query = data.artist.name + " " + data.album.name + " " + data.name;
|
||||
album = data.album.name;
|
||||
if (type !== 'album' && type !== 'track') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return pm.searchAsync(query, 5).then(function(result) {
|
||||
|
||||
if (!result.entries) {
|
||||
var matches = album.match(/^[^\(\[]+/);
|
||||
if (matches && matches[0] && matches[0] !== album) {
|
||||
var cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type === "album") {
|
||||
cleanedData.name = matches[0].trim();
|
||||
} else if (type === "track") {
|
||||
cleanedData.album.name = matches[0].trim();
|
||||
}
|
||||
return module.exports.search(cleanedData);
|
||||
} else {
|
||||
return {service: "google"};
|
||||
}
|
||||
}
|
||||
result = result.entries.filter(function(entry) {
|
||||
return entry[type];
|
||||
}).sort(function(a, b) { // sort by match score
|
||||
return a.score < b.score;
|
||||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
return {service: "google"};
|
||||
} else {
|
||||
var id;
|
||||
if (type === "album") {
|
||||
id = result.album.albumId;
|
||||
} else if (type === "track") {
|
||||
id = result.track.nid;
|
||||
}
|
||||
|
||||
return module.exports.lookupId(id, type);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (id.length > 0) {
|
||||
return {id: id, type: type};
|
||||
} else {
|
||||
return yield search({type: type, name: album, artist: {name: artist}});
|
||||
}
|
||||
} else if(path) {
|
||||
const matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
const type = matches[1][0] === 'T' ? 'track' : 'album';
|
||||
return yield lookupId(matches[1], type);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
export function* lookupId(id, type) {
|
||||
yield ready;
|
||||
if (type === 'album') {
|
||||
const album = yield pm.getAlbumAsync(id, false);
|
||||
return {
|
||||
service: 'google',
|
||||
type: 'album',
|
||||
id: album.albumId,
|
||||
name: album.name,
|
||||
streamUrl: 'https://play.google.com/music/m/' + album.albumId + '?signup_if_needed=1',
|
||||
purchaseUrl: 'https://play.google.com/store/music/album?id=' + album.albumId,
|
||||
artwork: {
|
||||
small: album.albumArtRef.replace('http:', 'https:'),
|
||||
large: album.albumArtRef.replace('http:', 'https:')
|
||||
},
|
||||
artist: {
|
||||
name: album.artist
|
||||
}
|
||||
};
|
||||
} else if (type === 'track') {
|
||||
const track = yield pm.getAllAccessTrackAsync(id);
|
||||
return {
|
||||
service: 'google',
|
||||
type: 'track',
|
||||
id: track.nid,
|
||||
name: track.title,
|
||||
streamUrl: 'https://play.google.com/music/m/' + track.nid + '?signup_if_needed=1',
|
||||
purchaseUrl: 'https://play.google.com/store/music/album?id=' + track.albumId,
|
||||
artwork: {
|
||||
small: track.albumArtRef[0].url.replace('http:', 'https:'),
|
||||
large: track.albumArtRef[0].url.replace('http:', 'https:')
|
||||
},
|
||||
album: {
|
||||
name: track.album
|
||||
},
|
||||
artist: {
|
||||
name: track.artist
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export function* search(data) {
|
||||
yield ready;
|
||||
let query, album;
|
||||
const type = data.type;
|
||||
|
||||
if (type === 'album') {
|
||||
query = data.artist.name + ' ' + data.name;
|
||||
album = data.name;
|
||||
} else if (type === 'track') {
|
||||
query = data.artist.name + ' ' + data.album.name + ' ' + data.name;
|
||||
album = data.album.name;
|
||||
}
|
||||
|
||||
let result = yield pm.searchAsync(query, 5)
|
||||
|
||||
if (!result.entries) {
|
||||
const matches = album.match(/^[^\(\[]+/);
|
||||
if (matches && matches[0] && matches[0] !== album) {
|
||||
const cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type === 'album') {
|
||||
cleanedData.name = matches[0].trim();
|
||||
} else if (type === 'track') {
|
||||
cleanedData.album.name = matches[0].trim();
|
||||
}
|
||||
return yield search(cleanedData);
|
||||
} else {
|
||||
return {service: 'google'};
|
||||
}
|
||||
}
|
||||
result = result.entries.filter(function(entry) {
|
||||
return entry[type];
|
||||
}).sort(function(a, b) { // sort by match score
|
||||
return a.score < b.score;
|
||||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
return {service: 'google'};
|
||||
} else {
|
||||
if (type === 'album') {
|
||||
return yield lookupId(result.album.albumId, type);
|
||||
} else if (type === 'track') {
|
||||
return yield lookupId(result.track.nid, type);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
import { parse } from 'url';
|
||||
|
||||
module.exports.match = function(url) {
|
||||
export function* match(url) {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
if (!parsed.host.match(/play\.google\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
const path = parsed.path;
|
||||
const hash = parsed.hash;
|
||||
|
||||
if (hash) {
|
||||
var parts = hash.split("/");
|
||||
var id = parts[2];
|
||||
var artist = parts[3];
|
||||
const parts = hash.split("/");
|
||||
const id = parts[2];
|
||||
const artist = parts[3];
|
||||
|
||||
if (id.length > 0) {
|
||||
return true;
|
||||
|
@ -21,7 +20,7 @@ module.exports.match = function(url) {
|
|||
return true;
|
||||
}
|
||||
} else if(path) {
|
||||
var matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
const matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
if (matches[1]) {
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue