combine.fm/lib/services/itunes/index.js

169 lines
4.4 KiB
JavaScript
Raw Normal View History

2025-05-20 13:18:38 +01:00
import { parse } from "url";
import querystring from "querystring";
import request from "superagent";
import urlMatch from "./url.js";
2014-12-04 21:11:55 +00:00
2025-05-20 13:18:38 +01:00
const apiRoot = "https://itunes.apple.com";
2014-12-04 21:11:55 +00:00
2018-04-14 00:18:48 +01:00
export async function parseUrl(url) {
2015-08-20 23:22:57 +01:00
const parsed = parse(url);
2025-05-20 13:18:38 +01:00
const matches = parsed.path.match(
/[/]?([/]?[a-z]{2}?)?[/]+(song|album)[/]+([^/]+)[/]+([^?]+)/
);
2015-08-20 23:22:57 +01:00
const query = querystring.parse(parsed.query);
2025-05-20 13:18:38 +01:00
let itunesId = matches[4];
2017-10-23 22:47:14 +01:00
if (matches) {
2025-05-20 13:18:38 +01:00
let type = "album";
2017-10-23 22:47:14 +01:00
if (matches[3].match(/^id/)) {
itunesId = matches[3].substr(2);
if (query.i) {
2025-05-20 13:18:38 +01:00
type = "track";
2017-10-23 22:47:14 +01:00
itunesId = query.i;
}
}
2017-10-23 22:47:14 +01:00
2025-05-20 13:18:38 +01:00
return await lookupId(itunesId, type, matches[1] || "us");
}
2017-07-20 14:31:07 +01:00
throw new Error();
}
2018-04-14 00:18:48 +01:00
export async function lookupId(possibleId, type, countrycode) {
2017-07-20 14:31:07 +01:00
let cc = countrycode;
let id = possibleId;
if (String(possibleId).match(/^[a-z]{2}/)) {
cc = possibleId.substr(0, 2);
id = possibleId.substr(2);
2014-12-06 21:12:52 +00:00
}
2017-10-24 19:45:41 +01:00
2017-07-20 14:31:07 +01:00
let path = `/lookup?id=${id}`;
2014-12-06 21:12:52 +00:00
if (cc) {
2017-07-20 14:31:07 +01:00
path = `/${cc}${path}`;
2014-12-06 21:12:52 +00:00
}
2017-10-24 19:45:41 +01:00
2019-03-11 23:26:54 +00:00
try {
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
2015-06-03 21:45:54 -07:00
2025-05-20 13:18:38 +01:00
if (
!result.results ||
result.resultCount === 0 ||
!result.results[0].collectionId
) {
2019-03-11 23:26:54 +00:00
throw new Error();
} else {
result = result.results[0];
2014-12-04 21:53:50 +00:00
2019-03-11 23:26:54 +00:00
const item = {
2025-05-20 13:18:38 +01:00
service: "itunes",
2019-03-11 23:26:54 +00:00
type,
id: cc + id,
name: result.trackName ? result.trackName : result.collectionName,
streamUrl: null,
purchaseUrl: result.collectionViewUrl,
artwork: {
2025-05-20 13:18:38 +01:00
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://")}`
2019-03-11 23:26:54 +00:00
},
artist: {
2025-05-20 13:18:38 +01:00
name: result.artistName
}
2015-06-03 21:45:54 -07:00
};
2025-05-20 13:18:38 +01:00
if (type === "track") {
2019-03-11 23:26:54 +00:00
item.album = {
2025-05-20 13:18:38 +01:00
name: result.collectionName
2019-03-11 23:26:54 +00:00
};
}
return item;
}
2025-05-20 13:18:38 +01:00
} catch (e) {
const error = new Error("Not Found");
2019-03-11 23:26:54 +00:00
error.status = 404;
return Promise.reject(error);
2015-06-03 21:45:54 -07:00
}
2017-07-20 14:31:07 +01:00
}
2014-12-04 21:11:55 +00:00
2018-04-14 00:18:48 +01:00
export async function search(data) {
2025-05-20 13:18:38 +01:00
const markets = ["us", "gb", "jp", "br", "de", "es"];
2017-07-20 14:31:07 +01:00
let query;
let album;
let entity;
2015-08-20 23:22:57 +01:00
const type = data.type;
2014-12-04 21:11:55 +00:00
2025-05-20 13:18:38 +01:00
if (type === "album") {
2017-07-20 14:31:07 +01:00
query = `${data.artist.name} ${data.name}`;
album = data.name;
2025-05-20 13:18:38 +01:00
entity = "album";
} else if (type === "track") {
2017-07-20 14:31:07 +01:00
query = `${data.artist.name} ${data.albumName} ${data.name}`;
album = data.albumName;
2025-05-20 13:18:38 +01:00
entity = "musicTrack";
2014-12-04 21:11:55 +00:00
}
2025-05-20 13:18:38 +01:00
for (const market of markets) {
// eslint-disable-line
const path = `/${market}/search?term=${encodeURIComponent(
query
)}&media=music&entity=${entity}`;
2018-04-14 00:18:48 +01:00
const response = await request.get(apiRoot + path);
let result = JSON.parse(response.text);
if (!result.results[0]) {
2017-07-20 14:31:07 +01:00
const matches = album.match(/^[^([]+/);
if (matches && matches[0] && matches[0] !== album) {
const cleanedData = JSON.parse(JSON.stringify(data));
2025-05-20 13:18:38 +01:00
if (type === "album") {
cleanedData.name = matches[0].trim();
2025-05-20 13:18:38 +01:00
} else if (type === "track") {
cleanedData.albumName = matches[0].trim();
}
2018-04-14 00:18:48 +01:00
return await search(cleanedData);
}
2014-12-04 21:11:55 +00:00
} else {
result = result.results[0];
const item = {
2025-05-20 13:18:38 +01:00
service: "itunes",
2017-07-20 14:31:07 +01:00
type,
id: `us${result.collectionId}`,
name: result.trackName ? result.trackName : result.collectionName,
2017-10-23 19:21:19 +01:00
streamUrl: result.collectionViewUrl,
purchaseUrl: result.collectionViewUrl,
artwork: {
2025-05-20 13:18:38 +01:00
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: {
2025-05-20 13:18:38 +01:00
name: result.artistName
}
};
2015-06-03 21:45:54 -07:00
2025-05-20 13:18:38 +01:00
if (type === "track") {
item.album = {
2025-05-20 13:18:38 +01:00
name: result.collectionName
};
2014-12-04 21:53:50 +00:00
}
return item;
2014-12-04 21:11:55 +00:00
}
2015-06-03 21:45:54 -07:00
}
2025-05-20 13:18:38 +01:00
return { service: "itunes" };
2017-07-20 14:31:07 +01:00
}
2025-05-20 13:18:38 +01:00
export const id = "itunes";
2017-07-20 14:31:07 +01:00
export const match = urlMatch;