@yt-kit/core 0.2.2 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from '@core/tasks/download/downloadVideo';
2
- export * from '@core/types/videoTypes';
3
- export * from '@core/types/processTypes';
4
- export { STANDARD_RESOLUTIONS } from '@core/lib/constants';
1
+ export * from './tasks/download/download';
2
+ export * from './types/videoTypes';
3
+ export * from './types/processTypes';
4
+ export { STANDARD_RESOLUTIONS } from './lib/constants';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Funcionalidades del programa
2
- export * from '@core/tasks/download/downloadVideo';
2
+ export * from './tasks/download/download';
3
3
  // Tipos
4
- export * from '@core/types/videoTypes';
5
- export * from '@core/types/processTypes';
4
+ export * from './types/videoTypes';
5
+ export * from './types/processTypes';
6
6
  // Constantes públicas (seguras de exportar)
7
- export { STANDARD_RESOLUTIONS } from '@core/lib/constants';
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, options: DownloadTasksOptions): Promise<DownloadResult>;
3
3
  }
4
4
  export interface DownloadOptions {
5
+ id: string;
6
+ outputFolder?: string;
7
+ filename?: string;
8
+ }
9
+ export interface DownloadTasksOptions {
5
10
  id: string;
6
11
  type: 'video' | 'audio';
12
+ outputFolder: string;
13
+ filename: string;
7
14
  }
8
15
  export interface DownloadResult {
9
16
  path: string;
@@ -2,7 +2,4 @@ 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";
8
- };
5
+ export declare const DEFAULT_FILENAME = "%(id)s.%(ext)s";
@@ -2,8 +2,4 @@ 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`
9
- };
5
+ export const DEFAULT_FILENAME = '%(id)s.%(ext)s';
@@ -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
+ }
@@ -1,2 +1,2 @@
1
- import type { CommandKey } from '@core/types/processTypes';
1
+ import type { CommandKey } from '../types/processTypes';
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
- import { COMMANDS } from '@core/lib/constants';
2
+ import { COMMANDS } from '../lib/constants';
3
+ import { streamLog } from './logs';
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 'src/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, 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, taskOptions);
19
+ }
20
+ function formDownloadTaskOptions(type, options) {
21
+ const taskOptions = {
22
+ id: options.id,
23
+ type,
24
+ outputFolder: options.outputFolder ?? '.',
25
+ filename: options.filename ?? DEFAULT_FILENAME
26
+ };
27
+ return taskOptions;
28
+ }
@@ -0,0 +1 @@
1
+ export type DownloadType = 'video' | 'audio';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,3 @@
1
- import type { COMMANDS } from '@core/lib/constants';
1
+ import type { COMMANDS } from '../lib/constants';
2
2
  export type CommandKey = keyof typeof COMMANDS;
3
3
  export type Command = typeof COMMANDS[keyof typeof COMMANDS];
@@ -1,3 +1,3 @@
1
- import type { STANDARD_RESOLUTIONS } from '@core/lib/constants';
1
+ import type { STANDARD_RESOLUTIONS } from '../lib/constants';
2
2
  export type Height = typeof STANDARD_RESOLUTIONS[number];
3
3
  export type Resolution = `${Height}p`;
@@ -1,5 +1,5 @@
1
- import type { Downloader, DownloadOptions, DownloadResult } from '@core/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, options: DownloadTasksOptions): Promise<DownloadResult>;
4
4
  private buildYtDlpArgs;
5
5
  }
@@ -1,5 +1,4 @@
1
- import { RUTAS } from '@core/lib/constants';
2
- import { spawnAsync } from '@core/lib/spawnAsync';
1
+ import { spawnAsync } from '../lib/spawnAsync';
3
2
  export class YtDlpDownloader {
4
3
  async download(url, options) {
5
4
  const args = this.buildYtDlpArgs(url, options);
@@ -13,19 +12,15 @@ export class YtDlpDownloader {
13
12
  buildYtDlpArgs(url, options) {
14
13
  const { id, type } = options;
15
14
  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';
15
+ const exportRoute = options.outputFolder;
16
+ const exportName = options.filename;
23
17
  const audioFormat = 'aac';
18
+ const audioFormatPreferences = isVideo
19
+ ? []
20
+ : ['-x', '--audio-format', audioFormat];
24
21
  const args = [
25
- '-f', `${id}/${ext}`,
26
- ...(isVideo
27
- ? []
28
- : ['-x', '--audio-format', audioFormat]),
22
+ '-f', id,
23
+ ...audioFormatPreferences,
29
24
  '-o', exportName,
30
25
  '-P', exportRoute,
31
26
  url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yt-kit/core",
3
- "version": "0.2.2",
3
+ "version": "0.3.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("@core/interfaces/Downloader").DownloadResult | undefined>;
@@ -1,9 +0,0 @@
1
- import { formYoutubeUrl } from '@core/lib/ytUtils';
2
- import { YtDlpDownloader } from '@core/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
- }