@yt-kit/core 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # YT-KIT (core)
2
+
3
+ Kit de herramientas para manejar multimedia orientada a videos.
4
+
5
+ ## Instalación
6
+
7
+ ```sh
8
+ # Con npm
9
+ npm install @yt-kit/core
10
+
11
+ # Con pnpm
12
+ pnpm add -E @yt-kit/core
13
+
14
+ # Con bun
15
+ bun add -E @yt-kit/core
16
+ ```
17
+
18
+ ## Sobre la estructura de carpetas
19
+
20
+ El archivo `pruebas.ts` es un archivo para probar el funcionamiento del proyecto antes de lanzar una nueva versión.
21
+
22
+ `src/index.ts` es el archivo principal para las builds, no para desarrollo. En todo caso, el archivo "principal" sería `pruebas.ts`.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './tasks/download/downloadVideo';
1
+ export * from './tasks/download/download';
2
2
  export * from './types/videoTypes';
3
- export * from './types/processTypes';
3
+ export * from './types/childProcessTypes';
4
4
  export { STANDARD_RESOLUTIONS } from './lib/constants';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Funcionalidades del programa
2
- export * from './tasks/download/downloadVideo';
2
+ export * from './tasks/download/download';
3
3
  // Tipos
4
4
  export * from './types/videoTypes';
5
- export * from './types/processTypes';
5
+ export * from './types/childProcessTypes';
6
6
  // Constantes públicas (seguras de exportar)
7
7
  export { STANDARD_RESOLUTIONS } from './lib/constants';
@@ -1,9 +1,16 @@
1
1
  export interface Downloader {
2
- download(url: string, options: DownloadOptions): Promise<DownloadResult>;
2
+ download(url: string, ytId: string, options: DownloadTasksOptions): Promise<DownloadResult>;
3
3
  }
4
4
  export interface DownloadOptions {
5
+ id: string;
6
+ outputPath?: string;
7
+ filename?: string;
8
+ }
9
+ export interface DownloadTasksOptions {
5
10
  id: string;
6
11
  type: 'video' | 'audio';
12
+ outputPath: string;
13
+ filename: string;
7
14
  }
8
15
  export interface DownloadResult {
9
16
  path: string;
@@ -2,7 +2,8 @@ export declare const STANDARD_RESOLUTIONS: readonly [18, 144, 240, 360, 480, 720
2
2
  export declare const COMMANDS: {
3
3
  readonly 'yt-dlp': "yt-dlp-linux";
4
4
  };
5
- export declare const RUTAS: {
6
- readonly VIDEOS_DESCARGADOS: "/home/mango/Dev/yt-kit/storage/videos_descargados";
7
- readonly AUDIOS_DESCARGADOS: "/home/mango/Dev/yt-kit/storage/audios_descargados";
5
+ export declare const DEFAULT_FILENAME = "%(ytId)s.%(ext)s";
6
+ export declare const PATTERNS: {
7
+ readonly ID: "%(id)s";
8
+ readonly YT_ID: "%(ytId)s";
8
9
  };
@@ -2,8 +2,8 @@ export const STANDARD_RESOLUTIONS = [18, 144, 240, 360, 480, 720, 1080, 1440, 21
2
2
  export const COMMANDS = {
3
3
  'yt-dlp': 'yt-dlp-linux'
4
4
  };
5
- const KWD = '/home/mango/Dev/yt-kit'; // KWD: [K]it [W]orking [D]irectory
6
- export const RUTAS = {
7
- VIDEOS_DESCARGADOS: `${KWD}/storage/videos_descargados`,
8
- AUDIOS_DESCARGADOS: `${KWD}/storage/audios_descargados`
5
+ export const DEFAULT_FILENAME = '%(ytId)s.%(ext)s';
6
+ export const PATTERNS = {
7
+ ID: '%(id)s',
8
+ YT_ID: '%(ytId)s'
9
9
  };
@@ -0,0 +1,4 @@
1
+ import type { PATTERNS } from './constants';
2
+ export type Pattern = typeof PATTERNS[keyof typeof PATTERNS];
3
+ export type PatternData = string;
4
+ export declare function expandPattern(pattern: string, patternMap: Map<Pattern, PatternData>): string;
@@ -0,0 +1,7 @@
1
+ export function expandPattern(pattern, patternMap) {
2
+ let resolvedPattern = pattern;
3
+ for (const [key, value] of patternMap) {
4
+ resolvedPattern = resolvedPattern.replaceAll(key, value);
5
+ }
6
+ return resolvedPattern;
7
+ }
@@ -0,0 +1 @@
1
+ export declare function streamLog(type: 'data' | 'err', data: any): void;
@@ -0,0 +1,8 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { stdout, stderr } from 'node:process';
3
+ export function streamLog(type, data) {
4
+ const stream = type === 'data'
5
+ ? stdout
6
+ : stderr;
7
+ stream.write(data);
8
+ }
@@ -0,0 +1 @@
1
+ export declare function streamLog(type: 'data' | 'err', data: any): void;
@@ -0,0 +1,8 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { stdout, stderr } from 'node:process';
3
+ export function streamLog(type, data) {
4
+ const stream = type === 'data'
5
+ ? stdout
6
+ : stderr;
7
+ stream.write(data);
8
+ }
@@ -0,0 +1,7 @@
1
+ interface ResolverProps {
2
+ filename: string;
3
+ id: string;
4
+ ytId: string;
5
+ }
6
+ export declare function resolveFilename({ filename, id, ytId }: ResolverProps): string;
7
+ export {};
@@ -0,0 +1,11 @@
1
+ import { PATTERNS } from './constants';
2
+ import { expandPattern } from './expandPattern';
3
+ import { sanitizeFilename } from './sanitizeFilename';
4
+ export function resolveFilename({ filename, id, ytId }) {
5
+ const map = new Map([
6
+ [PATTERNS.ID, id],
7
+ [PATTERNS.YT_ID, ytId]
8
+ ]);
9
+ const raw = expandPattern(filename, map);
10
+ return sanitizeFilename(raw);
11
+ }
@@ -0,0 +1 @@
1
+ export declare function sanitizeFilename(raw: string): string;
@@ -0,0 +1,6 @@
1
+ // Por ahora esto está enfocado solo para Linux,
2
+ // pero en un futuro sería ideal que soporte más sistemas operativos
3
+ export function sanitizeFilename(raw) {
4
+ return raw
5
+ .replaceAll('/', '⁄');
6
+ }
@@ -1,2 +1,2 @@
1
- import type { CommandKey } from '../types/processTypes';
1
+ import type { CommandKey } from '../types/childProcessTypes';
2
2
  export declare function spawnAsync(command: CommandKey, args: string[], showOutput?: boolean): Promise<unknown>;
@@ -1,24 +1,25 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { COMMANDS } from '../lib/constants';
3
+ import { streamLog } from './logger';
3
4
  export function spawnAsync(command, args, showOutput) {
4
5
  const _command = COMMANDS[command];
5
6
  return new Promise((resolve, reject) => {
6
- const process = spawn(_command, args);
7
+ const spawnProcess = spawn(_command, args);
7
8
  let stdout = '';
8
9
  let stderr = '';
9
- process.stdout.on('data', (chunk) => {
10
+ spawnProcess.stdout.on('data', (chunk) => {
10
11
  stdout += chunk;
11
12
  const chunkStr = chunk.toString();
12
13
  if (showOutput)
13
- console.log(chunkStr);
14
+ streamLog('data', chunkStr);
14
15
  });
15
- process.stderr.on('data', (chunk) => {
16
+ spawnProcess.stderr.on('data', (chunk) => {
16
17
  stderr += chunk;
17
18
  const chunkStr = chunk.toString();
18
19
  if (showOutput)
19
- console.log(chunkStr);
20
+ streamLog('err', chunkStr);
20
21
  });
21
- process.on('close', (code) => {
22
+ spawnProcess.on('close', (code) => {
22
23
  if (code === 0) {
23
24
  resolve(stdout.toString());
24
25
  }
@@ -26,7 +27,7 @@ export function spawnAsync(command, args, showOutput) {
26
27
  reject(new Error(stderr.toString()));
27
28
  }
28
29
  });
29
- process.on('error', (err) => {
30
+ spawnProcess.on('error', (err) => {
30
31
  reject(err);
31
32
  });
32
33
  });
@@ -0,0 +1,3 @@
1
+ import type { DownloadOptions } from '../../interfaces/Downloader';
2
+ export declare function downloadVideo(ytId: string, options: DownloadOptions): Promise<import("../../interfaces/Downloader").DownloadResult | undefined>;
3
+ export declare function downloadAudio(ytId: string, options: DownloadOptions): Promise<import("../../interfaces/Downloader").DownloadResult | undefined>;
@@ -0,0 +1,28 @@
1
+ // import { spawnAsync } from 'src/core/lib/spawnAsync'
2
+ // import type { DownloadOptions } from './interfaces/Downloader'
3
+ import { DEFAULT_FILENAME } from '../../lib/constants';
4
+ import { formYoutubeUrl } from '../../lib/ytUtils';
5
+ import { YtDlpDownloader } from '../../yt-dlp-downloader/YtDlpDownloader';
6
+ export async function downloadVideo(ytId, options) {
7
+ if (!ytId || !options.id)
8
+ return;
9
+ const url = formYoutubeUrl(ytId);
10
+ const taskOptions = formDownloadTaskOptions('video', options);
11
+ return new YtDlpDownloader().download(url, ytId, taskOptions);
12
+ }
13
+ export async function downloadAudio(ytId, options) {
14
+ if (!ytId || !options.id)
15
+ return;
16
+ const url = formYoutubeUrl(ytId);
17
+ const taskOptions = formDownloadTaskOptions('audio', options);
18
+ return new YtDlpDownloader().download(url, ytId, taskOptions);
19
+ }
20
+ function formDownloadTaskOptions(type, options) {
21
+ const taskOptions = {
22
+ id: options.id,
23
+ type,
24
+ outputPath: options.outputPath ?? '.',
25
+ filename: options.filename ?? DEFAULT_FILENAME
26
+ };
27
+ return taskOptions;
28
+ }
@@ -0,0 +1,3 @@
1
+ import type { COMMANDS } from '../lib/constants';
2
+ export type CommandKey = keyof typeof COMMANDS;
3
+ export type Command = typeof COMMANDS[keyof typeof COMMANDS];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type DownloadType = 'video' | 'audio';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type DownloadType = 'video' | 'audio';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
- import type { Downloader, DownloadOptions, DownloadResult } from '../interfaces/Downloader';
1
+ import type { Downloader, DownloadTasksOptions, DownloadResult } from '../interfaces/Downloader';
2
2
  export declare class YtDlpDownloader implements Downloader {
3
- download(url: string, options: DownloadOptions): Promise<DownloadResult>;
3
+ download(url: string, ytId: string, options: DownloadTasksOptions): Promise<DownloadResult>;
4
4
  private buildYtDlpArgs;
5
5
  }
@@ -1,8 +1,8 @@
1
- import { RUTAS } from '../lib/constants';
2
1
  import { spawnAsync } from '../lib/spawnAsync';
2
+ import { resolveFilename } from '../lib/resolveFilename';
3
3
  export class YtDlpDownloader {
4
- async download(url, options) {
5
- const args = this.buildYtDlpArgs(url, options);
4
+ async download(url, ytId, options) {
5
+ const args = this.buildYtDlpArgs(url, ytId, options);
6
6
  // const result = await spawnAsync('yt-dlp', args, true)
7
7
  await spawnAsync('yt-dlp', args, true);
8
8
  return {
@@ -10,22 +10,18 @@ export class YtDlpDownloader {
10
10
  path: 'unknown'
11
11
  };
12
12
  }
13
- buildYtDlpArgs(url, options) {
13
+ buildYtDlpArgs(url, ytId, options) {
14
14
  const { id, type } = options;
15
15
  const isVideo = type === 'video';
16
- const ext = isVideo
17
- ? 'mp4'
18
- : 'aac';
19
- const exportRoute = isVideo
20
- ? RUTAS.VIDEOS_DESCARGADOS
21
- : RUTAS.AUDIOS_DESCARGADOS;
22
- const exportName = '%(id)s.%(ext)s';
16
+ const exportRoute = options.outputPath;
17
+ const exportName = resolveFilename({ filename: options.filename, id: options.id, ytId });
23
18
  const audioFormat = 'aac';
19
+ const audioFormatPreferences = isVideo
20
+ ? []
21
+ : ['-x', '--audio-format', audioFormat];
24
22
  const args = [
25
- '-f', `${id}/${ext}`,
26
- ...(isVideo
27
- ? []
28
- : ['-x', '--audio-format', audioFormat]),
23
+ '-f', id,
24
+ ...audioFormatPreferences,
29
25
  '-o', exportName,
30
26
  '-P', exportRoute,
31
27
  url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yt-kit/core",
3
- "version": "0.2.4",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1 +0,0 @@
1
- export declare function downloadVideo(ytId: string, id: string): Promise<import("../../interfaces/Downloader").DownloadResult | undefined>;
@@ -1,9 +0,0 @@
1
- import { formYoutubeUrl } from '../../lib/ytUtils';
2
- import { YtDlpDownloader } from '../../yt-dlp-downloader/YtDlpDownloader';
3
- export async function downloadVideo(ytId, id) {
4
- if (!ytId || !id)
5
- return;
6
- const url = formYoutubeUrl(ytId);
7
- const options = { id, type: 'video' };
8
- return new YtDlpDownloader().download(url, options);
9
- }