cleanup and refactor routes into own files

This commit is contained in:
Karl0ss 2022-10-25 17:09:29 +01:00
parent ec8501d279
commit e6a22d78c0
11 changed files with 122 additions and 101 deletions

View File

@ -8,61 +8,61 @@ async function filterFeed() {
let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows
let hevcSwitch = JSON.parse(fs.readFileSync('config.json')).OnlyHEVC let hevcSwitch = JSON.parse(fs.readFileSync('config.json')).OnlyHEVC
let feed = JSON.parse(fs.readFileSync('./feedCache.json')); let feed = JSON.parse(fs.readFileSync('./feedCache.json'));
let retry_show_cache = [] let retryShowCache = []
let urls_to_check = [] let urlsToCheck = []
for (let show of myshowlist) { for (let show of myshowlist) {
try { try {
// Find show on feed // Find show on feed
let list_filtered_for_show = feed.filter(item => item.title.includes(show.Name)) let listFilteredForShow = feed.filter(item => item.title.includes(show.Name))
if (list_filtered_for_show.length > 0) { if (listFilteredForShow.length > 0) {
for (let match of list_filtered_for_show) { for (let match of listFilteredForShow) {
// If show is found get url then return all links on that page // If show is found get url then return all links on that page
let full_link_list_from_page = await getLinksFromURL(match.link) let fullLinkListFromPage = await getLinksFromURL(match.link)
if (hevcSwitch) { if (hevcSwitch) {
// Only get urls with HEVC in name // Only get urls with HEVC in name
urls_to_check = full_link_list_from_page.filter(item => item.includes('HEVC')) urlsToCheck = fullLinkListFromPage.filter(item => item.includes('HEVC'))
if (urls_to_check.length == 0) { if (urlsToCheck.length == 0) {
// If no urls with HEVC check for H265 // If no urls with HEVC check for H265
urls_to_check = full_link_list_from_page.filter(item => item.includes('H265')) urlsToCheck = fullLinkListFromPage.filter(item => item.includes('H265'))
} }
} else { } else {
urls_to_check = full_link_list_from_page urlsToCheck = fullLinkListFromPage
} }
// Only keep urls that match show quality // Only keep urls that match show quality
let urls_with_quality_in_url = urls_to_check.filter(item => item.includes(show.Quality)) let urlsWithQualityInUrl = urlsToCheck.filter(item => item.includes(show.Quality))
// Remove any url trying to direct to a torrent site search // Remove any url trying to direct to a torrent site search
let urls_without_torrent_in_url = urls_with_quality_in_url.filter(item => !item.includes('torrent')) let urlsWithoutTorrentInUrl = urlsWithQualityInUrl.filter(item => !item.includes('torrent'))
// Remove any url that doesn't include MeGusta // Remove any url that doesn't include MeGusta
if (hevcSwitch) { if (hevcSwitch) {
pre_nitroFlare = urls_without_torrent_in_url.filter(item => item.includes('MeGusta')) preNitroflare = urlsWithoutTorrentInUrl.filter(item => item.includes('MeGusta'))
} else { } else {
pre_nitroFlare = urls_without_torrent_in_url preNitroflare = urlsWithoutTorrentInUrl
} }
// NitroFlare doesn't group with the rest of the links in JD, remove them. // NitroFlare doesn't group with the rest of the links in JD, remove them.
let remove_nitroflare = pre_nitroFlare.filter(item => !item.includes('nitro')) let removeNitroflare = preNitroflare.filter(item => !item.includes('nitro'))
// Do some stuff // Do some stuff
urlObj = checkFileName(remove_nitroflare) urlObj = checkFileName(removeNitroflare)
let download_list = urlObj.urlList let downloadList = urlObj.urlList
// Send Links to JDdownloader // Send Links to JDdownloader
if (download_list.length !== 0) { if (downloadList.length !== 0) {
if (checkDownloadHistory(urlObj)) { if (checkDownloadHistory(urlObj)) {
log.info(urlObj.fileName + ' already downloaded, skipped.') log.info(urlObj.fileName + ' already downloaded, skipped.')
break break
} else { } else {
log.tele(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') log.tele(downloadList.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.')
linkAdder(download_list) linkAdder(downloadList)
global.link_check_time = new Date(); global.linkCheckTime = new Date();
} }
} else { } else {
// No HEVC links found // No HEVC links found
log.info(download_list.length + ' links for ' + show.Name + ' have been found, will recheck next time.') log.info(downloadList.length + ' links for ' + show.Name + ' have been found, will recheck next time.')
for (let feed_item of list_filtered_for_show) { for (let feedItem of listFilteredForShow) {
retry_show_cache.push(feed_item) retryShowCache.push(feedItem)
} }
global.link_check_time = new Date(); global.linkCheckTime = new Date();
} }
} }
} else { } else {
@ -71,12 +71,12 @@ async function filterFeed() {
} }
} catch (error) { } catch (error) {
log.error('Something went wrong ' + error) log.error('Something went wrong ' + error)
global.link_check_time = new Date(); global.linkCheckTime = new Date();
} }
} }
log.info('Wiping feed cache') log.info('Wiping feed cache')
fs.writeFileSync('./feedCache.json', JSON.stringify(retry_show_cache)); fs.writeFileSync('./feedCache.json', JSON.stringify(retryShowCache));
global.link_check_time = new Date(); global.linkCheckTime = new Date();
} }
module.exports = { module.exports = {

View File

@ -20,7 +20,7 @@ async function feedUpdater() {
// Save the file // Save the file
log.info(updatedArray.length + ' items in file cache') log.info(updatedArray.length + ' items in file cache')
fs.writeFileSync('./feedCache.json', JSON.stringify(updatedArray)); fs.writeFileSync('./feedCache.json', JSON.stringify(updatedArray));
global.rss_refresh_time = new Date(); global.rssRefreshTime = new Date();
} }
module.exports = { module.exports = {

View File

@ -1,11 +1,9 @@
const fs = require("fs"); const fs = require("fs");
config = JSON.parse(fs.readFileSync('config.json'))
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 { next_rss_refresh, next_link_check } = require('./utils')
const version = require('./package.json').version;
config = JSON.parse(fs.readFileSync('config.json'))
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const cors = require('cors'); const cors = require('cors');
@ -18,15 +16,16 @@ app.use(express.static(path.join(__dirname, "public")));
app.use(cors()); app.use(cors());
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); app.use(bodyParser.json());
pass = config.AdminPassword require('./routes')(app);
app.use(basicAuth({ app.use(basicAuth({
users: { 'admin': pass }, users: { 'admin': config.AdminPassword },
challenge: true, challenge: true,
})) }))
const port = config.WebUIPort;
global.rss_refresh_time = new Date(); global.rssRefreshTime = new Date();
global.link_check_time = new Date(); global.linkCheckTime = new Date();
global.version = require('./package.json').version;
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'
@ -42,60 +41,17 @@ log.tele = function () {
}); });
}; };
app.get("/", (req, res) => {
showListLength = JSON.parse(fs.readFileSync('config.json')).Shows.length
a =
res.render("index", { title: "Home", showListLength: showListLength, version: version, rss_time: next_rss_refresh(), link_check: next_link_check() });
});
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.tele('Running JDRssDownloader version ' + version) log.tele('Running JDRssDownloader version ' + global.version)
try { try {
RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins RSSFeedRefreshMins = config.RSSFeedRefreshMins
JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins JDPostLinksMins = config.JDPostLinksMins
} catch (error) { } catch (error) {
log.error('config.json file is missing.') log.error('config.json file is missing.')
} }
log.tele('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') log.tele('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes')
log.tele('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes') log.tele('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes')
app.listen(port, () => log.info(`WebUi is listening on ${port}!`)) app.listen(config.WebUIPort, () => log.info(`WebUi is listening on ${config.WebUIPort}!`))
setInterval(await feedUpdater, RSSFeedRefreshMins * 60000); setInterval(await feedUpdater, RSSFeedRefreshMins * 60000);
setInterval(await filterFeed, JDPostLinksMins * 60000); setInterval(await filterFeed, JDPostLinksMins * 60000);
} }

View File

@ -18,7 +18,7 @@ async function addNewShow(showData) {
}) })
try { try {
fs.writeFileSync('config.json', JSON.stringify(config)); fs.writeFileSync('config.json', JSON.stringify(config));
log.info(showData.showName + ' Added to the list, checking for ' + showData.quality + 'p' ) log.info(showData.showName + ' Added to the list, checking for ' + showData.quality + 'p')
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
@ -35,7 +35,7 @@ async function removeShow(showData) {
config.Shows = myArray config.Shows = myArray
try { try {
fs.writeFileSync('config.json', JSON.stringify(config)); fs.writeFileSync('config.json', JSON.stringify(config));
log.info(showData.showName + ' Removed from tracking list.' ) log.info(showData.showName + ' Removed from tracking list.')
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }

10
routes/index.js Normal file
View File

@ -0,0 +1,10 @@
var fs = require('fs');
module.exports = function(app) {
fs.readdirSync(__dirname).forEach(function(file) {
if (file === "index.js" || file.substr(file.lastIndexOf('.') + 1) !== 'js')
return;
var name = file.substr(0, file.indexOf('.'));
require('./' + name)(app);
});
}

15
routes/logFIle.js Normal file
View File

@ -0,0 +1,15 @@
const lineReader = require("line-reader");
const Promise = require("bluebird");
module.exports = function (app) {
app.get("/logFile", (req, res) => {
logFile = []
const eachLine = Promise.promisify(lineReader.eachLine);
eachLine('jdrssdownloader.log', function (line) {
logFile.push(line)
}).then(() => {
logFile = logFile.slice((logFile.length - 30), logFile.length)
res.render("logFile", { title: "App Logs", logFile: logFile.reverse() });
});
});
}

11
routes/root.js Normal file
View File

@ -0,0 +1,11 @@
const fs = require("fs");
const { nextLinkCheck, nextRssRefresh } = require('.././utils')
module.exports = function (app) {
app.get("/", (req, res) => {
showListLength = JSON.parse(fs.readFileSync('config.json')).Shows.length
a =
res.render("index", { title: "Home", showListLength: showListLength, version: global.version, rssTime: nextRssRefresh(), linkCheck: nextLinkCheck() });
});
}

28
routes/shows.js Normal file
View File

@ -0,0 +1,28 @@
const fs = require("fs");
const { addNewShow, removeShow } = require('.././apiFunctions')
module.exports = function (app) {
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.post('/addNewShow', (req, res) => {
addNewShow(req.body)
res.redirect("/shows");
});
app.post('/removeShow', (req, res) => {
removeShow(req.body)
res.redirect("/shows");
});
}

View File

@ -2,20 +2,21 @@ const fs = require('fs');
config = JSON.parse(fs.readFileSync('config.json')) config = JSON.parse(fs.readFileSync('config.json'))
var moment = require('moment'); var moment = require('moment');
function returnUpdatedDate(date, offset) {
function next_rss_refresh() { var newDate = moment(date);
var date1 = moment(global.rss_refresh_time); newDate.add(offset, 'm');
date1.add(config.RSSFeedRefreshMins, 'm'); return new moment(newDate).format("ddd, LTS")
return new moment(date1).format("ddd, LTS")
} }
function next_link_check() { function nextRssRefresh() {
var date1 = moment(global.link_check_time); return returnUpdatedDate(global.rssRefreshTime, config.RSSFeedRefreshMins)
date1.add(config.JDPostLinksMins, 'm'); }
return new moment(date1).format("ddd, LTS")
function nextLinkCheck() {
return returnUpdatedDate(global.linkCheckTime, config.JDPostLinksMins)
} }
module.exports = { module.exports = {
next_rss_refresh, next_link_check nextRssRefresh, nextLinkCheck
} }

View File

@ -8,9 +8,9 @@ block layout-content
h3 Number of Tracked Shows h3 Number of Tracked Shows
h1 #{showListLength} h1 #{showListLength}
h3 Next RSS Refresh h3 Next RSS Refresh
h1 #{rss_time} h1 #{rssTime}
h3 Next Link Check h3 Next Link Check
h1 #{link_check} h1 #{linkCheck}
div.NavButtons div.NavButtons
a(href="/shows") a(href="/shows")
div.NavButton Show List div.NavButton Show List
@ -18,5 +18,5 @@ block layout-content
div.NavButton Add New Show div.NavButton Add New Show
a(href="/shows/remove") a(href="/shows/remove")
div.NavButton Remove Show div.NavButton Remove Show
a(href="/logs") a(href="/logFile")
div.NavButton Logs div.NavButton Logs

View File

@ -3,7 +3,7 @@ extends layout
block layout-content block layout-content
div.View div.View
h1.Banner Log File h1.Banner Log File
div.body div.Message
each val in logFile each val in logFile
li= val li= val
div.NavButtons div.NavButtons