mirror of
https://github.com/karl0ss/JDRssDownloader.git
synced 2025-04-27 20:03:40 +01:00
cleanup and refactor routes into own files
This commit is contained in:
parent
ec8501d279
commit
e6a22d78c0
@ -8,61 +8,61 @@ async function filterFeed() {
|
||||
let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows
|
||||
let hevcSwitch = JSON.parse(fs.readFileSync('config.json')).OnlyHEVC
|
||||
let feed = JSON.parse(fs.readFileSync('./feedCache.json'));
|
||||
let retry_show_cache = []
|
||||
let urls_to_check = []
|
||||
let retryShowCache = []
|
||||
let urlsToCheck = []
|
||||
|
||||
|
||||
for (let show of myshowlist) {
|
||||
|
||||
try {
|
||||
// Find show on feed
|
||||
let list_filtered_for_show = feed.filter(item => item.title.includes(show.Name))
|
||||
if (list_filtered_for_show.length > 0) {
|
||||
for (let match of list_filtered_for_show) {
|
||||
let listFilteredForShow = feed.filter(item => item.title.includes(show.Name))
|
||||
if (listFilteredForShow.length > 0) {
|
||||
for (let match of listFilteredForShow) {
|
||||
// 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) {
|
||||
// Only get urls with HEVC in name
|
||||
urls_to_check = full_link_list_from_page.filter(item => item.includes('HEVC'))
|
||||
if (urls_to_check.length == 0) {
|
||||
urlsToCheck = fullLinkListFromPage.filter(item => item.includes('HEVC'))
|
||||
if (urlsToCheck.length == 0) {
|
||||
// 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 {
|
||||
urls_to_check = full_link_list_from_page
|
||||
urlsToCheck = fullLinkListFromPage
|
||||
}
|
||||
// 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
|
||||
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
|
||||
if (hevcSwitch) {
|
||||
pre_nitroFlare = urls_without_torrent_in_url.filter(item => item.includes('MeGusta'))
|
||||
preNitroflare = urlsWithoutTorrentInUrl.filter(item => item.includes('MeGusta'))
|
||||
} else {
|
||||
pre_nitroFlare = urls_without_torrent_in_url
|
||||
preNitroflare = urlsWithoutTorrentInUrl
|
||||
}
|
||||
// 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
|
||||
urlObj = checkFileName(remove_nitroflare)
|
||||
let download_list = urlObj.urlList
|
||||
urlObj = checkFileName(removeNitroflare)
|
||||
let downloadList = urlObj.urlList
|
||||
// Send Links to JDdownloader
|
||||
if (download_list.length !== 0) {
|
||||
if (downloadList.length !== 0) {
|
||||
if (checkDownloadHistory(urlObj)) {
|
||||
log.info(urlObj.fileName + ' already downloaded, skipped.')
|
||||
break
|
||||
} else {
|
||||
log.tele(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.')
|
||||
linkAdder(download_list)
|
||||
global.link_check_time = new Date();
|
||||
log.tele(downloadList.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.')
|
||||
linkAdder(downloadList)
|
||||
global.linkCheckTime = new Date();
|
||||
}
|
||||
} else {
|
||||
// No HEVC links found
|
||||
log.info(download_list.length + ' links for ' + show.Name + ' have been found, will recheck next time.')
|
||||
for (let feed_item of list_filtered_for_show) {
|
||||
retry_show_cache.push(feed_item)
|
||||
log.info(downloadList.length + ' links for ' + show.Name + ' have been found, will recheck next time.')
|
||||
for (let feedItem of listFilteredForShow) {
|
||||
retryShowCache.push(feedItem)
|
||||
}
|
||||
global.link_check_time = new Date();
|
||||
global.linkCheckTime = new Date();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -71,12 +71,12 @@ async function filterFeed() {
|
||||
}
|
||||
} catch (error) {
|
||||
log.error('Something went wrong ' + error)
|
||||
global.link_check_time = new Date();
|
||||
global.linkCheckTime = new Date();
|
||||
}
|
||||
}
|
||||
log.info('Wiping feed cache')
|
||||
fs.writeFileSync('./feedCache.json', JSON.stringify(retry_show_cache));
|
||||
global.link_check_time = new Date();
|
||||
fs.writeFileSync('./feedCache.json', JSON.stringify(retryShowCache));
|
||||
global.linkCheckTime = new Date();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
@ -20,7 +20,7 @@ async function feedUpdater() {
|
||||
// Save the file
|
||||
log.info(updatedArray.length + ' items in file cache')
|
||||
fs.writeFileSync('./feedCache.json', JSON.stringify(updatedArray));
|
||||
global.rss_refresh_time = new Date();
|
||||
global.rssRefreshTime = new Date();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
@ -1,11 +1,9 @@
|
||||
const fs = require("fs");
|
||||
config = JSON.parse(fs.readFileSync('config.json'))
|
||||
|
||||
const { feedUpdater } = require('./FeedUpdater')
|
||||
const { filterFeed } = require('./FeedFilter')
|
||||
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 bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
@ -18,15 +16,16 @@ app.use(express.static(path.join(__dirname, "public")));
|
||||
app.use(cors());
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(bodyParser.json());
|
||||
pass = config.AdminPassword
|
||||
require('./routes')(app);
|
||||
app.use(basicAuth({
|
||||
users: { 'admin': pass },
|
||||
users: { 'admin': config.AdminPassword },
|
||||
challenge: true,
|
||||
}))
|
||||
const port = config.WebUIPort;
|
||||
|
||||
global.rss_refresh_time = new Date();
|
||||
global.link_check_time = new Date();
|
||||
global.rssRefreshTime = new Date();
|
||||
global.linkCheckTime = new Date();
|
||||
global.version = require('./package.json').version;
|
||||
|
||||
global.log = require('simple-node-logger').createSimpleLogger({
|
||||
logFilePath: 'jdrssdownloader.log',
|
||||
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() {
|
||||
log.tele('Running JDRssDownloader version ' + version)
|
||||
log.tele('Running JDRssDownloader version ' + global.version)
|
||||
try {
|
||||
RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins
|
||||
JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins
|
||||
RSSFeedRefreshMins = config.RSSFeedRefreshMins
|
||||
JDPostLinksMins = config.JDPostLinksMins
|
||||
} catch (error) {
|
||||
log.error('config.json file is missing.')
|
||||
}
|
||||
log.tele('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' 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 filterFeed, JDPostLinksMins * 60000);
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ async function addNewShow(showData) {
|
||||
})
|
||||
try {
|
||||
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) {
|
||||
console.error(err);
|
||||
}
|
||||
@ -35,7 +35,7 @@ async function removeShow(showData) {
|
||||
config.Shows = myArray
|
||||
try {
|
||||
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) {
|
||||
console.error(err);
|
||||
}
|
||||
|
10
routes/index.js
Normal file
10
routes/index.js
Normal 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
15
routes/logFIle.js
Normal 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
11
routes/root.js
Normal 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
28
routes/shows.js
Normal 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");
|
||||
});
|
||||
}
|
21
utils.js
21
utils.js
@ -2,20 +2,21 @@ const fs = require('fs');
|
||||
config = JSON.parse(fs.readFileSync('config.json'))
|
||||
var moment = require('moment');
|
||||
|
||||
|
||||
function next_rss_refresh() {
|
||||
var date1 = moment(global.rss_refresh_time);
|
||||
date1.add(config.RSSFeedRefreshMins, 'm');
|
||||
return new moment(date1).format("ddd, LTS")
|
||||
function returnUpdatedDate(date, offset) {
|
||||
var newDate = moment(date);
|
||||
newDate.add(offset, 'm');
|
||||
return new moment(newDate).format("ddd, LTS")
|
||||
}
|
||||
|
||||
function next_link_check() {
|
||||
var date1 = moment(global.link_check_time);
|
||||
date1.add(config.JDPostLinksMins, 'm');
|
||||
return new moment(date1).format("ddd, LTS")
|
||||
function nextRssRefresh() {
|
||||
return returnUpdatedDate(global.rssRefreshTime, config.RSSFeedRefreshMins)
|
||||
}
|
||||
|
||||
function nextLinkCheck() {
|
||||
return returnUpdatedDate(global.linkCheckTime, config.JDPostLinksMins)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
next_rss_refresh, next_link_check
|
||||
nextRssRefresh, nextLinkCheck
|
||||
}
|
||||
|
||||
|
@ -8,9 +8,9 @@ block layout-content
|
||||
h3 Number of Tracked Shows
|
||||
h1 #{showListLength}
|
||||
h3 Next RSS Refresh
|
||||
h1 #{rss_time}
|
||||
h1 #{rssTime}
|
||||
h3 Next Link Check
|
||||
h1 #{link_check}
|
||||
h1 #{linkCheck}
|
||||
div.NavButtons
|
||||
a(href="/shows")
|
||||
div.NavButton Show List
|
||||
@ -18,5 +18,5 @@ block layout-content
|
||||
div.NavButton Add New Show
|
||||
a(href="/shows/remove")
|
||||
div.NavButton Remove Show
|
||||
a(href="/logs")
|
||||
a(href="/logFile")
|
||||
div.NavButton Logs
|
@ -3,7 +3,7 @@ extends layout
|
||||
block layout-content
|
||||
div.View
|
||||
h1.Banner Log File
|
||||
div.body
|
||||
div.Message
|
||||
each val in logFile
|
||||
li= val
|
||||
div.NavButtons
|
Loading…
x
Reference in New Issue
Block a user