@yt-kit/core 0.3.0 → 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
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
@@ -2,6 +2,6 @@
2
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,15 +1,15 @@
1
1
  export interface Downloader {
2
- download(url: string, options: DownloadTasksOptions): Promise<DownloadResult>;
2
+ download(url: string, ytId: string, options: DownloadTasksOptions): Promise<DownloadResult>;
3
3
  }
4
4
  export interface DownloadOptions {
5
5
  id: string;
6
- outputFolder?: string;
6
+ outputPath?: string;
7
7
  filename?: string;
8
8
  }
9
9
  export interface DownloadTasksOptions {
10
10
  id: string;
11
11
  type: 'video' | 'audio';
12
- outputFolder: string;
12
+ outputPath: string;
13
13
  filename: string;
14
14
  }
15
15
  export interface DownloadResult {
@@ -2,4 +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 DEFAULT_FILENAME = "%(id)s.%(ext)s";
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";
9
+ };
@@ -2,4 +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
- export const DEFAULT_FILENAME = '%(id)s.%(ext)s';
5
+ export const DEFAULT_FILENAME = '%(ytId)s.%(ext)s';
6
+ export const PATTERNS = {
7
+ ID: '%(id)s',
8
+ YT_ID: '%(ytId)s'
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,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,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { COMMANDS } from '../lib/constants';
3
- import { streamLog } from './logs';
3
+ import { streamLog } from './logger';
4
4
  export function spawnAsync(command, args, showOutput) {
5
5
  const _command = COMMANDS[command];
6
6
  return new Promise((resolve, reject) => {
@@ -1,6 +1,6 @@
1
1
  // import { spawnAsync } from 'src/core/lib/spawnAsync'
2
2
  // import type { DownloadOptions } from './interfaces/Downloader'
3
- import { DEFAULT_FILENAME } from 'src/lib/constants';
3
+ import { DEFAULT_FILENAME } from '../../lib/constants';
4
4
  import { formYoutubeUrl } from '../../lib/ytUtils';
5
5
  import { YtDlpDownloader } from '../../yt-dlp-downloader/YtDlpDownloader';
6
6
  export async function downloadVideo(ytId, options) {
@@ -8,20 +8,20 @@ export async function downloadVideo(ytId, options) {
8
8
  return;
9
9
  const url = formYoutubeUrl(ytId);
10
10
  const taskOptions = formDownloadTaskOptions('video', options);
11
- return new YtDlpDownloader().download(url, taskOptions);
11
+ return new YtDlpDownloader().download(url, ytId, taskOptions);
12
12
  }
13
13
  export async function downloadAudio(ytId, options) {
14
14
  if (!ytId || !options.id)
15
15
  return;
16
16
  const url = formYoutubeUrl(ytId);
17
17
  const taskOptions = formDownloadTaskOptions('audio', options);
18
- return new YtDlpDownloader().download(url, taskOptions);
18
+ return new YtDlpDownloader().download(url, ytId, taskOptions);
19
19
  }
20
20
  function formDownloadTaskOptions(type, options) {
21
21
  const taskOptions = {
22
22
  id: options.id,
23
23
  type,
24
- outputFolder: options.outputFolder ?? '.',
24
+ outputPath: options.outputPath ?? '.',
25
25
  filename: options.filename ?? DEFAULT_FILENAME
26
26
  };
27
27
  return taskOptions;
@@ -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 {};
@@ -1,5 +1,5 @@
1
1
  import type { Downloader, DownloadTasksOptions, DownloadResult } from '../interfaces/Downloader';
2
2
  export declare class YtDlpDownloader implements Downloader {
3
- download(url: string, options: DownloadTasksOptions): Promise<DownloadResult>;
3
+ download(url: string, ytId: string, options: DownloadTasksOptions): Promise<DownloadResult>;
4
4
  private buildYtDlpArgs;
5
5
  }
@@ -1,7 +1,8 @@
1
1
  import { spawnAsync } from '../lib/spawnAsync';
2
+ import { resolveFilename } from '../lib/resolveFilename';
2
3
  export class YtDlpDownloader {
3
- async download(url, options) {
4
- const args = this.buildYtDlpArgs(url, options);
4
+ async download(url, ytId, options) {
5
+ const args = this.buildYtDlpArgs(url, ytId, options);
5
6
  // const result = await spawnAsync('yt-dlp', args, true)
6
7
  await spawnAsync('yt-dlp', args, true);
7
8
  return {
@@ -9,11 +10,11 @@ export class YtDlpDownloader {
9
10
  path: 'unknown'
10
11
  };
11
12
  }
12
- buildYtDlpArgs(url, options) {
13
+ buildYtDlpArgs(url, ytId, options) {
13
14
  const { id, type } = options;
14
15
  const isVideo = type === 'video';
15
- const exportRoute = options.outputFolder;
16
- const exportName = options.filename;
16
+ const exportRoute = options.outputPath;
17
+ const exportName = resolveFilename({ filename: options.filename, id: options.id, ytId });
17
18
  const audioFormat = 'aac';
18
19
  const audioFormatPreferences = isVideo
19
20
  ? []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yt-kit/core",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"