Skip to content

Commit

Permalink
All code commented
Browse files Browse the repository at this point in the history
  • Loading branch information
petersem committed Apr 2, 2021
1 parent 0046f27 commit e60a39e
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# any excluded dir with a .keep file will stay in github, but all other files will not :)
# any excluded dir with a .keep file. Will stay in github, but all other files will not :)
!.keep

# ignore the following folders
/node_modules
/public/mp3cache/**
/public/imagecache/**
docker-dompose.yaml
29 changes: 25 additions & 4 deletions classes/arr/radarr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ const util = require("./../core/utility");
const core = require("./../core/cache");
const axios = require("axios");

/**
* @desc Used to communicate with Radarr to obtain a list of future releases
* @param radarrUrl
* @param radarrToken
*/
class Radarr {
constructor(radarrUrl, radarrToken) {
this.radarrUrl = radarrUrl;
this.radarrToken = radarrToken;
}

/**
* @desc Gets the movie titles that fall within the range specified
* @param {string} startDate - in yyyy-mm-dd format - Generally todays date
* @param {string} endDate - in yyyy-mm-dd format - future date
* @returns {Promise<object>} json results - results of search
*/
async GetComingSoonRawData(startDate, endDate) {
let response;

Expand All @@ -24,7 +35,7 @@ class Radarr {
"&end=" +
endDate
)
.catch(err => {
.catch((err) => {
throw err;
});
} catch (err) {
Expand All @@ -36,7 +47,15 @@ class Radarr {
}

// ******* TODO - not yet done below this point!!!!!
async GetComingSoon(startDate, endDate, premieres) {


/**
* @desc Get Movie coming soon data and formats into mediaCard array
* @param {string} startDate - in yyyy-mm-dd format - Generally todays date
* @param {string} endDate - in yyyy-mm-dd format - future date
* @returns {Promise<object>} mediaCards array - results of search
*/
async GetComingSoon(startDate, endDate) {
let csCards = [];
// get raw data first
let raw = await this.GetComingSoonRawData(startDate, endDate);
Expand Down Expand Up @@ -152,10 +171,12 @@ class Radarr {
if (csCards.length == 0) {
console.log(now.toLocaleString() + " No Coming soon titles found");
} else {
console.log(now.toLocaleString() + " Coming soon 'Movie' titles refreshed");
console.log(
now.toLocaleString() + " Coming soon 'Movie' titles refreshed"
);
}
return csCards;
}
}

module.exports = Radarr;
module.exports = Radarr;
22 changes: 21 additions & 1 deletion classes/arr/sonarr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ const util = require("./../core/utility");
const core = require("./../core/cache");
const axios = require("axios");

/**
* @desc Used to communicate with Sonarr to obtain a list of future releases
* @param sonarrUrl
* @param sonarrToken
*/
class Sonarr {
constructor(sonarrUrl, sonarrToken) {
this.sonarrUrl = sonarrUrl;
this.sonarrToken = sonarrToken;
}

/**
* @desc Gets the tv titles that fall within the range specified
* @param {string} startDate - in yyyy-mm-dd format - Generally todays date
* @param {string} endDate - in yyyy-mm-dd format - future date
* @returns {Promise<object>} json results - results of search
*/
async GetComingSoonRawData(startDate, endDate) {
let response;

// call sonarr API and return results
try {
response = await axios
.get(
Expand All @@ -24,17 +36,25 @@ class Sonarr {
"&end=" +
endDate
)
.catch(err => {
.catch((err) => {
throw err;
});
} catch (err) {
// displpay error if call failed
let d = new Date();
console.log(d.toLocaleString() + " Sonarr error: ", err.message);
}

return response;
}

/**
* @desc Get TV coming soon data and formats into mediaCard array
* @param {string} startDate - in yyyy-mm-dd format - Generally todays date
* @param {string} endDate - in yyyy-mm-dd format - future date
* @param {string} premieres - boolean (string format) to show only season premieres
* @returns {Promise<object>} mediaCards array - results of search
*/
async GetComingSoon(startDate, endDate, premieres) {
let csCards = [];
// get raw data first
Expand Down
4 changes: 4 additions & 0 deletions classes/cards/CardType.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @desc A card type enum
* @returns nothing
*/
class CardType {

static CardTypeEnum = {NowScreening: "Now Screening", OnDemand: "On-Demand", ComingSoon: "Coming Soon", Playing: "Playing", IFrame: "Iframe", Picture: "Custom Picture"};
Expand Down
8 changes: 8 additions & 0 deletions classes/cards/MediaCard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const util = require("./../core/utility");

/**
* @desc mediaCards base class for defining every card that is showed in the poster app
* @returns nothing
*/
class MediaCard {
constructor() {
this.ID = null;
Expand Down Expand Up @@ -30,6 +34,10 @@ class MediaCard {
this.rendered = "";
}

/**
* @desc renders the properties of the card into html, then sets this to the 'rendered' property
* @returns nothing
*/
async Render() {
let hidden = "";
if (this.cardType != "Now Screening") hidden = "hidden";
Expand Down
43 changes: 39 additions & 4 deletions classes/core/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ const request = require("request");
const fsExtra = require("fs-extra");
const util = require("./utility");

class Core {
/**
* @desc Cache class manages the downloaad, cleanup and random selection of mp3 and poster image assets. Methods are static.
* @returns nothing
*/
class Cache {
constructor() {
return;
}

/**
* @desc Downloads the poster image
* @param {string} url - the full url to the picture file
* @param {string} fileName - the filename to save the image file as
* @returns nothing
*/
static async CacheImage(url, fileName) {
const savePath = "./public/imagecache/" + fileName;
await this.download(url, savePath, fileName);
return;
}

/**
* @desc Downloads the tv mp3 file from tvthemes.plexapp.com
* @param {string} fileName - the filename to download and save. this in the format of tvdbid.mp3
* @returns nothing
*/
static async CacheMP3(fileName) {
const savePath = "./public/mp3cache/" + fileName;
const url = "http://tvthemes.plexapp.com/" + fileName;
Expand All @@ -22,16 +37,23 @@ class Core {
return;
}

/**
* @desc Download any asset, providing it does not already exist in the save location
* @param {string} url - the full url to the asset
* @param {string} savePath - the path to save the asset to
* @param {string} fileName - the filename to save the asset as
* @returns nothing
*/
static async download(url, savePath, fileName) {
// download file function
const download = (url, savePath, callback) => {
try {
request.head(url, (err, res, body) => {
request(url)
.pipe(fs.createWriteStream(savePath,{autoClose:true}))
.pipe(fs.createWriteStream(savePath, { autoClose: true }))
.on("close", callback)
.on("error", (err) => {
console.log('download failed: ' + err.message);
console.log("download failed: " + err.message);
})
.on("finish", () => {
//console.log("Download Completed");
Expand All @@ -58,23 +80,36 @@ class Core {
}
}

/**
* @desc Deletes all files from the MP3Cache folder
* @returns nothing
*/
static async DeleteMP3Cache() {
const directory = "./public/mp3cache/";
fsExtra.emptyDirSync(directory);
console.log("✅ MP3 cache cleared");
}

/**
* @desc Deletes all files from the imageCache folder
* @returns nothing
*/
static async DeleteImageCache() {
const directory = "./public/imagecache/";
fsExtra.emptyDirSync(directory);
console.log("✅ Image cache cleared");
}

/**
* @desc Returns a single random mp3 filename from the randomthemese folder
* @returns {string} fileName - a random filename
*/
static async GetRandomMP3() {
let directory = "./public/randomthemes";

// calls radom_items function to return a random item from an array
let randomFile = await util.random_item(fs.readdirSync(directory));
return randomFile;
}
}
module.exports = Core;
module.exports = Cache;
14 changes: 13 additions & 1 deletion classes/core/globalPage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const cache = require("./cache");

/**
* @desc globalPage object is passed to poster.ejs and contains all browser settings and card data
* @param {number} slideDuration - how long each slide will be visible for (seconds)
* @param {number} refreshPeriod - how long before the brpwser does a refresh and data is refreshed (seconds)
* @param {string} playThemesIfAvailable - boolean - will enable theme music wherever it is set
* @param {string} playGenericThemes - boolean - will play random themes from the /randomthemes directory for movies
* @param {string} fadeTransition - boolean - if true, will fade transition. false will slide.
* @returns {<object>} globalPage
*/
class globalPage {
constructor(
slideDuration,
Expand All @@ -18,6 +27,10 @@ class globalPage {
return;
}

/**
* @desc Takes merged mediaCard set and applies card order number and active card slide, then generates the rendered HTML for each media card.
* @returns nothing
*/
async OrderAndRenderCards() {
if (this.cards.length != 0) {
let webID = 0;
Expand All @@ -38,7 +51,6 @@ class globalPage {
}
return;
}

}

module.exports = globalPage;
34 changes: 27 additions & 7 deletions classes/core/utility.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/**
* @desc utility class for string and object handling
* @returns {<object>} utility
*/
class utility {
// return true if null
/**
* @desc Returns true is null, empty or undefined
* @param {string} val
* @returns {Promise<boolean>} boolean - true empty, undefined or null
*/
static async isEmpty(val) {
if (val == undefined || val == "" || val == null) {
return true;
Expand All @@ -8,7 +16,11 @@ class utility {
}
}

// return empty string if null
/**
* @desc Returns an empty string if undefined, null or empty, else the submitted value
* @param {string} val
* @returns {Promise<string>} string - either an empty string or the submitted string value
*/
static async emptyIfNull(val) {
if (val == undefined || val == null || val == "") {
return "";
Expand All @@ -17,14 +29,22 @@ class utility {
}
}

// gets a random item from an array
/**
* @desc Gets a random item from an array
* @param {Array} items - a given array of anything
* @returns {Promise<object>} object - returns one random item
*/
static async random_item(items) {
return items[Math.floor(Math.random() * items.length)];
}

// builds random set of on-demand cards
static async build_random_od_set(numberOnDemand,mediaCards) {

/**
* @desc builds random set of on-demand cards
* @param {number} numberOnDemand - the number of on-demand cards to return
* @param {object} mediaCards - an array of on-demand mediaCards
* @returns {Promise<object>} mediaCard[] - an array of mediaCards
*/
static async build_random_od_set(numberOnDemand, mediaCards) {
let onDemandCards = [];
for await (let i of Array(numberOnDemand).keys()) {
let odc;
Expand All @@ -35,4 +55,4 @@ class utility {
}
}

module.exports = utility;
module.exports = utility;
Loading

0 comments on commit e60a39e

Please sign in to comment.