From 69460fe65974fce60d6b764a58362a9864677a78 Mon Sep 17 00:00:00 2001 From: Karl0ss Date: Tue, 25 Oct 2022 15:52:25 +0100 Subject: [PATCH] express updated and telegram fixes --- FeedFilter.js | 4 -- JDRssDownloader.js | 96 +++++++++++++++++++++++++++++++++++++++------- apiFunctions.js | 47 +++++++++++++++++++++++ config-sample.json | 2 + package.json | 5 +++ 5 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 apiFunctions.js diff --git a/FeedFilter.js b/FeedFilter.js index ce6bf61..2675ec0 100644 --- a/FeedFilter.js +++ b/FeedFilter.js @@ -3,7 +3,6 @@ const { linkAdder } = require('./JDLinkAdder'); const { getLinksFromURL } = require('./LinkGrabber') const { checkFileName } = require('./checkFileName') const { checkDownloadHistory } = require('./checkDownloadHistory') -const { telegrambot } = require('./telegramCommunication') async function filterFeed() { let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows @@ -54,9 +53,6 @@ async function filterFeed() { break } else { log.info(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') - if (TelegramBotConfig) { - telegrambot(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') - } linkAdder(download_list) } } else { diff --git a/JDRssDownloader.js b/JDRssDownloader.js index 5f180e9..d1c128f 100644 --- a/JDRssDownloader.js +++ b/JDRssDownloader.js @@ -2,21 +2,97 @@ const fs = require("fs"); const { feedUpdater } = require('./FeedUpdater') const { filterFeed } = require('./FeedFilter') const { telegrambot } = require('./telegramCommunication') +const { addNewShow, removeShow } = require('./apiFunctions') const version = require('./package.json').version; - -global.TelegramBotConfig = JSON.parse(fs.readFileSync('config.json')).TelegramBot +config = JSON.parse(fs.readFileSync('config.json')) +const express = require('express'); +const bodyParser = require('body-parser'); +const cors = require('cors'); +const path = require("path"); +const basicAuth = require('express-basic-auth') +const app = express(); +app.set("views", path.join(__dirname, "views")); +app.set("view engine", "pug"); +app.use(express.static(path.join(__dirname, "public"))); +app.use(cors()); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(bodyParser.json()); +pass = config.AdminPassword +app.use(basicAuth({ + users: { 'admin': pass }, + challenge: true, +})) +const port = config.WebUIPort; global.log = require('simple-node-logger').createSimpleLogger({ logFilePath: 'jdrssdownloader.log', timestampFormat: 'YYYY-MM-DD HH:mm:ss.SSS' }); +log.info = function() { + var args = Array.prototype.slice.call( arguments ), + entry = log.log('info', args); + process.nextTick(function() { + if (config.TelegramBot) { + telegrambot(entry.msg[0]) + } + }); +}; + +log.error = function() { + var args = Array.prototype.slice.call( arguments ), + entry = log.log('error', args); + process.nextTick(function() { + if (config.TelegramBot) { + telegrambot(entry.msg[0]) + } + }); +}; + +app.get("/", (req, res) => { + showListLength = JSON.parse(fs.readFileSync('config.json')).Shows.length + res.render("index", { title: "Home", showListLength:showListLength, version:version }); +}); + +app.get("/shows", (req, res) => { + showList = JSON.parse(fs.readFileSync('config.json')).Shows + res.render("shows", { title: "Show List", showList: showList }); +}); + +app.get("/shows/add", (req, res) => { + res.render("addshow", { title: "Add Show" }); +}); + +app.get("/shows/remove", (req, res) => { + showList = JSON.parse(fs.readFileSync('config.json')).Shows + res.render("removeshow", { title: "Remove Show", showList: showList }); +}); + +app.get("/logs", (req, res) => { + const lineReader = require("line-reader"); + const Promise = require("bluebird"); + logFile = [] + const eachLine = Promise.promisify(lineReader.eachLine); + eachLine('jdrssdownloader.log', function (line) { + logFile.push(line) + }).then(() => { + logFile = logFile.slice((logFile.length - 50), logFile.length) + res.render("logs", { title: "App Logs", logFile: logFile }); + }); +}); + +app.post('/addNewShow', (req, res) => { + addNewShow(req.body) + res.redirect("/shows"); +}); + +app.post('/removeShow', (req, res) => { + removeShow(req.body) + res.redirect("/shows"); +}); + async function main() { log.info('Running JDRssDownloader version ' + version) - - if (TelegramBotConfig) { - telegrambot('Running JDRssDownloader version ' + version) - } try { RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins @@ -24,14 +100,8 @@ async function main() { log.error('config.json file is missing.') } log.info('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') - if (TelegramBotConfig) { - telegrambot('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') - } log.info('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes') - if (TelegramBotConfig) { - telegrambot('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes') - } - + app.listen(port, () => log.info(`WebUi is listening on ${port}!`)) setInterval(await feedUpdater, RSSFeedRefreshMins * 60000); setInterval(await filterFeed, JDPostLinksMins * 60000); } diff --git a/apiFunctions.js b/apiFunctions.js new file mode 100644 index 0000000..9f3bdc4 --- /dev/null +++ b/apiFunctions.js @@ -0,0 +1,47 @@ +const fs = require('fs'); +const { config } = require('process'); + +async function addNewShow(showData) { + let config = JSON.parse(fs.readFileSync('config.json')) + let exist = false + for (let show of config.Shows) { + if (show.Name == showData.showName) { + exist = true + } + } + if (exist) { + log.error(showData.showName + ' Already exists in list and not added') + } else { + config.Shows.push({ + "Name": showData.showName, + "Quality": showData.quality + }) + try { + fs.writeFileSync('config.json', JSON.stringify(config)); + log.info(showData.showName + ' Added to the list, checking for ' + showData.quality + 'p' ) + } catch (err) { + console.error(err); + } + } +} + +async function removeShow(showData) { + let config = JSON.parse(fs.readFileSync('config.json')) + + myArray = config.Shows.filter(function (obj) { + return obj.Name !== showData.showName; + }); + + config.Shows = myArray + try { + fs.writeFileSync('config.json', JSON.stringify(config)); + log.info(showData.showName + ' Removed from tracking list.' ) + } catch (err) { + console.error(err); + } +} + +module.exports = { + addNewShow, removeShow +} + diff --git a/config-sample.json b/config-sample.json index aeef84b..e52f5db 100644 --- a/config-sample.json +++ b/config-sample.json @@ -1,6 +1,8 @@ { "JDUserName": "", "JDPassword": "", + "AdminPassword":"", + "WebUIPort": 3100, "RSSFeed": "", "RSSFeedRefreshMins": 10, "JDPostLinksMins": 180, diff --git a/package.json b/package.json index 3e1985c..d990418 100644 --- a/package.json +++ b/package.json @@ -12,9 +12,14 @@ "dependencies": { "axios": "^0.27.2", "cheerio": "^1.0.0-rc.11", + "cors": "^2.8.5", + "express": "^4.18.2", + "express-basic-auth": "^1.2.1", "jdownloader-client": "^1.0.0", + "line-reader": "^0.4.0", "lodash": "^4.17.21", "node-telegram-bot-api": "^0.59.0", + "pug": "^3.0.2", "rss-parser": "^3.12.0", "simple-node-logger": "^21.8.12" },