docs
This commit is contained in:
parent
121452e1c9
commit
cae6761d24
7 changed files with 132 additions and 78 deletions
12
lib/args.js
12
lib/args.js
|
@ -1,5 +1,11 @@
|
|||
let argRegex = new RegExp('(\-\-[a-zA-Z0-9]+)(=)(.*)');
|
||||
|
||||
/**
|
||||
* Parses args from node process
|
||||
* @returns { {[ key: string ]: string | string[]} } parsed args array;
|
||||
* if the arg followed format `--arg=val`, it will be indexed as `args[arg]=val`;
|
||||
* otherwise it will be pushed to an array under `args['_']`
|
||||
*/
|
||||
const parseArgs = () => {
|
||||
let _args = { '_': [] };
|
||||
[...process.argv.slice(2)].forEach(arg => {
|
||||
|
@ -16,6 +22,12 @@ const parseArgs = () => {
|
|||
|
||||
export const args = parseArgs();
|
||||
|
||||
/**
|
||||
* Gets value of specified arg
|
||||
* @param {string} argName the name of the arg to retrieve
|
||||
* @returns {string} value of specified arg
|
||||
* @throws will throw error if arg was not specified
|
||||
*/
|
||||
export const getArg = (argName) => {
|
||||
let arg = args[argName];
|
||||
if (!!arg) {
|
||||
|
|
46
lib/dl.js
46
lib/dl.js
|
@ -5,6 +5,50 @@ import { error, log } from './log.js';
|
|||
|
||||
const ctx = 'getUser.js';
|
||||
|
||||
/**
|
||||
* Runs {@link getUser} concurrently for many users
|
||||
* @param {{ user: string, logs: string[] }[]} userDb userDb to run {@link getUser} on
|
||||
* @param {number} threadMax max number of threads to run concurrently
|
||||
* @param {string} directory the directory to save the user media folders in
|
||||
* @returns {Promise<void>} promise which resolves once all threads for every user have finished
|
||||
*/
|
||||
export const getMany = (userDb, threadMax, directory) => new Promise((resolve, reject) => {
|
||||
let running = 0;
|
||||
let index = 0;
|
||||
|
||||
const get = () => {
|
||||
const onFinish = (currentIndex) => {
|
||||
log(ctx, `Finished ${userDb[currentIndex].user}/media`);
|
||||
--running;
|
||||
get();
|
||||
}
|
||||
|
||||
while (running < threadMax && index < userDb.length) {
|
||||
++running;
|
||||
let currentIndex = index++;
|
||||
|
||||
let proc = getUser(userDb[currentIndex].user, directory);
|
||||
proc.stdout.on('data', data => {
|
||||
userDb[currentIndex].logs.push(data);
|
||||
});
|
||||
proc.stderr.on('data', _ => onFinish(currentIndex));
|
||||
proc.on('close', _ => onFinish(currentIndex));
|
||||
proc.on('error', _ => onFinish(currentIndex));
|
||||
}
|
||||
|
||||
if (running === 0) {
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
get();
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieves gallery for specified user and saves to the specified parent path
|
||||
* @param {string} user the user to retrieve media from
|
||||
* @param {string} path the path to save the user's media folder in
|
||||
* @returns {ChildProcess} the process that was {@link spawn spawned}
|
||||
*/
|
||||
export const getUser = (user, path) => {
|
||||
const url = `https://twitter.com/${user}/media`;
|
||||
let args;
|
||||
|
@ -18,7 +62,7 @@ export const getUser = (user, path) => {
|
|||
const proc = spawn(`python3 ~/.local/bin/gallery-dl -c ./config.json${!!args ? ' ' + args + ' ' : ' '}-d ${path} ${url}`, { shell: true });
|
||||
|
||||
proc.stdout.on('data', data => {
|
||||
//log(ctx, data);
|
||||
log(ctx, data);
|
||||
});
|
||||
proc.stderr.on('data', data => {
|
||||
error(ctx, data);
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
import { readdir } from 'fs/promises'
|
||||
|
||||
export const getChildren = async source =>
|
||||
/**
|
||||
* Gets the directories under the specified source directory
|
||||
* @param {string} source path to the parent directory
|
||||
* @returns {Promise<string[]>} the names of the child directories
|
||||
*/
|
||||
export const getChildDirectories = async source =>
|
||||
(await readdir(source, { withFileTypes: true }))
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name)
|
||||
|
|
14
lib/log.js
14
lib/log.js
|
@ -2,15 +2,29 @@ const options = { year: 'numeric', day: '2-digit', month: '2-digit', hour: '2-di
|
|||
const osLocale = (process.env.LANG || process.env.LANGUAGE || process.env.LC_ALL || process.env.LC_MESSAGES).split('.')[0].replace('_', '-');
|
||||
const dtFormat = Intl.DateTimeFormat(osLocale, options);
|
||||
|
||||
/**
|
||||
* Gets formatted timestamp
|
||||
* @returns {string} timestamp string
|
||||
*/
|
||||
export const getTime = () => {
|
||||
return dtFormat.format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs formatted output to the console
|
||||
* @param {string} src the source script doing the logging
|
||||
* @param {string} msg the message to log
|
||||
*/
|
||||
export const log = (src, msg) => {
|
||||
const time = getTime();
|
||||
console.log(`${time} : ${src} : ${msg}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* Logs formatted output to the console as an error
|
||||
* @param {string} src the source script doing the logging
|
||||
* @param {string} msg the message to log
|
||||
*/
|
||||
export const error = (src, msg) => {
|
||||
const time = new Date().toISOString();
|
||||
console.error(`${time} : ${src} : ${msg}`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue