From b51a38325f95108e79dce470df84b93f507a3d3b Mon Sep 17 00:00:00 2001
From: unknown <89595418+unknownsrc@users.noreply.github.com>
Date: Sat, 25 Mar 2023 14:13:52 +0100
Subject: [PATCH] adding blacklist feature
---
controller/websocket.js | 17 ++++++++++++++++-
index.js | 2 --
utils/auto.js | 5 +++++
utils/redis.js | 7 ++++++-
4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/controller/websocket.js b/controller/websocket.js
index 703eea5..3544202 100644
--- a/controller/websocket.js
+++ b/controller/websocket.js
@@ -24,7 +24,12 @@ exports.save = async (ws, req) => {
if (await redis.get(id)) {
ws.send('DATA - Someone is already downloading this video...')
- ws.close()
+ return ws.close()
+ }
+
+ if (await redis.get(`blacklist:${id}`)) {
+ ws.send('DATA - You can\'t download that. The video is blacklisted.')
+ return ws.close()
}
const already = await prisma.videos.findFirst({
@@ -138,6 +143,11 @@ exports.playlist = async (ws, req) => {
continue
}
+ if (await redis.get(`blacklist:${id}`)) {
+ ws.send(`DATA - ${video.title} is blacklisted from downloading, skipping`)
+ continue
+ }
+
ws.send(`INFO - Downloading ${video.title}
`)
await redis.set(id, 'downloading')
@@ -220,6 +230,11 @@ exports.channel = async (ws, req) => {
continue
}
+ if (await redis.get(`blacklist:${id}`)) {
+ ws.send(`DATA - ${video.title} is blacklisted from downloading, skipping`)
+ continue
+ }
+
ws.send(`INFO - Downloading ${video.title}
`)
await redis.set(id, 'downloading')
diff --git a/index.js b/index.js
index 059b85c..fb5c9b3 100644
--- a/index.js
+++ b/index.js
@@ -4,7 +4,6 @@ const express = require('express')
const cors = require('cors')
const logger = require('./utils/logger.js')
-const redis = require('./utils/redis.js')
const auto = require('./utils/auto.js')
const latestController = require('./controller/latest.js')
@@ -35,7 +34,6 @@ app.ws('/saveplaylist', websocketController.playlist)
app.ws('/savechannel', websocketController.channel)
app.get('/autodownload', websocketController.addAutodownload)
-redis.flushdb()
auto.handleCheck()
// setInterval(() => {
// auto.handleCheck()
diff --git a/utils/auto.js b/utils/auto.js
index 0442e70..609ce61 100644
--- a/utils/auto.js
+++ b/utils/auto.js
@@ -47,6 +47,11 @@ async function handleDownload(channelId) {
return
}
+ if (await redis.get(`blacklist:${id}`)) {
+ logger.info({ message: `${video.title} is blacklisted from downloading, ${id}` })
+ return
+ }
+
if (video.duration > 5400) {
logger.info({ message: `${video.title} is longer than 1h30m, ${id}` })
return
diff --git a/utils/redis.js b/utils/redis.js
index 5334238..8f83c6e 100644
--- a/utils/redis.js
+++ b/utils/redis.js
@@ -7,8 +7,13 @@ const redis = new Redis({
password: process.env.REDIS_PASS,
});
-redis.on('ready', function () {
+redis.on('ready', async function () {
logger.info({ message: 'Connected to redis!' })
+
+ const keys = await redis.keys('*')
+ const filteredKeys = keys.filter(key => !key.startsWith('blacklist:'))
+ await redis.del(filteredKeys)
+
});
module.exports = redis
\ No newline at end of file