@yt-kit/core 0.6.0 → 1.0.1

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.
@@ -1,12 +1,9 @@
1
- export interface CachedData {
2
- content: string;
3
- timestamp: number;
4
- }
1
+ import type { CachedData, CacheKey, ValueToCache } from '../types/cacheTypes';
5
2
  export declare class CacheManager {
6
3
  private store;
7
4
  private defaultTtl;
8
- get(key: string): CachedData | null;
9
- set(key: string, value: CachedData): void;
5
+ get(key: CacheKey, json?: boolean): Promise<CachedData | null>;
6
+ set(key: CacheKey, value: ValueToCache): Promise<void>;
10
7
  delete(key: string): void;
11
8
  }
12
9
  export declare const cache: CacheManager;
@@ -0,0 +1,2 @@
1
+ import type { CachedData, CacheKey } from '../types/cacheTypes';
2
+ export declare function getFromDisk(key: CacheKey, json?: boolean): Promise<CachedData | undefined>;
@@ -0,0 +1,2 @@
1
+ import type { CacheKey } from '../types/cacheTypes';
2
+ export declare function removeFromDisk(key: CacheKey): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import type { CachedData, CacheKey } from '../types/cacheTypes';
2
+ export declare function saveInDisk(key: CacheKey, value: CachedData): Promise<void>;
@@ -0,0 +1,8 @@
1
+ import type { ConfigKey, ConfigValue } from '../types/configTypes';
2
+ export declare class Configuration {
3
+ private store;
4
+ get<K extends ConfigKey>(key: K): ConfigValue<K> | null;
5
+ set<K extends ConfigKey>(key: K, value: ConfigValue<K>): void;
6
+ delete<K extends ConfigKey>(key: K): void;
7
+ }
8
+ export declare const config: Configuration;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,17 @@
1
+ export { CacheManager } from './cache/CacheManager';
2
+ export { Configuration } from './config/Configuration';
1
3
  export { downloadAudio, downloadVideo } from './tasks/download/download';
2
4
  export { findFormatId } from './yt-dlp/findFormatId';
5
+ export { getFromDisk } from './cache/getFromDisk';
6
+ export { saveInDisk } from './cache/saveInDisk';
3
7
  export { formYoutubeUrl } from './lib/ytUtils';
4
- export * from './types/videoTypes';
5
- export * from './types/childProcessTypes';
8
+ export { getBetterFormat, getWorstFormat } from './lib/compareFormats';
9
+ export { resolveFilenamePattern } from './lib/resolveFilenamePattern';
10
+ export { expandPattern } from './lib/expandPattern';
11
+ export { resolvePath } from './lib/resolvePath';
12
+ export type * from './types/videoTypes';
13
+ export type * from './types/childProcessTypes';
14
+ export type * from './types/configTypes';
15
+ export type * from './types/cacheTypes';
16
+ export type { Pattern, PatternData } from './lib/expandPattern';
6
17
  export { STANDARD_RESOLUTIONS } from './lib/constants';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var q=[18,144,240,360,480,720,1080,1440,2160],R={"yt-dlp":"yt-dlp-linux"},P="%(ytId)s.%(ext)s",T={ID:"%(id)s",YT_ID:"%(ytId)s"};function l(t){return`https://youtube.com/watch?v=${t}`}import{spawn as K}from"node:child_process";import{stdout as b,stderr as B}from"node:process";function C(t,e){(t==="data"?b:B).write(e)}function d(t,e,r){let o=R[t];return new Promise((i,p)=>{let f=K(o,e),c="",m="";f.stdout.on("data",(a)=>{c+=a;let s=a.toString();if(r)C("data",s)}),f.stderr.on("data",(a)=>{m+=a;let s=a.toString();if(r)C("err",s)}),f.on("close",(a)=>{if(a===0)i(c.toString());else p(Error(m.toString()))}),f.on("error",(a)=>{p(a)})})}function M(t,e){let r=t;for(let[o,i]of e)r=r.replaceAll(o,i);return r}function N(t){return t.replaceAll("/","⁄")}function E({filename:t,id:e,ytId:r}){let o=new Map([[T.ID,e],[T.YT_ID,r]]),i=M(t,o);return N(i)}class y{async download(t,e,r){let o=this.buildYtDlpArgs(t,e,r);return await d("yt-dlp",o,!0),{duration:-1,path:"unknown"}}buildYtDlpArgs(t,e,r){let{id:o,type:i}=r,p=i==="video",f=r.outputPath,c=E({filename:r.filename,id:r.id,ytId:e}),m="aac";return["-f",o,...p?[]:["-x","--audio-format","aac"],"-o",c,"-P",f,t]}}async function $(t,e){if(!t||!e.id)return;let r=l(t),o=O("video",e);return new y().download(r,t,o)}async function V(t,e){if(!t||!e.id)return;let r=l(t),o=O("audio",e);return new y().download(r,t,o)}function O(t,e){return{id:e.id,type:t,outputPath:e.outputPath??".",filename:e.filename??P}}function w({a:t,b:e,compareResolution:r,type:o}){let i=o==="video"&&r&&t.quality!==null&&e.quality!==null,p=o==="video"&&r&&t.height!==null&&e.height!==null,f=o==="video"&&t.fps!==null&&e.fps!==null,c=o==="audio"&&t.asr!==null&&e.asr!==null,m=t.tbr!==null&&e.tbr!==null,a=0,s=0;if(i){if((t.quality??0)>(e.quality??0))a+=3;else if((t.quality??0)<(e.quality??0))s+=3}if(p){if((t.height??0)>(e.height??0))a+=3;else if((t.height??0)<(e.height??0))s+=3}if(f){if((t.fps??0)>(e.fps??0))a+=2;else if((t.fps??0)<(e.fps??0))s+=2}if(c){if((t.asr??0)>(e.asr??0))a+=2;else if((t.asr??0)<(e.asr??0))s+=2}if(m){if((t.tbr??0)>(e.tbr??0))a+=1;else if((t.tbr??0)<(e.tbr??0))s+=1}return{aScore:a,bScore:s}}function h(t,e,{type:r,compareResolution:o}){if(!t||!e)return;let{aScore:i,bScore:p}=w({a:t,b:e,type:r,compareResolution:o});return i>p?t:e}function A(t,e,{type:r,compareResolution:o}){if(!t||!e)return;let{aScore:i,bScore:p}=w({a:t,b:e,type:r,compareResolution:o});return i>p?e:t}var Y={day:86400000};function v(t,e){if(!Y[e])return t;return Y[e]*t}class _{store=new Map;defaultTtl=v(10,"day");get(t){let e=this.store.get(t);if(!e)return null;if(Date.now()-e.timestamp>this.defaultTtl)return this.delete(t),null;return e}set(t,e){this.store.set(t,{...e,timestamp:Date.now()})}delete(t){this.store.delete(t)}}var g=new _;function L(t){return g.get(t)}function U(t,e){return g.set(t,e)}async function z(t,e){let r=l(t),o=`formats-${t}`,i=Boolean(e.match(/\d/)),p=i?!1:"N/A",f=["--print","%(formats)j",r],m=L(o)?.content??"";if(!m){let n;try{n=await d("yt-dlp",f)}catch(x){throw console.error("Error consiguiendo el ID del formato"),x}if(typeof n!=="string")throw Error("La salida del proceso es de tipo inválido (se esperaba string)");m=n,U(o,{content:m,timestamp:Date.now()})}let a=[];try{a=JSON.parse(m)}catch(n){throw console.error("Error convirtiendo la salida de yt-dlp a JSON"),n}if(!Array.isArray(a))throw Error("Se esperaba un array");let s=void 0,D=void 0,S=void 0,F=void 0,u=void 0;for(let n of a){let x=n.resolution==="audio only";if(i&&n.format_note===e){p=!0,u=h(u,n,{compareResolution:!1,type:"video"})??n;continue}if(x)S=h(S,n,{compareResolution:!1,type:"audio"})??n,F=A(F,n,{compareResolution:!1,type:"audio"})??n;else s=h(s,n,{compareResolution:!0,type:"video"})??n,D=A(D,n,{compareResolution:!0,type:"video"})??n}if(!i)u={"best-video":s,"worst-video":D,"best-audio":S,"worst-audio":F}[e];return{foundSpecific:p,formatId:u?.format_id,desiredFormat:u}}export{l as formYoutubeUrl,z as findFormatId,$ as downloadVideo,V as downloadAudio,q as STANDARD_RESOLUTIONS};
1
+ class A{store=new Map;get(t){let e=this.store.get(t);if(!e)return null;return e}set(t,e){this.store.set(t,e)}delete(t){this.store.delete(t)}}var l=new A;var V={day:86400000,hrs:3600000,min:60000,sec:1000,nano:0.001};function _(t,e){if(!V[e])return t;return t*V[e]}function Y(t){if(typeof t!=="string")return!1;return!0}import{readFile as W}from"node:fs/promises";import{homedir as J}from"node:os";function c(t){return t.replace("~",J())}async function h(t,e=!1){let r=l.get("cache")?.cacheLocation;if(!r)return;let o=c(r),s;try{s=await W(o,"utf8")}catch(a){console.error(`Error leyendo la caché en el almacenamiento para ${t} :`,a)}if(e&&s)try{s=JSON.parse(s)}catch(a){console.error(`Error convirtiendo la caché de ${t} a JSON:`,a)}if(!s)return;return{content:e?s.content:s,timestamp:Date.now()}}import{rm as L}from"node:fs/promises";async function q(t){let e=l.get("cache")?.cacheLocation;if(!e)return;let r=c(e);try{await L(r)}catch(o){console.error(`Error borrando la caché de ${t} en el almacenamiento:`,o)}}import{mkdir as Q,writeFile as G}from"node:fs/promises";import{dirname as H}from"node:path";async function C(t,e){let r=l.get("cache")?.cacheLocation;if(!r)return;let o=c(r),s=H(o);try{Q(s,{recursive:!0}),await G(o,JSON.stringify(e),"utf8")}catch(a){console.error(`Error cacheando ${t} en el almacenamiento:`,a)}}class E{store=new Map;defaultTtl=_(10,"day");async get(t,e=!1){if(!Y(t))throw Error("CacheKey inválida",{cause:t});let r=l.get("cache")?.method??"memory",o=null;if(r==="memory")o=this.store.get(t);if(r==="disk")o=await h(t,e);if(r==="hybrid"){if(o=this.store.get(t),!o)o=await h(t)}if(!o)return null;if(Date.now()-o.timestamp>this.defaultTtl){if(r!=="disk")this.delete(t);if(r==="disk")q(t);return null}return o}async set(t,e){if(!Y(t))throw Error("CacheKey inválida",{cause:t});let r=l.get("cache")?.method??"memory",o={...e,timestamp:Date.now()};if(r==="memory")this.store.set(t,o);if(r==="disk")await C(t,o);if(r==="hybrid")this.store.set(t,o),await C(t,o)}delete(t){this.store.delete(t)}}var T=new E;var X=[18,144,240,360,480,720,1080,1440,2160],v={"yt-dlp":"yt-dlp"},U="%(ytId)s.%(ext)s",M={ID:"%(id)s",YT_ID:"%(ytId)s"};function d(t){return`https://youtube.com/watch?v=${t}`}import{spawn as I}from"node:child_process";import{stdout as Z,stderr as j}from"node:process";function O(t,e){(t==="data"?Z:j).write(e)}function D(t,e,r){let o=v[t];return new Promise((s,a)=>{let p=I(o,e),u="",m="";p.stdout.on("data",(i)=>{u+=i;let f=i.toString();if(r)O("data",f)}),p.stderr.on("data",(i)=>{m+=i;let f=i.toString();if(r)O("err",f)}),p.on("close",(i)=>{if(i===0)s(u.toString());else a(Error(m.toString()))}),p.on("error",(i)=>{a(i)})})}function N(t,e){let r=t;for(let[o,s]of e)r=r.replaceAll(o,s);return r}function B(t){return t.replaceAll("/","⁄")}function P({filename:t,id:e,ytId:r}){let o=new Map([[M.ID,e],[M.YT_ID,r]]),s=N(t,o);return B(s)}class S{async download(t,e,r){let o=this.buildYtDlpArgs(t,e,r);return await D("yt-dlp",o,!0),{duration:-1,path:"unknown"}}buildYtDlpArgs(t,e,r){let{id:o,type:s}=r,a=s==="video",p=r.outputPath,u=P({filename:r.filename,id:r.id,ytId:e}),m="aac";return["-f",o,...a?[]:["-x","--audio-format","aac"],"-o",u,"-P",p,t]}}async function b(t,e){if(!t||!e.id)return;let r=d(t),o=$("video",e);return new S().download(r,t,o)}async function k(t,e){if(!t||!e.id)return;let r=d(t),o=$("audio",e);return new S().download(r,t,o)}function $(t,e){return{id:e.id,type:t,outputPath:e.outputPath??".",filename:e.filename??U}}function z({a:t,b:e,compareResolution:r,type:o}){let s=o==="video"&&r&&t.quality!==null&&e.quality!==null,a=o==="video"&&r&&t.height!==null&&e.height!==null,p=o==="video"&&t.fps!==null&&e.fps!==null,u=o==="audio"&&t.asr!==null&&e.asr!==null,m=t.tbr!==null&&e.tbr!==null,i=0,f=0;if(s){if((t.quality??0)>(e.quality??0))i+=3;else if((t.quality??0)<(e.quality??0))f+=3}if(a){if((t.height??0)>(e.height??0))i+=3;else if((t.height??0)<(e.height??0))f+=3}if(p){if((t.fps??0)>(e.fps??0))i+=2;else if((t.fps??0)<(e.fps??0))f+=2}if(u){if((t.asr??0)>(e.asr??0))i+=2;else if((t.asr??0)<(e.asr??0))f+=2}if(m){if((t.tbr??0)>(e.tbr??0))i+=1;else if((t.tbr??0)<(e.tbr??0))f+=1}return{aScore:i,bScore:f}}function g(t,e,{type:r,compareResolution:o}){if(!t||!e)return;let{aScore:s,bScore:a}=z({a:t,b:e,type:r,compareResolution:o});return s>a?t:e}function x(t,e,{type:r,compareResolution:o}){if(!t||!e)return;let{aScore:s,bScore:a}=z({a:t,b:e,type:r,compareResolution:o});return s>a?e:t}async function tt(t,e){let r=d(t),o=`formats-${t}`,s=Boolean(e.match(/\d/)),a=s?!1:"N/A",p=["--print","%(formats)j",r],m=(await T.get(o))?.content??"";if(!m){let n;try{n=await D("yt-dlp",p)}catch(R){throw console.error("Error consiguiendo el ID del formato"),R}if(typeof n!=="string")throw Error("La salida del proceso es de tipo inválido (se esperaba string)");m=n,T.set(o,{content:m})}let i=[];try{i=JSON.parse(m)}catch(n){throw console.error("Error convirtiendo la salida de yt-dlp a JSON"),n}if(!Array.isArray(i))throw Error("Se esperaba un array");let f=void 0,K=void 0,w=void 0,F=void 0,y=void 0;for(let n of i){let R=n.resolution==="audio only";if(s&&n.format_note===e){a=!0,y=g(y,n,{compareResolution:!1,type:"video"})??n;continue}if(R)w=g(w,n,{compareResolution:!1,type:"audio"})??n,F=x(F,n,{compareResolution:!1,type:"audio"})??n;else f=g(f,n,{compareResolution:!0,type:"video"})??n,K=x(K,n,{compareResolution:!0,type:"video"})??n}if(!s)y={"best-video":f,"worst-video":K,"best-audio":w,"worst-audio":F}[e];return{foundSpecific:a,formatId:y?.format_id,desiredFormat:y}}export{C as saveInDisk,c as resolvePath,P as resolveFilenamePattern,x as getWorstFormat,h as getFromDisk,g as getBetterFormat,d as formYoutubeUrl,tt as findFormatId,N as expandPattern,b as downloadVideo,k as downloadAudio,X as STANDARD_RESOLUTIONS,A as Configuration,E as CacheManager};
@@ -1,10 +1,9 @@
1
1
  import type { MediaType } from '../types/videoTypes';
2
2
  import type { YtDlpFormat } from '../types/ytDlpFormatTypes';
3
- export declare function getBetterFormat(a: YtDlpFormat | undefined, b: YtDlpFormat | undefined, { type, compareResolution }: {
3
+ interface CompareOptions {
4
4
  type: MediaType;
5
5
  compareResolution: boolean;
6
- }): YtDlpFormat | undefined;
7
- export declare function getWorstFormat(a: YtDlpFormat | undefined, b: YtDlpFormat | undefined, { type, compareResolution }: {
8
- type: MediaType;
9
- compareResolution: boolean;
10
- }): YtDlpFormat | undefined;
6
+ }
7
+ export declare function getBetterFormat(a: YtDlpFormat | undefined, b: YtDlpFormat | undefined, { type, compareResolution }: CompareOptions): YtDlpFormat | undefined;
8
+ export declare function getWorstFormat(a: YtDlpFormat | undefined, b: YtDlpFormat | undefined, { type, compareResolution }: CompareOptions): YtDlpFormat | undefined;
9
+ export {};
@@ -1,6 +1,6 @@
1
1
  export declare const STANDARD_RESOLUTIONS: readonly [18, 144, 240, 360, 480, 720, 1080, 1440, 2160];
2
2
  export declare const COMMANDS: {
3
- readonly 'yt-dlp': "yt-dlp-linux";
3
+ readonly 'yt-dlp': "yt-dlp";
4
4
  };
5
5
  export declare const DEFAULT_FILENAME = "%(ytId)s.%(ext)s";
6
6
  export declare const PATTERNS: {
@@ -3,5 +3,5 @@ interface ResolverProps {
3
3
  id: string;
4
4
  ytId: string;
5
5
  }
6
- export declare function resolveFilename({ filename, id, ytId }: ResolverProps): string;
6
+ export declare function resolveFilenamePattern({ filename, id, ytId }: ResolverProps): string;
7
7
  export {};
@@ -0,0 +1 @@
1
+ export declare function resolvePath(path: string): string;
@@ -1,4 +1,4 @@
1
- type TimeUnit = 'day';
1
+ type TimeUnit = 'day' | 'hrs' | 'min' | 'sec' | 'nano';
2
2
  type Miliseconds = number;
3
3
  /**
4
4
  * Recibe una cantidad `amount` y una unidad `unit`, y devuelve esa cantidad en milisegundos.
@@ -13,4 +13,5 @@ type Miliseconds = number;
13
13
  * @returns Devuelve en milisegundos la cantidad de unidad especificada
14
14
  */
15
15
  export declare function timeToMs(amount: number, unit: TimeUnit): Miliseconds;
16
+ export declare function msToTime(ms: number, unit: TimeUnit): number;
16
17
  export {};
@@ -0,0 +1,13 @@
1
+ export type CacheKey = 'formats:' | (string & {});
2
+ export interface CachedData {
3
+ content: string;
4
+ timestamp: number;
5
+ }
6
+ export interface ValueToCache {
7
+ content: string;
8
+ }
9
+ export interface CacheConfig {
10
+ method: CacheMethod;
11
+ cacheLocation?: string;
12
+ }
13
+ export type CacheMethod = 'memory' | 'disk' | 'hybrid';
@@ -0,0 +1,6 @@
1
+ import type { CacheConfig } from './cacheTypes';
2
+ export type ConfigKey = keyof ConfigMap;
3
+ export type ConfigValue<T extends ConfigKey> = ConfigMap[T];
4
+ export type ConfigMap = {
5
+ 'cache': CacheConfig;
6
+ };
@@ -0,0 +1,2 @@
1
+ import type { CacheKey } from '../types/cacheTypes';
2
+ export declare function isValidCacheKey(key: CacheKey): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yt-kit/core",
3
- "version": "0.6.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1 +0,0 @@
1
- export declare function getFromCache(key: string): import("./CacheManager").CachedData | null;
@@ -1,2 +0,0 @@
1
- import { type CachedData } from './CacheManager';
2
- export declare function saveInCache(key: string, value: CachedData): void;