Fix Google Play Music and Spotify
This commit is contained in:
parent
688ec0f2f9
commit
6c29d50f1e
21 changed files with 566 additions and 559 deletions
|
@ -2,17 +2,13 @@ import { parse } from 'url';
|
|||
import querystring from 'querystring';
|
||||
import request from 'superagent';
|
||||
import 'superagent-bluebird-promise';
|
||||
import { match as urlMatch } from './url';
|
||||
|
||||
export let id = 'itunes';
|
||||
import urlMatch from './url';
|
||||
|
||||
const apiRoot = 'https://itunes.apple.com';
|
||||
|
||||
export const match = urlMatch;
|
||||
|
||||
export function* parseUrl(url) {
|
||||
const parsed = parse(url);
|
||||
const matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
|
||||
const query = querystring.parse(parsed.query);
|
||||
|
||||
if (matches) {
|
||||
|
@ -23,20 +19,21 @@ export function* parseUrl(url) {
|
|||
id = query.i;
|
||||
}
|
||||
return yield module.exports.lookupId(id, type, matches[1] || 'us');
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
export function* lookupId(id, type, cc) {
|
||||
if (String(id).match(/^[a-z]{2}/)) {
|
||||
cc = id.substr(0, 2);
|
||||
id = id.substr(2);
|
||||
export function* lookupId(possibleId, type, countrycode) {
|
||||
let cc = countrycode;
|
||||
let id = possibleId;
|
||||
if (String(possibleId).match(/^[a-z]{2}/)) {
|
||||
cc = possibleId.substr(0, 2);
|
||||
id = possibleId.substr(2);
|
||||
}
|
||||
|
||||
let path = '/lookup?id=' + id;
|
||||
let path = `/lookup?id=${id}`;
|
||||
if (cc) {
|
||||
path = '/' + cc + path;
|
||||
path = `/${cc}${path}`;
|
||||
}
|
||||
|
||||
const response = yield request.get(apiRoot + path);
|
||||
|
@ -49,54 +46,56 @@ export function* lookupId(id, type, cc) {
|
|||
} else {
|
||||
result = result.results[0];
|
||||
|
||||
let item = {
|
||||
const item = {
|
||||
service: 'itunes',
|
||||
type: type,
|
||||
type,
|
||||
id: cc + id,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: {
|
||||
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
|
||||
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
|
||||
small: `https://match.audio/itunes/${result.artworkUrl100.replace('100x100', '200x200').replace('http://', '')}`,
|
||||
large: `https://match.audio/itunes/${result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')}`,
|
||||
},
|
||||
artist: {
|
||||
name: result.artistName
|
||||
}
|
||||
name: result.artistName,
|
||||
},
|
||||
};
|
||||
|
||||
if (type === 'track') {
|
||||
item.album = {
|
||||
name: result.collectionName
|
||||
name: result.collectionName,
|
||||
};
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function* search(data) {
|
||||
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
|
||||
let query, album, entity;
|
||||
let query;
|
||||
let album;
|
||||
let entity;
|
||||
const type = data.type;
|
||||
|
||||
if (type === 'album') {
|
||||
query = data.artist.name + ' ' + data.name;
|
||||
query = `${data.artist.name} ${data.name}`;
|
||||
album = data.name;
|
||||
entity = 'album';
|
||||
} else if (type === 'track') {
|
||||
query = data.artist.name + ' ' + data.albumName + ' ' + data.name;
|
||||
query = `${data.artist.name} ${data.albumName} ${data.name}`;
|
||||
album = data.albumName;
|
||||
entity = 'musicTrack';
|
||||
}
|
||||
|
||||
for (let market of markets) {
|
||||
const path = '/' + market + '/search?term=' + encodeURIComponent(query) + '&media=music&entity=' + entity;
|
||||
for (const market of markets) { // eslint-disable-line
|
||||
const path = `/${market}/search?term=${encodeURIComponent(query)}&media=music&entity=${entity}`;
|
||||
const response = yield request.get(apiRoot + path);
|
||||
|
||||
let result = JSON.parse(response.text);
|
||||
if (!result.results[0]) {
|
||||
const matches = album.match(/^[^\(\[]+/);
|
||||
const matches = album.match(/^[^([]+/);
|
||||
if (matches && matches[0] && matches[0] !== album) {
|
||||
const cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type === 'album') {
|
||||
|
@ -111,27 +110,30 @@ export function* search(data) {
|
|||
|
||||
const item = {
|
||||
service: 'itunes',
|
||||
type: type,
|
||||
id: 'us' + result.collectionId,
|
||||
type,
|
||||
id: `us${result.collectionId}`,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: {
|
||||
small: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '200x200').replace('http://', ''),
|
||||
large: 'https://match.audio/itunes/' + result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')
|
||||
small: `https://match.audio/itunes/${result.artworkUrl100.replace('100x100', '200x200').replace('http://', '')}`,
|
||||
large: `https://match.audio/itunes/${result.artworkUrl100.replace('100x100', '600x600').replace('http://', '')}`,
|
||||
},
|
||||
artist: {
|
||||
name: result.artistName
|
||||
}
|
||||
name: result.artistName,
|
||||
},
|
||||
};
|
||||
|
||||
if (type === 'track') {
|
||||
item.album = {
|
||||
name: result.collectionName
|
||||
name: result.collectionName,
|
||||
};
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return {service: 'itunes'};
|
||||
};
|
||||
return { service: 'itunes' };
|
||||
}
|
||||
|
||||
export const id = 'itunes';
|
||||
export const match = urlMatch;
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
|
||||
export function* match(url, type) {
|
||||
export default function match(url) {
|
||||
const parsed = parse(url);
|
||||
|
||||
if (!parsed.host.match(/itunes.apple\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
const query = querystring.parse(parsed.query);
|
||||
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
|
||||
|
||||
return !!matches[3];
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue