implement notification server and client

This commit is contained in:
MeowcaTheoRange 2024-02-19 18:26:31 -06:00
parent 8a163139df
commit 042d715bd8
5 changed files with 92 additions and 6 deletions

View file

@ -17,4 +17,13 @@ NEXTCLOUD_INSTANCE= # next.abtmtr.link
NEXTCLOUD_USERNAME= # meowcatheorange NEXTCLOUD_USERNAME= # meowcatheorange
NEXTCLOUD_PASSWORD= # ccccc-ccccc-ccccc-ccccc-ccccc NEXTCLOUD_PASSWORD= # ccccc-ccccc-ccccc-ccccc-ccccc
NEXTCLOUD_FOLDER= # /Music/LastFMDownloader/ NEXTCLOUD_FOLDER= # /Music/LastFMDownloader/
# Notifications
NOTIFICATION_SERVER_PORT= # 80
NOTIFICATION_SERVER_PASSWORD= # password123
NOTIFICATION_CLIENT_INSTANCE= # secret.abtmtr.link
NOTIFICATION_CLIENT_PORT= # 80
NOTIFICATION_CLIENT_PROTOCOL= # https

View file

@ -1,10 +1,12 @@
require('dotenv').config(); require('dotenv').config();
const { Readable } = require("stream"); const { Readable } = require("stream");
const { finished } = require("stream/promises");
const { GetListByKeyword } = require("youtube-search-api"); const { GetListByKeyword } = require("youtube-search-api");
const skewered = require("skewered"); const skewered = require("skewered");
const { createClient } = require("fix-esm").require("webdav"); const { createClient } = require("fix-esm").require("webdav");
const path = require("path"); const path = require("path");
const { readFileSync, writeFileSync } = require("fs"); const { readFileSync, writeFileSync } = require("fs");
const io = require('socket.io-client');
function pathGenerator({ url, name, channelName }) { function pathGenerator({ url, name, channelName }) {
return path.join(process.env.NEXTCLOUD_FOLDER, `${url}-${skewered(`${name} - ${channelName}`)}.mp3`) return path.join(process.env.NEXTCLOUD_FOLDER, `${url}-${skewered(`${name} - ${channelName}`)}.mp3`)
@ -118,16 +120,22 @@ async function uploadToNextcloud({ fileStream, url, name, channelName }, nextclo
}); });
const fileName = pathGenerator({ url, name, channelName }); const fileName = pathGenerator({ url, name, channelName });
fileStream.pipe(nextcloudClient.createWriteStream(fileName)); await finished(fileStream.pipe(nextcloudClient.createWriteStream(fileName)));
return fileName; return fileName;
} }
async function main() { async function main() {
const socket = io.connect(`http://localhost:${process.env.NOTIFICATION_SERVER_PORT}`, {reconnect: true});
socket.on('connect', function (s) {
console.log('Successfully connected to notification server');
});
const video = await getVideo(); const video = await getVideo();
console.log(video); console.log(video);
if (video == null) return null; if (video == null) return dismantle(socket);
const nextcloudClient = createClient( const nextcloudClient = createClient(
`https://${process.env.NEXTCLOUD_INSTANCE}/remote.php/dav/files/${process.env.NEXTCLOUD_USERNAME}/`, `https://${process.env.NEXTCLOUD_INSTANCE}/remote.php/dav/files/${process.env.NEXTCLOUD_USERNAME}/`,
@ -137,13 +145,37 @@ async function main() {
} }
); );
if (await checkNextcloud(video, nextcloudClient)) return null; if (await checkNextcloud(video, nextcloudClient)) return dismantle(socket);
socket.emit('nodeMessage', {
message: `New song found - ${video.name} from ${video.channelName}`,
password: process.env.NOTIFICATION_SERVER_PASSWORD
})
const cobalt = await downloadFromCobalt(video); const cobalt = await downloadFromCobalt(video);
console.log(cobalt); console.log(cobalt);
socket.emit('nodeMessage', {
message: `Nextcloud upload running - ${cobalt.name} from ${cobalt.channelName}`,
password: process.env.NOTIFICATION_SERVER_PASSWORD
});
const nextcloud = await uploadToNextcloud(cobalt, nextcloudClient); const nextcloud = await uploadToNextcloud(cobalt, nextcloudClient);
console.log(nextcloud); console.log(nextcloud);
return nextcloud;
socket.emit('nodeMessage', {
message: `Nextcloud upload finished - ${cobalt.name} from ${cobalt.channelName}`,
password: process.env.NOTIFICATION_SERVER_PASSWORD
})
return dismantle(socket);
} }
main(); main();
function dismantle(socket) {
socket.disconnect();
return null;
}

21
notification-client.js Normal file
View file

@ -0,0 +1,21 @@
require('dotenv').config();
const io = require('socket.io-client');
const notifier = require('node-notifier');
console.log(`${process.env.NOTIFICATION_CLIENT_PROTOCOL}://${process.env.NOTIFICATION_CLIENT_INSTANCE}:${process.env.NOTIFICATION_CLIENT_PORT}`);
const socket = io.connect(`${process.env.NOTIFICATION_CLIENT_PROTOCOL}://${process.env.NOTIFICATION_CLIENT_INSTANCE}:${process.env.NOTIFICATION_CLIENT_PORT}`, {reconnect: true});
socket.on('connect', function (s) {
notifier.notify({
title: 'LastFMDownloader',
message: 'Connected to notification server'
});
socket.on('message', (msg) => {
notifier.notify({
title: 'LastFMDownloader',
message: msg
});
})
});

20
notification-server.js Normal file
View file

@ -0,0 +1,20 @@
require('dotenv').config();
const express = require('express');
const { createServer } = require('node:http');
const { Server } = require('socket.io');
const app = express();
const server = createServer(app);
const io = new Server(server);
io.on('connection', (socket) => {
console.log('A client connected!');
socket.on('nodeMessage', (msg) => {
if (msg.password === process.env.NOTIFICATION_SERVER_PASSWORD)
io.emit('message', msg.message);
})
});
server.listen(process.env.NOTIFICATION_SERVER_PORT, () => {
console.log(`server running on port ${process.env.NOTIFICATION_SERVER_PORT}`);
});

View file

@ -11,8 +11,12 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^16.4.4", "dotenv": "^16.4.4",
"express": "^4.18.2",
"fix-esm": "^1.0.1", "fix-esm": "^1.0.1",
"node-notifier": "^10.0.1",
"skewered": "^1.0.0", "skewered": "^1.0.0",
"socket.io": "^4.7.4",
"socket.io-client": "^4.7.4",
"webdav": "^5.3.2", "webdav": "^5.3.2",
"youtube-search-api": "^1.2.1" "youtube-search-api": "^1.2.1"
} }