diff --git a/src/index.js b/src/index.js index dae0b55..eaef18e 100644 --- a/src/index.js +++ b/src/index.js @@ -58,10 +58,14 @@ export const main = async () => { callbackUri: `${env.PUBLIC_API_URL}/auth/google/callback` }); - server.addContentTypeParser(['text/xml', 'application/xml', 'application/atom+xml'], function (request, payload, done) { - xml2js.parseString(payload, function (err, body) { - done(err, body) - }) + server.addContentTypeParser(['text/xml', 'application/xml', 'application/atom+xml'], { parseAs: 'string' }, async (request, payload, done) => { + try { + let parsed = await xml2js.parseStringPromise(payload); + done(null, parsed) + } catch(e) { + console.log(e); + done(e, undefined) + } }) // Routes diff --git a/src/routes/webhook.js b/src/routes/webhook.js index f96e55e..b7f92d4 100644 --- a/src/routes/webhook.js +++ b/src/routes/webhook.js @@ -37,17 +37,16 @@ export const webhookRoutes = (fastify, _, done) => { }); fastify.post("/youtube", async (req, reply) => { - const { headers, payload } = req; + const { headers, body } = req; const contentType = headers['content-type']; - + console.log(JSON.stringify(body.feed.contry)) // Check if the content type is 'application/atom+xml' if (contentType === 'application/atom+xml') { // Parse the XML payload - const { entries } = payload; - + const { feed } = body; // Example processing: log the video IDs of new videos - entries.forEach(entry => { - const videoId = entry.find('yt:videoId').value; + feed.entry.forEach(entry => { + const videoId = entry["yt:videoId"][0]; console.log(`New video uploaded: ${videoId}`); });