chore: minor artist import cleanup
This commit is contained in:
parent
d8a5cb696e
commit
e36be0f9d8
2 changed files with 13 additions and 10 deletions
|
@ -21,7 +21,7 @@ function sanitizeMediaString(str) {
|
||||||
export async function handler(event, context) {
|
export async function handler(event, context) {
|
||||||
const {
|
const {
|
||||||
DIRECTUS_URL,
|
DIRECTUS_URL,
|
||||||
DIRECTUS_API_TOKEN,
|
DIRECTUS_TOKEN,
|
||||||
ARTIST_IMPORT_TOKEN,
|
ARTIST_IMPORT_TOKEN,
|
||||||
ARTIST_FLOW_ID,
|
ARTIST_FLOW_ID,
|
||||||
ALBUM_FLOW_ID,
|
ALBUM_FLOW_ID,
|
||||||
|
@ -31,12 +31,11 @@ export async function handler(event, context) {
|
||||||
const requestUrl = new URL(event.rawUrl);
|
const requestUrl = new URL(event.rawUrl);
|
||||||
const providedToken = requestUrl.searchParams.get("token");
|
const providedToken = requestUrl.searchParams.get("token");
|
||||||
|
|
||||||
if (!providedToken || providedToken !== ARTIST_IMPORT_TOKEN) {
|
if (!providedToken || providedToken !== ARTIST_IMPORT_TOKEN)
|
||||||
return {
|
return {
|
||||||
statusCode: 401,
|
statusCode: 401,
|
||||||
body: "Unauthorized",
|
body: "Unauthorized",
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
async function saveToDirectus(endpoint, payload) {
|
async function saveToDirectus(endpoint, payload) {
|
||||||
try {
|
try {
|
||||||
|
@ -44,16 +43,18 @@ export async function handler(event, context) {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${DIRECTUS_API_TOKEN}`,
|
Authorization: `Bearer ${DIRECTUS_TOKEN}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
data.errors?.[0]?.message || "Failed to save to Directus",
|
data.errors?.[0]?.message || "Failed to save to Directus",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.data;
|
return data.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error saving to ${endpoint}:`, error.message);
|
console.error(`Error saving to ${endpoint}:`, error.message);
|
||||||
|
@ -67,9 +68,10 @@ export async function handler(event, context) {
|
||||||
`${DIRECTUS_URL}/items/genres?filter[name][_eq]=${encodeURIComponent(
|
`${DIRECTUS_URL}/items/genres?filter[name][_eq]=${encodeURIComponent(
|
||||||
genreName.toLowerCase(),
|
genreName.toLowerCase(),
|
||||||
)}`,
|
)}`,
|
||||||
{ headers: { Authorization: `Bearer ${DIRECTUS_API_TOKEN}` } },
|
{ headers: { Authorization: `Bearer ${DIRECTUS_TOKEN}` } },
|
||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
return data.data?.[0]?.id || null;
|
return data.data?.[0]?.id || null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching genre ID:", error.message);
|
console.error("Error fetching genre ID:", error.message);
|
||||||
|
@ -79,19 +81,18 @@ export async function handler(event, context) {
|
||||||
|
|
||||||
const artistId = requestUrl.searchParams.get("artist_id");
|
const artistId = requestUrl.searchParams.get("artist_id");
|
||||||
|
|
||||||
if (!artistId) {
|
if (!artistId)
|
||||||
return {
|
return {
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
body: "artist_id parameter is required",
|
body: "artist_id parameter is required",
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
let artistData;
|
let artistData;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const artistResponse = await fetch(
|
const artistResponse = await fetch(
|
||||||
`${DIRECTUS_URL}/flows/trigger/${ARTIST_FLOW_ID}?artist_id=${artistId}&import_token=${ARTIST_IMPORT_TOKEN}`,
|
`${DIRECTUS_URL}/flows/trigger/${ARTIST_FLOW_ID}?artist_id=${artistId}&import_token=${ARTIST_IMPORT_TOKEN}`,
|
||||||
{ headers: { Authorization: `Bearer ${DIRECTUS_API_TOKEN}` } },
|
{ headers: { Authorization: `Bearer ${DIRECTUS_TOKEN}` } },
|
||||||
);
|
);
|
||||||
const artistResult = await artistResponse.json();
|
const artistResult = await artistResponse.json();
|
||||||
artistData =
|
artistData =
|
||||||
|
@ -109,8 +110,9 @@ export async function handler(event, context) {
|
||||||
const artistName = artistData.title || "";
|
const artistName = artistData.title || "";
|
||||||
const artistKey = sanitizeMediaString(artistName);
|
const artistKey = sanitizeMediaString(artistName);
|
||||||
const countryName = artistData.Country?.[0]?.tag || "";
|
const countryName = artistData.Country?.[0]?.tag || "";
|
||||||
|
const countryNameSlug = countryName.replace(/\s+/g, "-").toLowerCase();
|
||||||
const countryIsoCode = countries.getAlpha2Code(countryName, "en") || "";
|
const countryIsoCode = countries.getAlpha2Code(countryName, "en") || "";
|
||||||
const slug = `/music/artists/${artistKey}-${countryName.toLowerCase()}`;
|
const slug = `/music/artists/${artistKey}-${countryNameSlug}`;
|
||||||
const description = artistData.summary || "";
|
const description = artistData.summary || "";
|
||||||
const mbid = artistData.Guid?.[0]?.id?.replace("mbid://", "") || "";
|
const mbid = artistData.Guid?.[0]?.id?.replace("mbid://", "") || "";
|
||||||
const genreNames = artistData.Genre?.map((g) => g.tag.toLowerCase()) || [];
|
const genreNames = artistData.Genre?.map((g) => g.tag.toLowerCase()) || [];
|
||||||
|
@ -150,7 +152,7 @@ export async function handler(event, context) {
|
||||||
try {
|
try {
|
||||||
const albumResponse = await fetch(
|
const albumResponse = await fetch(
|
||||||
`${DIRECTUS_URL}/flows/trigger/${ALBUM_FLOW_ID}?artist_id=${artistId}&import_token=${ARTIST_IMPORT_TOKEN}`,
|
`${DIRECTUS_URL}/flows/trigger/${ALBUM_FLOW_ID}?artist_id=${artistId}&import_token=${ARTIST_IMPORT_TOKEN}`,
|
||||||
{ headers: { Authorization: `Bearer ${DIRECTUS_API_TOKEN}` } },
|
{ headers: { Authorization: `Bearer ${DIRECTUS_TOKEN}` } },
|
||||||
);
|
);
|
||||||
const albumResult = await albumResponse.json();
|
const albumResult = await albumResponse.json();
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ schedule = "*/15 * * * *"
|
||||||
from = "/api/artist-import"
|
from = "/api/artist-import"
|
||||||
to = "/.netlify/functions/artist-import"
|
to = "/.netlify/functions/artist-import"
|
||||||
status = 200
|
status = 200
|
||||||
|
query = "*"
|
||||||
|
|
||||||
[[redirects]]
|
[[redirects]]
|
||||||
from = "/api/scrobble"
|
from = "/api/scrobble"
|
||||||
|
|
Reference in a new issue