Fix Google Play Music and Spotify

This commit is contained in:
Jonathan Cremin 2017-07-20 14:31:07 +01:00
parent 688ec0f2f9
commit 6c29d50f1e
21 changed files with 566 additions and 559 deletions

View file

@ -1,21 +1,143 @@
import { parse } from 'url';
import bluebird from 'bluebird';
import PlayMusic from 'playmusic';
import { match as urlMatch } from './url';
import debuglog from 'debug';
import urlMatch from './url';
const debug = debuglog('match.audio');
const pm = bluebird.promisifyAll(new PlayMusic());
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.');
debug('GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Google Play Music.');
}
let ready = pm.initAsync({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}).catch(function(err) {
console.log(err);
});
const ready = pm.initAsync({
email: process.env.GOOGLE_EMAIL,
password: process.env.GOOGLE_PASSWORD })
.catch((err) => {
debug(err);
});
export const match = urlMatch;
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,
},
};
}
return { service: 'google' };
}
function exactMatch(needle, haystack, type) {
// try to find exact match
return haystack.find((entry) => {
if (!entry[type]) {
return false;
}
const title = entry[type].title;
if (title === needle) {
return entry;
}
return false;
});
}
function looseMatch(needle, haystack, type) {
// try to find exact match
return haystack.find((entry) => {
if (!entry[type]) {
return false;
}
const name = entry[type].title || entry[type].name;
if (name.indexOf(needle) >= 0) {
return entry[type];
}
return false;
});
}
export function* search(data, original = {}) {
yield ready;
let query;
let 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.albumName} ${data.name}`;
album = data.albumName;
}
const result = yield pm.searchAsync(query, 5);
if (!result.entries) {
const matches = album.match(/^[^([]+/);
if (matches && matches[0]) {
const cleanedData = JSON.parse(JSON.stringify(data));
if (type === 'album') {
cleanedData.name = data.name.match(/^[^([]+/)[0].trim();
} else if (type === 'track') {
cleanedData.albumName = data.albumName.match(/^[^([]+/)[0].trim();
cleanedData.name = data.name.match(/^[^([]+/)[0].trim();
}
return yield search(cleanedData, data);
}
return { service: 'google' };
}
const name = original.name || data.name;
let match = exactMatch(name, result.entries, data.type);
if (!match) {
match = looseMatch(name, result.entries, data.type);
}
if (!match) {
return { service: 'google' };
}
if (type === 'album') {
return yield lookupId(match.album.albumId, type);
} else if (type === 'track') {
return yield lookupId(match.track.storeId, type);
}
return { service: 'google' };
}
export function* parseUrl(url) {
yield ready;
@ -34,131 +156,16 @@ export function* parseUrl(url) {
}
if (id.length > 0) {
return {id: id, type: type};
} else {
return yield search({type: type, name: album, artist: {name: artist}});
return { id, type };
}
} else if(path) {
return yield search({ 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, original={}) {
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.albumName + ' ' + data.name;
album = data.albumName;
}
let result = yield pm.searchAsync(query, 5)
if (!result.entries) {
const matches = album.match(/^[^\(\[]+/);
if (matches && matches[0]) {
const cleanedData = JSON.parse(JSON.stringify(data));
if (type === 'album') {
cleanedData.name = data.name.match(/^[^\(\[]+/)[0].trim();
} else if (type === 'track') {
cleanedData.albumName = data.albumName.match(/^[^\(\[]+/)[0].trim();
cleanedData.name = data.name.match(/^[^\(\[]+/)[0].trim();
}
return yield search(cleanedData, data);
} else {
return {service: 'google'};
}
}
const name = original.name || data.name;
let match;
if (!(match = exactMatch(name, result.entries, data.type))) {
match = looseMatch(name, result.entries, data.type);
}
if (!match) {
return {service: 'google'};
} else {
if (type === 'album') {
return yield lookupId(match.album.albumId, type);
} else if (type === 'track') {
return yield lookupId(match.track.storeId, type);
}
}
};
function exactMatch(needle, haystack, type) {
// try to find exact match
return haystack.find(function(entry) {
if (!entry[type]) {
return false;
}
entry = entry[type];
if (entry.title === needle) {
return entry;
}
});
}
function looseMatch(needle, haystack, type) {
// try to find exact match
return haystack.find(function(entry) {
if (!entry[type]) {
return false;
}
const name = entry[type].title || entry[type].name;
if (name.indexOf(needle) >= 0) {
return entry[type];
}
});
}
export const match = urlMatch;
export const id = 'google';

View file

@ -1,7 +1,7 @@
import { parse } from 'url';
export function* match(url) {
var parsed = parse(url.replace(/\+/g, "%20"));
export default function match(url) {
const parsed = parse(url.replace(/\+/g, '%20'));
if (!parsed.host.match(/play\.google\.com$/)) {
return false;
}
@ -10,7 +10,7 @@ export function* match(url) {
const hash = parsed.hash;
if (hash) {
const parts = hash.split("/");
const parts = hash.split('/');
const id = parts[2];
const artist = parts[3];
@ -19,11 +19,11 @@ export function* match(url) {
} else if (artist.length > 0) {
return true;
}
} else if(path) {
} else if (path) {
const matches = path.match(/\/music\/m\/([\w]+)/);
if (matches[1]) {
return true
return true;
}
}
return false
};
return false;
}