fixed parser and webhook
This commit is contained in:
parent
33aadc41d0
commit
5208119964
12
src/index.js
12
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
|
||||
|
@ -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}`);
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user