From 042d715bd89500d22f9b6431f940fb3905477baa Mon Sep 17 00:00:00 2001 From: MeowcaTheoRange Date: Mon, 19 Feb 2024 18:26:31 -0600 Subject: [PATCH] implement notification server and client --- .env.example | 11 ++++++++++- index.js | 42 +++++++++++++++++++++++++++++++++++++----- notification-client.js | 21 +++++++++++++++++++++ notification-server.js | 20 ++++++++++++++++++++ package.json | 4 ++++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 notification-client.js create mode 100644 notification-server.js diff --git a/.env.example b/.env.example index b5f62b4..541071a 100644 --- a/.env.example +++ b/.env.example @@ -17,4 +17,13 @@ NEXTCLOUD_INSTANCE= # next.abtmtr.link NEXTCLOUD_USERNAME= # meowcatheorange NEXTCLOUD_PASSWORD= # ccccc-ccccc-ccccc-ccccc-ccccc -NEXTCLOUD_FOLDER= # /Music/LastFMDownloader/ \ No newline at end of file +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 \ No newline at end of file diff --git a/index.js b/index.js index ee16b5c..ea148e0 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,12 @@ require('dotenv').config(); const { Readable } = require("stream"); +const { finished } = require("stream/promises"); const { GetListByKeyword } = require("youtube-search-api"); const skewered = require("skewered"); const { createClient } = require("fix-esm").require("webdav"); const path = require("path"); const { readFileSync, writeFileSync } = require("fs"); +const io = require('socket.io-client'); function pathGenerator({ url, name, channelName }) { 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 }); - fileStream.pipe(nextcloudClient.createWriteStream(fileName)); + await finished(fileStream.pipe(nextcloudClient.createWriteStream(fileName))); return fileName; } 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(); console.log(video); - if (video == null) return null; + if (video == null) return dismantle(socket); const nextcloudClient = createClient( `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); 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); 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(); \ No newline at end of file +main(); + +function dismantle(socket) { + socket.disconnect(); + return null; +} \ No newline at end of file diff --git a/notification-client.js b/notification-client.js new file mode 100644 index 0000000..b27253c --- /dev/null +++ b/notification-client.js @@ -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 + }); + }) +}); \ No newline at end of file diff --git a/notification-server.js b/notification-server.js new file mode 100644 index 0000000..c7c1db3 --- /dev/null +++ b/notification-server.js @@ -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}`); +}); \ No newline at end of file diff --git a/package.json b/package.json index 53c1cbd..63f390d 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,12 @@ "license": "ISC", "dependencies": { "dotenv": "^16.4.4", + "express": "^4.18.2", "fix-esm": "^1.0.1", + "node-notifier": "^10.0.1", "skewered": "^1.0.0", + "socket.io": "^4.7.4", + "socket.io-client": "^4.7.4", "webdav": "^5.3.2", "youtube-search-api": "^1.2.1" }