Skip to content

Commit

Permalink
Logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Apr 4, 2024
1 parent 78d6541 commit eb42d3e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "floatplane-plex-downloader",
"version": "5.12.0",
"version": "5.12.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fetchFFMPEG } from "./lib/helpers/fetchFFMPEG.js";
import { defaultSettings } from "./lib/defaults.js";

import { loginFloatplane, User } from "./logins.js";
import { VideoDownloader } from "./Downloader.js";
import { VideoDownloader } from "./lib/Downloader.js";
import chalk from "chalk-template";

import type { ContentPost } from "floatplane/content";
Expand Down Expand Up @@ -103,9 +103,10 @@ process.on("SIGTERM", process.exit);
console.log(`Unable to authenticate with floatplane... ${(<Error>err).message}\nPlease login to floatplane...`);
user = await loginFloatplane();
}

await initProm(user!.id);

console.log(`Initalized! Running version ${DownloaderVersion} instance ${user!.id}`);

await downloadNewVideos();

if (settings.floatplane.waitForNewVideos === true) {
Expand Down
12 changes: 7 additions & 5 deletions src/Downloader.ts → src/lib/Downloader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Counter, Gauge } from "prom-client";
import { Video } from "./lib/Video.js";
import { Video } from "./Video.js";
import type { Progress } from "got";

import { settings, args } from "./lib/helpers/index.js";
import { settings, args } from "./helpers/index.js";
import { MyPlexAccount } from "@ctrl/plex";

import { ProgressHeadless } from "./lib/logging/ProgressConsole.js";
import { ProgressBars } from "./lib/logging/ProgressBars.js";
import { ProgressHeadless } from "./logging/ProgressConsole.js";
import { ProgressBars } from "./logging/ProgressBars.js";

import { promisify } from "util";
const sleep = promisify(setTimeout);
Expand Down Expand Up @@ -81,6 +81,7 @@ export class VideoDownloader {

let downloadInterval: NodeJS.Timeout;
downloadRequest.once("downloadProgress", (downloadProgress: Progress) => {
logger.log("Starting download...");
downloadInterval = setInterval(() => logger.onDownloadProgress(downloadRequest.downloadProgress), 125);
logger.onDownloadProgress(downloadProgress);
});
Expand All @@ -91,6 +92,7 @@ export class VideoDownloader {
}).finally(() => {
clearInterval(downloadInterval);
logger.onDownloadProgress(downloadRequest.downloadProgress);
logger.log("Finished download...");
});
}
// eslint-disable-next-line no-fallthrough
Expand All @@ -107,7 +109,7 @@ export class VideoDownloader {
}
// eslint-disable-next-line no-fallthrough
case Video.State.Muxed: {
logger.done("Muxed");
logger.done("Download & Muxing complete!");
VideoDownloader.ProgressLogger.CompletedVideos++;
promDownloadedTotal.inc();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "dotenv/config";
import json5 from "json5";
const { parse } = json5;

export const DownloaderVersion = "5.12.0";
export const DownloaderVersion = "5.12.1";

import type { PartialArgs, Settings } from "../types.js";

Expand Down
35 changes: 17 additions & 18 deletions src/lib/logging/ProgressBars.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { MultiProgressBars } from "multi-progress-bars";
import { ProgressLogger, type IProgressLogger } from "./ProgressLogger.js";
import type { Progress } from "got";
import chalk from "chalk-template";

export class ProgressBars extends ProgressLogger implements IProgressLogger {
private static readonly _reset = "\u001b[0m";
private static readonly cy = (str: string | number) => `\u001b[36;1m${str}\u001b[0m`;
private static readonly gr = (str: string | number) => `\u001b[32;1m${str}\u001b[0m`;
private static readonly ye = (str: string | number) => `\u001b[33;1m${str}\u001b[0m`;
private static readonly bl = (str: string | number) => `\u001b[34;1m${str}\u001b[0m`;

private static readonly _Bars: MultiProgressBars = new MultiProgressBars({ initMessage: "", anchor: "bottom" });

public static TotalBytes = 0;
public static DownloadedBytes = 0;
public static DownloadSpeed = 0;
public static Errors = 0;

private _startTime: undefined | number = undefined;

Expand Down Expand Up @@ -43,9 +39,12 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {
this.removeBar();
}
public error(message: string, final?: true) {
this.log(`${ProgressLogger.ERR}: ${message}`);
this.log(chalk`{red ERR}: ${message}`);
this.reset();
if (final) this.removeBar();
if (final) {
this.removeBar();
ProgressBars.Errors++;
}
}
private removeBar() {
setTimeout(() => ProgressBars._Bars.removeTask(this.title), 10000 + Math.floor(Math.random() * 6000));
Expand All @@ -71,22 +70,22 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {

const downloadETA = progress.total / this._downloadSpeed - elapsed;

const downloaded = `${ProgressBars.cy((progress.transferred / 1000000).toFixed(2))}/${ProgressBars.cy(`${(progress.total / 1000000).toFixed(2)}MB`)}`;
const speed = `${ProgressBars.gr((this._downloadSpeed / 125000).toFixed(2) + "mb/s")}`;
const eta = `ETA: ${ProgressBars.bl(`${Math.floor(downloadETA / 60)}m ${Math.floor(downloadETA) % 60}s`)}`;
const downloaded = chalk`{cyan ${(progress.transferred / 1000000).toFixed(2)}}/{cyan ${(progress.total / 1000000).toFixed(2)}MB}`;
const speed = chalk`{green ${(this._downloadSpeed / 125000).toFixed(2)} mb/s}`;
const eta = chalk`ETA: {blue ${Math.floor(downloadETA / 60)}m ${Math.floor(downloadETA) % 60}s}`;

ProgressBars._Bars.updateTask(this.title, {
percentage: progress.percent,
message: `${ProgressBars._reset}${downloaded} ${speed} ${eta}`,
message: `${downloaded} ${speed} ${eta}`,
});

const processed = `Processed: ${ProgressBars.ye(ProgressBars.CompletedVideos)}/${ProgressBars.ye(ProgressBars.TotalVideos)}`;
const downloadedTotal = `Total Downloaded: ${ProgressBars.cy((ProgressBars.DownloadedBytes / 1000000).toFixed(2))}/${ProgressBars.cy(
`${(ProgressBars.TotalBytes / 1000000).toFixed(2)}MB`,
)}`;
const speedTotal = `Download Speed: ${ProgressBars.gr(`${(ProgressBars.DownloadSpeed / 125000).toFixed(2)}mb/s`)}`;
const processed = chalk`Processed: {yellow ${ProgressBars.CompletedVideos}}/{yellow ${ProgressBars.TotalVideos}} Errors: {red ${ProgressBars.Errors}}`;
const downloadedTotal = chalk`Total Downloaded: {cyan ${(ProgressBars.DownloadedBytes / 1000000).toFixed(2)}}/{cyan ${(
ProgressBars.TotalBytes / 1000000
).toFixed(2)}MB}`;
const speedTotal = chalk`Download Speed: {green ${(ProgressBars.DownloadSpeed / 125000).toFixed(2)}mb/s}`;
ProgressBars._Bars.setFooter({
message: `${processed} ${downloadedTotal} ${speedTotal}`,
message: `${processed} ${downloadedTotal} ${speedTotal}`,
pattern: "",
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/logging/ProgressConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export class ProgressHeadless extends ProgressLogger implements IProgressLogger
this.log(`${message} (${ProgressLogger.CompletedVideos}/${ProgressLogger.TotalVideos})`);
}
public error(message: string) {
this.log(`${ProgressLogger.ERR}: ${message}`);
this.log(`An error occoured: ${message}`);
}
}
2 changes: 0 additions & 2 deletions src/lib/logging/ProgressLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export class ProgressLogger {
public static TotalVideos = 0;
public static CompletedVideos = 0;

public static readonly ERR = "\u001b[31m\u001b[1mERR\u001b[0m";

public downloadedBytes = 0;

private static _downloadedBytesTotalCounter = new Counter({
Expand Down

0 comments on commit eb42d3e

Please sign in to comment.