Added backend Youtube feed support
This commit is contained in:
32
Modules/webSubManager.js
Normal file
32
Modules/webSubManager.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { error } from './logManager';
|
||||
|
||||
|
||||
export async function subbWebSub(callback, topic, hub) {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ 'hub.mode': 'subscribe', 'hub.topic': topic, 'hub.callback': callback, 'hub.verify': 'sync' }),
|
||||
};
|
||||
|
||||
return await fetch(hub, options)
|
||||
.then(res => {
|
||||
res.status === 204 ? true : false;
|
||||
})
|
||||
.catch(err => error(err));
|
||||
}
|
||||
|
||||
export async function unsubWebSub(callback, topic, hub) {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ 'hub.mode': 'unsubscribe', 'hub.topic': topic, 'hub.callback': callback, 'hub.verify': 'sync' }),
|
||||
};
|
||||
|
||||
return await fetch(hub, options)
|
||||
.then(res => {
|
||||
res.status === 204 ? true : false;
|
||||
})
|
||||
.catch(err => error(err));
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"port": 3000,
|
||||
"subs": [
|
||||
"Bitcoin",
|
||||
"ethereum",
|
||||
@@ -8,5 +9,12 @@
|
||||
"discord": {
|
||||
"avatar_url": "https://aostia.me/portfolio/assets/avatar.png",
|
||||
"webhook": "https://discord.com/api/webhooks/id/token"
|
||||
},
|
||||
"youtube": {
|
||||
"callback": "https://news.chromatic.moe/youtube/callback",
|
||||
"channels": [
|
||||
"UCeeFfhMcJa1kjtfZAGskOCA",
|
||||
"UCXuqSBlHAE6Xw-yeJA0Tunw"
|
||||
]
|
||||
}
|
||||
}
|
||||
33
index.js
33
index.js
@@ -1,10 +1,21 @@
|
||||
import { exec } from 'child_process';
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
const { log } = require('./Modules/logManager');
|
||||
const { get } = require('./Modules/fetchManager');
|
||||
const { createCron } = require('./Modules/cronManager');
|
||||
const { generateHtml } = require('./Modules/htmlManager');
|
||||
const { subbWebSub, unsubWebSub } = require('./Modules/webSubManager');
|
||||
|
||||
const { port, subs, youtube, discord } = require('./config.json');
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.set('trust proxy', 1);
|
||||
app.use(cors({
|
||||
origin: '*',
|
||||
}));
|
||||
|
||||
const { subs, discord } = require('./config.json');
|
||||
|
||||
async function main() {
|
||||
const posts = [];
|
||||
@@ -38,5 +49,21 @@ async function main() {
|
||||
discord.webhook ? exec(`curl -X POST -F 'payload_json={"username":"ChromaNews","avatar_url":"${discord.avatar_url}","content":"New weekly news!"}' -F 'file1=@index.html' ${discord.webhook}`) : log('No webhook found.');
|
||||
}
|
||||
|
||||
main();
|
||||
createCron('monday 0 0', main);
|
||||
for (const channel of youtube.channels) subbWebSub(youtube.callback, `https://www.youtube.com/feeds/videos.xml?channel_id=${channel}`, 'https://pubsubhubbub.appspot.com/subscribe');
|
||||
|
||||
createCron('monday 0 0', main);
|
||||
|
||||
app.get((youtube.callback).replace(/^.*\/\/[^/]+/, ''), (req, res) => {
|
||||
log('YouTube callback received.');
|
||||
res.send('ok');
|
||||
});
|
||||
|
||||
app.listen(port, async () => {
|
||||
log(`running on port ${port}`);
|
||||
});
|
||||
|
||||
process.on('SIGINT', function() {
|
||||
console.log('Stopping server...');
|
||||
for (const channel of youtube.channels) unsubWebSub(youtube.callback, `https://www.youtube.com/feeds/videos.xml?channel_id=${channel}`, 'https://pubsubhubbub.appspot.com/subscribe');
|
||||
process.exit();
|
||||
});
|
||||
@@ -15,6 +15,8 @@
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"form-data": "^4.0.0",
|
||||
"node-cron": "^3.0.3",
|
||||
"pino": "^8.19.0"
|
||||
|
||||
Reference in New Issue
Block a user