express updated and telegram fixes

This commit is contained in:
Karl0ss 2022-10-25 15:52:25 +01:00
parent c3ff742bd5
commit 69460fe659
5 changed files with 137 additions and 17 deletions

View File

@ -3,7 +3,6 @@ const { linkAdder } = require('./JDLinkAdder');
const { getLinksFromURL } = require('./LinkGrabber') const { getLinksFromURL } = require('./LinkGrabber')
const { checkFileName } = require('./checkFileName') const { checkFileName } = require('./checkFileName')
const { checkDownloadHistory } = require('./checkDownloadHistory') const { checkDownloadHistory } = require('./checkDownloadHistory')
const { telegrambot } = require('./telegramCommunication')
async function filterFeed() { async function filterFeed() {
let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows
@ -54,9 +53,6 @@ async function filterFeed() {
break break
} else { } else {
log.info(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') 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) linkAdder(download_list)
} }
} else { } else {

View File

@ -2,21 +2,97 @@ const fs = require("fs");
const { feedUpdater } = require('./FeedUpdater') const { feedUpdater } = require('./FeedUpdater')
const { filterFeed } = require('./FeedFilter') const { filterFeed } = require('./FeedFilter')
const { telegrambot } = require('./telegramCommunication') const { telegrambot } = require('./telegramCommunication')
const { addNewShow, removeShow } = require('./apiFunctions')
const version = require('./package.json').version; const version = require('./package.json').version;
config = JSON.parse(fs.readFileSync('config.json'))
global.TelegramBotConfig = JSON.parse(fs.readFileSync('config.json')).TelegramBot 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({ global.log = require('simple-node-logger').createSimpleLogger({
logFilePath: 'jdrssdownloader.log', logFilePath: 'jdrssdownloader.log',
timestampFormat: 'YYYY-MM-DD HH:mm:ss.SSS' 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() { async function main() {
log.info('Running JDRssDownloader version ' + version) log.info('Running JDRssDownloader version ' + version)
if (TelegramBotConfig) {
telegrambot('Running JDRssDownloader version ' + version)
}
try { try {
RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins
JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins
@ -24,14 +100,8 @@ async function main() {
log.error('config.json file is missing.') log.error('config.json file is missing.')
} }
log.info('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') 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') log.info('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes')
if (TelegramBotConfig) { app.listen(port, () => log.info(`WebUi is listening on ${port}!`))
telegrambot('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes')
}
setInterval(await feedUpdater, RSSFeedRefreshMins * 60000); setInterval(await feedUpdater, RSSFeedRefreshMins * 60000);
setInterval(await filterFeed, JDPostLinksMins * 60000); setInterval(await filterFeed, JDPostLinksMins * 60000);
} }

47
apiFunctions.js Normal file
View File

@ -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
}

View File

@ -1,6 +1,8 @@
{ {
"JDUserName": "", "JDUserName": "",
"JDPassword": "", "JDPassword": "",
"AdminPassword":"",
"WebUIPort": 3100,
"RSSFeed": "", "RSSFeed": "",
"RSSFeedRefreshMins": 10, "RSSFeedRefreshMins": 10,
"JDPostLinksMins": 180, "JDPostLinksMins": 180,

View File

@ -12,9 +12,14 @@
"dependencies": { "dependencies": {
"axios": "^0.27.2", "axios": "^0.27.2",
"cheerio": "^1.0.0-rc.11", "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", "jdownloader-client": "^1.0.0",
"line-reader": "^0.4.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"node-telegram-bot-api": "^0.59.0", "node-telegram-bot-api": "^0.59.0",
"pug": "^3.0.2",
"rss-parser": "^3.12.0", "rss-parser": "^3.12.0",
"simple-node-logger": "^21.8.12" "simple-node-logger": "^21.8.12"
}, },