fixed parser and webhook

This commit is contained in:
Omer Sabic 2024-06-13 12:05:19 +02:00
parent 33aadc41d0
commit 5208119964
2 changed files with 13 additions and 10 deletions

View File

@ -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

View File

@ -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}`);
});