diff --git a/FeedFilter.js b/FeedFilter.js index 729ad1b..3bb61d8 100644 --- a/FeedFilter.js +++ b/FeedFilter.js @@ -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 = { diff --git a/FeedUpdater.js b/FeedUpdater.js index 05ae995..6905fb2 100644 --- a/FeedUpdater.js +++ b/FeedUpdater.js @@ -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 = { diff --git a/JDRssDownloader.js b/JDRssDownloader.js index 078f700..fbf05ad 100644 --- a/JDRssDownloader.js +++ b/JDRssDownloader.js @@ -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); } diff --git a/apiFunctions.js b/apiFunctions.js index 9f3bdc4..81e7ac3 100644 --- a/apiFunctions.js +++ b/apiFunctions.js @@ -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); } diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..cece3c0 --- /dev/null +++ b/routes/index.js @@ -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); + }); +} \ No newline at end of file diff --git a/routes/logFIle.js b/routes/logFIle.js new file mode 100644 index 0000000..5e0d62f --- /dev/null +++ b/routes/logFIle.js @@ -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() }); + }); + }); +} \ No newline at end of file diff --git a/routes/root.js b/routes/root.js new file mode 100644 index 0000000..ec52316 --- /dev/null +++ b/routes/root.js @@ -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() }); + }); +} \ No newline at end of file diff --git a/routes/shows.js b/routes/shows.js new file mode 100644 index 0000000..96369c6 --- /dev/null +++ b/routes/shows.js @@ -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"); + }); +} diff --git a/utils.js b/utils.js index 184d88e..0ceaf32 100644 --- a/utils.js +++ b/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 } diff --git a/views/index.pug b/views/index.pug index 24d7814..b94462d 100644 --- a/views/index.pug +++ b/views/index.pug @@ -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 \ No newline at end of file diff --git a/views/logs.pug b/views/logFile.pug similarity index 91% rename from views/logs.pug rename to views/logFile.pug index 4b6b7d9..a238775 100644 --- a/views/logs.pug +++ b/views/logFile.pug @@ -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