@pieda/video-dl 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 piedasing
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @pieda/video-dl
2
+
3
+ ```bash
4
+ npm install @pieda/video-dl
5
+ ```
6
+
7
+ ```js
8
+ import { useDownloader } from '@pieda/video-dl';
9
+
10
+ const ytdl = useDownloader({ type: 'youtube' });
11
+ await ytdl({
12
+ videoType: 'youtube',
13
+ videoId: '<videoId>',
14
+ dest: '<path to download>',
15
+ debug: false,
16
+ });
17
+ ```
18
+
19
+ ```js
20
+ import { useYoutube } from '@pieda/video-dl';
21
+
22
+ await useYoutube({
23
+ videoId: '<videoId>',
24
+ dest: '<path to download>',
25
+ debug: false,
26
+ });
27
+ ```
@@ -0,0 +1 @@
1
+ export declare const importModule: (moduleName: string) => Promise<unknown>;
@@ -0,0 +1,4 @@
1
+ export { useDownloader } from './useDownloader';
2
+ export { useYoutube } from './useYoutube';
3
+ export { useFacebook } from './useFacebook';
4
+ export { useVimeo } from './useVimeo';
@@ -0,0 +1,9 @@
1
+ import { useYoutube } from './useYoutube';
2
+ import { useFacebook } from './useFacebook';
3
+ import { useVimeo } from './useVimeo';
4
+ type TUseDownloader = {
5
+ type: 'youtube' | 'facebook' | 'vimeo';
6
+ };
7
+ type TUseDownloaderResponse = typeof useYoutube | typeof useFacebook | typeof useVimeo;
8
+ export declare const useDownloader: ({ type }: TUseDownloader) => TUseDownloaderResponse;
9
+ export {};
@@ -0,0 +1,7 @@
1
+ type TUseFacebook = {
2
+ videoId: string;
3
+ dest: string;
4
+ debug?: boolean;
5
+ };
6
+ export declare const useFacebook: ({ videoId, dest, debug, }: TUseFacebook) => Promise<void>;
7
+ export {};
@@ -0,0 +1,8 @@
1
+ type TUseVimeo = {
2
+ videoId: string;
3
+ dest: string;
4
+ downloadBtnSelector?: string;
5
+ debug?: boolean;
6
+ };
7
+ export declare const useVimeo: ({ videoId, dest, downloadBtnSelector, debug, }: TUseVimeo) => Promise<void>;
8
+ export {};
@@ -0,0 +1,6 @@
1
+ type TUseYoutube = {
2
+ videoId: string;
3
+ dest: string;
4
+ };
5
+ export declare const useYoutube: ({ videoId, dest }: TUseYoutube) => Promise<void>;
6
+ export {};
@@ -0,0 +1 @@
1
+ export { useDownloader, useYoutube, useFacebook, useVimeo } from './composables';
@@ -0,0 +1,107 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@pieda/video-dl",
3
+ "private": false,
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/piedasing/-pieda-video-dl.git"
12
+ },
13
+ "author": "piedasing",
14
+ "license": "MIT",
15
+ "engines": {
16
+ "node": ">=18"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/types/library.d.ts",
22
+ "default": "./dist/video-dl.es.js"
23
+ }
24
+ }
25
+ },
26
+ "scripts": {
27
+ "dev": "vite",
28
+ "build": "vue-tsc && vite build && tsc --declaration --emitDeclarationOnly",
29
+ "preview": "vite preview",
30
+ "prepare": "npm run build",
31
+ "version:major": "npm version major",
32
+ "version:minor": "npm version minor",
33
+ "version:patch": "npm version patch"
34
+ },
35
+ "dependencies": {
36
+ "@distube/ytdl-core": "^4.13.5",
37
+ "axios": "^1.7.2",
38
+ "puppeteer": "^22.13.1"
39
+ },
40
+ "devDependencies": {
41
+ "fs": "^0.0.1-security",
42
+ "path": "^0.12.7",
43
+ "typescript": "^5.0.2",
44
+ "vite": "^5.0.12",
45
+ "vue-tsc": "^1.4.2"
46
+ },
47
+ "peerDependencies": {
48
+ "@distube/ytdl-core": "^4.13.5",
49
+ "axios": "^1.7.2",
50
+ "puppeteer": "^22.13.1"
51
+ }
52
+ }