@pieda/video-dl 1.0.0 → 1.1.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.
@@ -1 +1,2 @@
1
1
  export declare const importModule: (moduleName: string) => Promise<unknown>;
2
+ export declare const downloadFileFromUrl: (url: string, filename: string) => Promise<void>;
@@ -0,0 +1,4 @@
1
+ export declare const useLogger: (debug?: boolean) => {
2
+ log: (...args: any[]) => void;
3
+ error: (...args: any[]) => void;
4
+ };
@@ -1,6 +1,7 @@
1
1
  type TUseYoutube = {
2
2
  videoId: string;
3
3
  dest: string;
4
+ debug?: boolean;
4
5
  };
5
- export declare const useYoutube: ({ videoId, dest }: TUseYoutube) => Promise<void>;
6
+ export declare const useYoutube: ({ videoId, dest, debug }: TUseYoutube) => Promise<void>;
6
7
  export {};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=o=>new Promise((e,c)=>{import(o).then(t=>e(t.default)).catch(c)}),$=async(o,e)=>{const c=await i("fs"),t=await i("axios");return new Promise((a,n)=>{t.get(o,{responseType:"stream"}).then(r=>{const l=r.data,s=c.createWriteStream(e);l.pipe(s),s.on("finish",function(){s.close(()=>{a()})})}).catch(n)})},v=(o=!1)=>({log:(...e)=>{o&&console.log(...e)},error:(...e)=>{o&&console.error(...e)}}),k=async({videoId:o,dest:e,debug:c=!1})=>{const t=v(c);t.log(`Downloading video: ${o} to: ${e}`);const a=`https://www.youtube.com/watch?v=${o}`,n=await i("fs"),r=await i("path"),l=await i("@distube/ytdl-core");return n.existsSync(r.dirname(e))||n.mkdirSync(r.dirname(e),{recursive:!0},777),new Promise((s,u)=>{l(a).pipe(n.createWriteStream(e)).on("finish",()=>{t.log("Downloading finished."),s()})})},S=async({videoId:o,dest:e,debug:c=!1})=>{const t=v(c);t.log(`Downloading video: ${o} to: ${e}`);const a=`https://m.facebook.com/watch/?v=${o}`,n=await i("fs"),r=await i("path"),l=await i("puppeteer");n.existsSync(r.dirname(e))||(n.mkdirSync(r.dirname(e),{recursive:!0},777),t.log(`建立 ${r.dirname(e)}`));const s=await l.launch();t.log("啟動 puppeteer");const u=await s.newPage();await u.setUserAgent("'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"),t.log("開新分頁"),await u.goto(a,{waitUntil:"networkidle0"}),t.log(`前往 ${a}`),await u.waitForSelector("video"),t.log("找到 video 元素");const w=await u.$eval("video",p=>p.getAttribute("src"));t.log(`取得 video url: ${w}`),await s.close(),t.log("關閉 puppeteer"),await $(w,e),t.log(`下載完成 ${w}`)},P=async({videoId:o,dest:e,downloadBtnSelector:c,debug:t=!1})=>{const a=v(t);a.log(`Downloading video: ${o} to: ${e}`);const n=`https://vimeo.com/${o}`,r=await i("fs"),l=await i("path"),s=await i("axios"),u=await i("puppeteer"),w=async p=>{var m;const y=await s.get(p,{responseType:"json",headers:{"X-Requested-With":"XMLHttpRequest"}}),d=Array.from(((m=y.data)==null?void 0:m.files)||[]);return d[d.length-1].download_url};return r.existsSync(l.dirname(e))||(r.mkdirSync(l.dirname(e),{recursive:!0},777),a.log(`建立 ${l.dirname(e)}`)),new Promise(async(p,y)=>{const d=await u.launch({headless:!1,defaultViewport:{width:1920,height:1080}});a.log("啟動 puppeteer");const g=await d.newPage();a.log("開新分頁"),await g.setRequestInterception(!0),g.on("request",async m=>{const h=m.url();if(h.indexOf("action=load_download_config")>-1){a.log(`攔截下載請求 ${h}`);const b=await w(h);a.log(`正在開始下載 ${b}`),await $(b,e),a.log(`下載完成 ${e}`),await d.close(),a.log("關閉 puppeteer"),p();return}m.continue()}),await g.goto(n,{waitUntil:"networkidle0"}),a.log(`前往 ${n}`);const f=c||"#main > div > main > div > div > div > div._1cCXb.sc-kEmuub.bmHlZQ > div._3oHHF.sc-bbkauy.fmFDPY > div > button:nth-child(1)";await g.waitForSelector(f),a.log(`找到下載按鈕 ${f}`),await g.click(f),a.log("點擊下載按鈕")})},U=({type:o})=>{switch(o){case"youtube":return k;case"facebook":return S;case"vimeo":return P;default:throw new Error(`Invalid video type: ${o}`)}};exports.useDownloader=U;exports.useFacebook=S;exports.useVimeo=P;exports.useYoutube=k;
@@ -0,0 +1,110 @@
1
+ const i = (t) => new Promise((e, c) => {
2
+ import(t).then((o) => e(o.default)).catch(c);
3
+ }), b = async (t, e) => {
4
+ const c = await i("fs"), o = await i("axios");
5
+ return new Promise((a, n) => {
6
+ o.get(t, { responseType: "stream" }).then((r) => {
7
+ const l = r.data, s = c.createWriteStream(e);
8
+ l.pipe(s), s.on("finish", function() {
9
+ s.close(() => {
10
+ a();
11
+ });
12
+ });
13
+ }).catch(n);
14
+ });
15
+ }, v = (t = !1) => ({
16
+ log: (...e) => {
17
+ t && console.log(...e);
18
+ },
19
+ error: (...e) => {
20
+ t && console.error(...e);
21
+ }
22
+ }), k = async ({ videoId: t, dest: e, debug: c = !1 }) => {
23
+ const o = v(c);
24
+ o.log(`Downloading video: ${t} to: ${e}`);
25
+ const a = `https://www.youtube.com/watch?v=${t}`, n = await i("fs"), r = await i("path"), l = await i("@distube/ytdl-core");
26
+ return n.existsSync(r.dirname(e)) || n.mkdirSync(r.dirname(e), { recursive: !0 }, 777), new Promise((s, w) => {
27
+ l(a).pipe(n.createWriteStream(e)).on("finish", () => {
28
+ o.log("Downloading finished."), s();
29
+ });
30
+ });
31
+ }, S = async ({
32
+ videoId: t,
33
+ dest: e,
34
+ debug: c = !1
35
+ }) => {
36
+ const o = v(c);
37
+ o.log(`Downloading video: ${t} to: ${e}`);
38
+ const a = `https://m.facebook.com/watch/?v=${t}`, n = await i("fs"), r = await i("path"), l = await i("puppeteer");
39
+ n.existsSync(r.dirname(e)) || (n.mkdirSync(r.dirname(e), { recursive: !0 }, 777), o.log(`建立 ${r.dirname(e)}`));
40
+ const s = await l.launch();
41
+ o.log("啟動 puppeteer");
42
+ const w = await s.newPage();
43
+ await w.setUserAgent(
44
+ "'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"
45
+ ), o.log("開新分頁"), await w.goto(a, {
46
+ waitUntil: "networkidle0"
47
+ }), o.log(`前往 ${a}`), await w.waitForSelector("video"), o.log("找到 video 元素");
48
+ const u = await w.$eval("video", (g) => g.getAttribute("src"));
49
+ o.log(`取得 video url: ${u}`), await s.close(), o.log("關閉 puppeteer"), await b(u, e), o.log(`下載完成 ${u}`);
50
+ }, P = async ({
51
+ videoId: t,
52
+ dest: e,
53
+ downloadBtnSelector: c,
54
+ debug: o = !1
55
+ }) => {
56
+ const a = v(o);
57
+ a.log(`Downloading video: ${t} to: ${e}`);
58
+ const n = `https://vimeo.com/${t}`, r = await i("fs"), l = await i("path"), s = await i("axios"), w = await i("puppeteer"), u = async (g) => {
59
+ var m;
60
+ const y = await s.get(g, {
61
+ responseType: "json",
62
+ headers: {
63
+ "X-Requested-With": "XMLHttpRequest"
64
+ }
65
+ }), d = Array.from(((m = y.data) == null ? void 0 : m.files) || []);
66
+ return d[d.length - 1].download_url;
67
+ };
68
+ return r.existsSync(l.dirname(e)) || (r.mkdirSync(l.dirname(e), { recursive: !0 }, 777), a.log(`建立 ${l.dirname(e)}`)), new Promise(async (g, y) => {
69
+ const d = await w.launch({
70
+ headless: !1,
71
+ defaultViewport: {
72
+ width: 1920,
73
+ height: 1080
74
+ }
75
+ });
76
+ a.log("啟動 puppeteer");
77
+ const p = await d.newPage();
78
+ a.log("開新分頁"), await p.setRequestInterception(!0), p.on("request", async (m) => {
79
+ const h = m.url();
80
+ if (h.indexOf("action=load_download_config") > -1) {
81
+ a.log(`攔截下載請求 ${h}`);
82
+ const $ = await u(h);
83
+ a.log(`正在開始下載 ${$}`), await b($, e), a.log(`下載完成 ${e}`), await d.close(), a.log("關閉 puppeteer"), g();
84
+ return;
85
+ }
86
+ m.continue();
87
+ }), await p.goto(n, {
88
+ waitUntil: "networkidle0"
89
+ }), a.log(`前往 ${n}`);
90
+ const f = c || "#main > div > main > div > div > div > div._1cCXb.sc-kEmuub.bmHlZQ > div._3oHHF.sc-bbkauy.fmFDPY > div > button:nth-child(1)";
91
+ await p.waitForSelector(f), a.log(`找到下載按鈕 ${f}`), await p.click(f), a.log("點擊下載按鈕");
92
+ });
93
+ }, x = ({ type: t }) => {
94
+ switch (t) {
95
+ case "youtube":
96
+ return k;
97
+ case "facebook":
98
+ return S;
99
+ case "vimeo":
100
+ return P;
101
+ default:
102
+ throw new Error(`Invalid video type: ${t}`);
103
+ }
104
+ };
105
+ export {
106
+ x as useDownloader,
107
+ S as useFacebook,
108
+ P as useVimeo,
109
+ k as useYoutube
110
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@pieda/video-dl",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
+ "main": "dist/video-dl.cjs",
7
+ "module": "dist/video-dl.js",
6
8
  "files": [
7
9
  "dist"
8
10
  ],
@@ -19,8 +21,9 @@
19
21
  ".": {
20
22
  "import": {
21
23
  "types": "./dist/types/library.d.ts",
22
- "default": "./dist/video-dl.es.js"
23
- }
24
+ "default": "./dist/video-dl.js"
25
+ },
26
+ "require": "./dist/video-dl.cjs"
24
27
  }
25
28
  },
26
29
  "scripts": {
@@ -1,107 +0,0 @@
1
- const o = (i) => new Promise((t, d) => {
2
- import(i).then((n) => t(n.default)).catch(d);
3
- }), $ = async ({ videoId: i, dest: t }) => {
4
- const d = `https://www.youtube.com/watch?v=${i}`, n = await o("fs"), s = await o("path"), c = await o("@distube/ytdl-core");
5
- return n.existsSync(s.dirname(t)) || n.mkdirSync(s.dirname(t), { recursive: !0 }, 777), new Promise((m, y) => {
6
- c(d).pipe(n.createWriteStream(t)).on("finish", () => {
7
- m();
8
- });
9
- });
10
- }, k = async ({
11
- videoId: i,
12
- dest: t,
13
- debug: d = !1
14
- }) => {
15
- const n = `https://m.facebook.com/watch/?v=${i}`, s = await o("fs"), c = await o("path"), m = await o("axios"), y = await o("puppeteer"), g = (r, l) => new Promise((w, f) => {
16
- m.get(r, { responseType: "stream" }).then((u) => {
17
- const p = u.data, v = s.createWriteStream(l);
18
- p.pipe(v), v.on("finish", function() {
19
- v.close(() => {
20
- w();
21
- });
22
- });
23
- });
24
- }), e = (...r) => {
25
- d && console.log(...r);
26
- };
27
- s.existsSync(c.dirname(t)) || (s.mkdirSync(c.dirname(t), { recursive: !0 }, 777), e(`建立 ${c.dirname(t)}`));
28
- const b = await y.launch();
29
- e("啟動 puppeteer");
30
- const h = await b.newPage();
31
- await h.setUserAgent(
32
- "'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"
33
- ), e("開新分頁"), await h.goto(n, {
34
- waitUntil: "networkidle0"
35
- }), e(`前往 ${n}`), await h.waitForSelector("video"), e("找到 video 元素");
36
- const a = await h.$eval("video", (r) => r.getAttribute("src"));
37
- e(`取得 video url: ${a}`), await b.close(), e("關閉 puppeteer"), await g(a, t), e(`下載完成 ${a}`);
38
- }, S = async ({
39
- videoId: i,
40
- dest: t,
41
- downloadBtnSelector: d,
42
- debug: n = !1
43
- }) => {
44
- const s = `https://vimeo.com/${i}`, c = await o("fs"), m = await o("path"), y = await o("axios"), g = await o("puppeteer"), e = (...a) => {
45
- n && console.log(...a);
46
- }, b = async (a) => {
47
- var u;
48
- const r = await y.get(a, {
49
- responseType: "json",
50
- headers: {
51
- "X-Requested-With": "XMLHttpRequest"
52
- }
53
- }), l = Array.from(((u = r.data) == null ? void 0 : u.files) || []);
54
- return l[l.length - 1].download_url;
55
- }, h = (a, r) => new Promise((l, w) => {
56
- y.get(a, { responseType: "stream" }).then((f) => {
57
- const u = f.data, p = c.createWriteStream(r);
58
- u.pipe(p), p.on("finish", function() {
59
- p.close(() => {
60
- l();
61
- });
62
- });
63
- });
64
- });
65
- return c.existsSync(m.dirname(t)) || (c.mkdirSync(m.dirname(t), { recursive: !0 }, 777), e(`建立 ${m.dirname(t)}`)), new Promise(async (a, r) => {
66
- const l = await g.launch({
67
- headless: !1,
68
- defaultViewport: {
69
- width: 1920,
70
- height: 1080
71
- }
72
- });
73
- e("啟動 puppeteer");
74
- const w = await l.newPage();
75
- e("開新分頁"), await w.setRequestInterception(!0), w.on("request", async (u) => {
76
- const p = u.url();
77
- if (p.indexOf("action=load_download_config") > -1) {
78
- e(`攔截下載請求 ${p}`);
79
- const v = await b(p);
80
- e(`正在開始下載 ${v}`), await h(v, t), e(`下載完成 ${t}`), await l.close(), e("關閉 puppeteer"), a();
81
- return;
82
- }
83
- u.continue();
84
- }), await w.goto(s, {
85
- waitUntil: "networkidle0"
86
- }), e(`前往 ${s}`);
87
- const f = d || "#main > div > main > div > div > div > div._1cCXb.sc-kEmuub.bmHlZQ > div._3oHHF.sc-bbkauy.fmFDPY > div > button:nth-child(1)";
88
- await w.waitForSelector(f), e(`找到下載按鈕 ${f}`), await w.click(f), e("點擊下載按鈕");
89
- });
90
- }, P = ({ type: i }) => {
91
- switch (i) {
92
- case "youtube":
93
- return $;
94
- case "facebook":
95
- return k;
96
- case "vimeo":
97
- return S;
98
- default:
99
- throw new Error(`Invalid video type: ${i}`);
100
- }
101
- };
102
- export {
103
- P as useDownloader,
104
- k as useFacebook,
105
- S as useVimeo,
106
- $ as useYoutube
107
- };