Some service api fixes
This commit is contained in:
parent
2225712d5f
commit
3ea715de39
6 changed files with 425 additions and 249 deletions
|
@ -1,28 +1,30 @@
|
|||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
import request from 'superagent';
|
||||
import urlMatch from './url.js';
|
||||
import { parse } from "url";
|
||||
import querystring from "querystring";
|
||||
import request from "superagent";
|
||||
import urlMatch from "./url.js";
|
||||
|
||||
const apiRoot = 'https://itunes.apple.com';
|
||||
const apiRoot = "https://itunes.apple.com";
|
||||
|
||||
export async function parseUrl(url) {
|
||||
const parsed = parse(url);
|
||||
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
|
||||
const matches = parsed.path.match(
|
||||
/[/]?([/]?[a-z]{2}?)?[/]+(song|album)[/]+([^/]+)[/]+([^?]+)/
|
||||
);
|
||||
const query = querystring.parse(parsed.query);
|
||||
|
||||
let itunesId = matches[3];
|
||||
let itunesId = matches[4];
|
||||
|
||||
if (matches) {
|
||||
let type = 'album';
|
||||
let type = "album";
|
||||
if (matches[3].match(/^id/)) {
|
||||
itunesId = matches[3].substr(2);
|
||||
if (query.i) {
|
||||
type = 'track';
|
||||
type = "track";
|
||||
itunesId = query.i;
|
||||
}
|
||||
}
|
||||
|
||||
return await lookupId(itunesId, type, matches[1] || 'us');
|
||||
return await lookupId(itunesId, type, matches[1] || "us");
|
||||
}
|
||||
throw new Error();
|
||||
}
|
||||
|
@ -44,61 +46,74 @@ export async function lookupId(possibleId, type, countrycode) {
|
|||
const response = await request.get(apiRoot + path);
|
||||
let result = JSON.parse(response.text);
|
||||
|
||||
if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
|
||||
if (
|
||||
!result.results ||
|
||||
result.resultCount === 0 ||
|
||||
!result.results[0].collectionId
|
||||
) {
|
||||
throw new Error();
|
||||
} else {
|
||||
result = result.results[0];
|
||||
|
||||
const item = {
|
||||
service: 'itunes',
|
||||
service: "itunes",
|
||||
type,
|
||||
id: cc + id,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: {
|
||||
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
|
||||
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
|
||||
small: `${result.artworkUrl100
|
||||
.replace("100x100", "200x200")
|
||||
.replace(".mzstatic.com", ".mzstatic.com")
|
||||
.replace("http://", "https://")}`,
|
||||
large: `${result.artworkUrl100
|
||||
.replace("100x100", "600x600")
|
||||
.replace(".mzstatic.com", ".mzstatic.com")
|
||||
.replace("http://", "https://")}`
|
||||
},
|
||||
artist: {
|
||||
name: result.artistName,
|
||||
},
|
||||
name: result.artistName
|
||||
}
|
||||
};
|
||||
|
||||
if (type === 'track') {
|
||||
if (type === "track") {
|
||||
item.album = {
|
||||
name: result.collectionName,
|
||||
name: result.collectionName
|
||||
};
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
} catch(e) {
|
||||
const error = new Error('Not Found');
|
||||
} catch (e) {
|
||||
const error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function search(data) {
|
||||
const markets = ['us', 'gb', 'jp', 'br', 'de', 'es'];
|
||||
const markets = ["us", "gb", "jp", "br", "de", "es"];
|
||||
let query;
|
||||
let album;
|
||||
let entity;
|
||||
const type = data.type;
|
||||
|
||||
if (type === 'album') {
|
||||
if (type === "album") {
|
||||
query = `${data.artist.name} ${data.name}`;
|
||||
album = data.name;
|
||||
entity = 'album';
|
||||
} else if (type === 'track') {
|
||||
entity = "album";
|
||||
} else if (type === "track") {
|
||||
query = `${data.artist.name} ${data.albumName} ${data.name}`;
|
||||
album = data.albumName;
|
||||
entity = 'musicTrack';
|
||||
entity = "musicTrack";
|
||||
}
|
||||
|
||||
for (const market of markets) { // eslint-disable-line
|
||||
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 = await request.get(apiRoot + path);
|
||||
|
||||
let result = JSON.parse(response.text);
|
||||
|
@ -106,9 +121,9 @@ export async function search(data) {
|
|||
const matches = album.match(/^[^([]+/);
|
||||
if (matches && matches[0] && matches[0] !== album) {
|
||||
const cleanedData = JSON.parse(JSON.stringify(data));
|
||||
if (type === 'album') {
|
||||
if (type === "album") {
|
||||
cleanedData.name = matches[0].trim();
|
||||
} else if (type === 'track') {
|
||||
} else if (type === "track") {
|
||||
cleanedData.albumName = matches[0].trim();
|
||||
}
|
||||
return await search(cleanedData);
|
||||
|
@ -117,31 +132,37 @@ export async function search(data) {
|
|||
result = result.results[0];
|
||||
|
||||
const item = {
|
||||
service: 'itunes',
|
||||
service: "itunes",
|
||||
type,
|
||||
id: `us${result.collectionId}`,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: result.collectionViewUrl,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: {
|
||||
small: `${result.artworkUrl100.replace('100x100', '200x200').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
|
||||
large: `${result.artworkUrl100.replace('100x100', '600x600').replace('.mzstatic.com', '.mzstatic.com').replace('http://', 'https://')}`,
|
||||
small: `${result.artworkUrl100
|
||||
.replace("100x100", "200x200")
|
||||
.replace(".mzstatic.com", ".mzstatic.com")
|
||||
.replace("http://", "https://")}`,
|
||||
large: `${result.artworkUrl100
|
||||
.replace("100x100", "600x600")
|
||||
.replace(".mzstatic.com", ".mzstatic.com")
|
||||
.replace("http://", "https://")}`
|
||||
},
|
||||
artist: {
|
||||
name: result.artistName,
|
||||
},
|
||||
name: result.artistName
|
||||
}
|
||||
};
|
||||
|
||||
if (type === 'track') {
|
||||
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 id = "itunes";
|
||||
export const match = urlMatch;
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
import { parse } from 'url';
|
||||
import { parse } from "url";
|
||||
|
||||
export default function match(url) {
|
||||
const parsed = parse(url);
|
||||
|
||||
if (!parsed.host.match(/itunes.apple\.com$/)) {
|
||||
if (
|
||||
!parsed.host.match(/itunes\.apple\.com$/) &&
|
||||
!parsed.host.match(/music\.apple\.com$/)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
|
||||
const matches = parsed.path.match(
|
||||
/[/]?([/]?[a-z]{2}?)?[/]+(song|album)[/]+([^/]+)[/]+([^?]+)/
|
||||
);
|
||||
|
||||
return !!matches[3];
|
||||
return !!matches[4];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue