chore: cleanup scrobble function

This commit is contained in:
Cory Dransfeldt 2024-11-30 18:20:10 -08:00
parent a74ea3644d
commit 43a498b52f
No known key found for this signature in database

View file

@ -115,6 +115,7 @@ export async function handler(event, context) {
} }
const contentType = event.headers["content-type"] || ""; const contentType = event.headers["content-type"] || "";
if (!contentType.includes("multipart/form-data")) if (!contentType.includes("multipart/form-data"))
return { return {
statusCode: 400, statusCode: 400,
@ -126,12 +127,12 @@ export async function handler(event, context) {
try { try {
const boundary = contentType.split("boundary=")[1]; const boundary = contentType.split("boundary=")[1];
if (!boundary) throw new Error("Missing boundary in Content-Type"); if (!boundary) throw new Error("Missing boundary in Content-Type");
const rawBody = Buffer.from(event.body, "base64").toString("utf-8"); const rawBody = Buffer.from(event.body, "base64").toString("utf-8");
const formData = parseMultipartFormData(rawBody, boundary); const formData = parseMultipartFormData(rawBody, boundary);
const payload = JSON.parse(formData.payload); const payload = JSON.parse(formData.payload);
console.log("Parsed payload:", payload);
if (payload?.event === "media.scrobble") { if (payload?.event === "media.scrobble") {
const artistName = payload["Metadata"]["grandparentTitle"]; const artistName = payload["Metadata"]["grandparentTitle"];
@ -279,7 +280,12 @@ export async function handler(event, context) {
body: JSON.stringify({ status: "success" }), body: JSON.stringify({ status: "success" }),
}; };
} catch (e) { } catch (e) {
console.error("Error occurred during request processing:", e.message, e.stack); console.error(
"Error occurred during request processing:",
e.message,
e.stack,
);
return { return {
statusCode: 500, statusCode: 500,
body: JSON.stringify({ status: "error", message: "Oops! Error." }), body: JSON.stringify({ status: "error", message: "Oops! Error." }),