@yt-kit/core 1.0.2 → 1.0.4
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 +4 -2
- package/dist/index.js +1 -1
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -16,10 +16,11 @@ export type CacheMethod = "memory" | "disk" | "hybrid";
|
|
|
16
16
|
export declare class CacheManager {
|
|
17
17
|
private store;
|
|
18
18
|
private defaultTtl;
|
|
19
|
-
get(key: CacheKey
|
|
19
|
+
get(key: CacheKey): Promise<CachedData | null>;
|
|
20
20
|
set(key: CacheKey, value: ValueToCache): Promise<void>;
|
|
21
21
|
delete(key: string): void;
|
|
22
22
|
}
|
|
23
|
+
export declare const cache: CacheManager;
|
|
23
24
|
export type ConfigKey = keyof ConfigMap;
|
|
24
25
|
export type ConfigValue<T extends ConfigKey> = ConfigMap[T];
|
|
25
26
|
export type ConfigMap = {
|
|
@@ -31,6 +32,7 @@ export declare class Configuration {
|
|
|
31
32
|
set<K extends ConfigKey>(key: K, value: ConfigValue<K>): void;
|
|
32
33
|
delete<K extends ConfigKey>(key: K): void;
|
|
33
34
|
}
|
|
35
|
+
export declare const config: Configuration;
|
|
34
36
|
export interface DownloadOptions {
|
|
35
37
|
id: string;
|
|
36
38
|
outputPath?: string;
|
|
@@ -130,7 +132,7 @@ export declare function findFormatId(ytId: string, formatToFind: FormatsToFind):
|
|
|
130
132
|
formatId: string | undefined;
|
|
131
133
|
desiredFormat: YtDlpFormat | undefined;
|
|
132
134
|
}>;
|
|
133
|
-
export declare function getFromDisk(key: CacheKey
|
|
135
|
+
export declare function getFromDisk(key: CacheKey): Promise<CachedData | undefined>;
|
|
134
136
|
export declare function saveInDisk(key: CacheKey, value: CachedData): Promise<void>;
|
|
135
137
|
export declare function formYoutubeUrl(ytId: string): string;
|
|
136
138
|
export interface CompareOptions {
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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 T(t){let e=l.get("cache")?.cacheLocation;if(!e)return;let r=c(e),o;try{o=await W(r,"utf8")}catch{}if(o)try{o=JSON.parse(o)}catch(s){console.error(`Error convirtiendo la caché de ${t} a JSON:`,s)}if(!o)return;return o}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){if(!Y(t))throw Error("CacheKey inválida",{cause:t});let e=l.get("cache")?.method??"memory",r=null;if(e==="memory")r=this.store.get(t);if(e==="disk")r=await T(t);if(e==="hybrid"){if(r=this.store.get(t),!r)r=await T(t)}if(!r)return null;if(Date.now()-r.timestamp>this.defaultTtl){if(e!=="disk")this.delete(t);if(e==="disk")q(t);return null}return r}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 g=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 P(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 N({filename:t,id:e,ytId:r}){let o=new Map([[M.ID,e],[M.YT_ID,r]]),s=P(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=N({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 h(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 g.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,g.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 que los formatos fuesen un array. Puede que el contenido cacheado esté corrupto o sea inválido.");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=h(y,n,{compareResolution:!1,type:"video"})??n;continue}if(R)w=h(w,n,{compareResolution:!1,type:"audio"})??n,F=x(F,n,{compareResolution:!1,type:"audio"})??n;else f=h(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,N as resolveFilenamePattern,x as getWorstFormat,T as getFromDisk,h as getBetterFormat,d as formYoutubeUrl,tt as findFormatId,P as expandPattern,b as downloadVideo,k as downloadAudio,l as config,g as cache,X as STANDARD_RESOLUTIONS,A as Configuration,E as CacheManager};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yt-kit/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"start": "bun pruebas.ts",
|
|
24
24
|
"build": "bun build.ts && dts-bundle-generator --no-check -o dist/index.d.ts src/index.ts",
|
|
25
|
+
"build:debug": "tsc -p tsconfig.build.json",
|
|
25
26
|
"prepublishOnly": "bun run build"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|