This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (123 loc) · 4.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const { webkit } = require('playwright');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { nanoid } = require('nanoid');
const express = require('express')
const app = express()
const port = 3000
const fs = require('fs').promises;
const ytDlp = './yt-dlp';
const fse = require('fs-extra');
let gettingVideos = false;
(async () => {
const glent = await fs.readdir('./videos/douyin/');
if (Array.isArray(glent) && glent.length) {
} else
await getVideos({ douyin: true });
const kobyakov = await fs.readdir('./videos/bilibili/');
if (Array.isArray(kobyakov) && kobyakov.length) {
} else
await getVideos({ douyin: false });
setInterval(async () => {
await fse.emptyDir('videos/bilibili');
await fse.emptyDir('videos/douyin');
await getVideos({ douyin: false });
await getVideos({ douyin: true });
}, 86400000)
})();
async function getVideos(settings) {
gettingVideos = true;
if (settings.douyin == true) {
console.log('[DOUYIN]')
console.log('Launching Webkit...');
const browser = await webkit.launch();
console.log("Opening Tian Yiming's Douyin account & waiting for network idle...");
const page = await browser.newPage();
await page.goto('https://www.douyin.com/user/MS4wLjABAAAA1EfwKVjULgmlYNqkJXnpHo_OEVwAyHqDj322ZV_UF7sd2X_CAMc2BAGPPpEttglX', { waitUntil: 'networkidle', timeout: 0 });
let locator = await page.locator('a.B3AsdZT9.chmb2GX8.UwG3qaZV');
for (let i = 0; i < await locator.count(); i++) {
console.log(`Downloading video #${i + 1}...`);
const url = await locator.nth(i).getAttribute('href');
try {
const { stdout, stderr } = await exec(`${ytDlp} https:${url} --cookies-from-browser firefox -o "videos/douyin/${nanoid()}.mp4"`);
console.log(stdout, stderr)
} catch {
try {
const { stdout, stderr } = await exec(`${ytDlp} https:${url} --cookies-from-browser firefox -o "videos/douyin/${nanoid()}.mp4"`);
console.log(stdout, stderr)
} catch {}
}
}
console.log('Closing the browser...');
await browser.close();
} else {
console.log('[BILIBILI]')
console.log('Launching Webkit...');
const browser = await webkit.launch();
console.log("Opening Tian Yiming's BiliBili account & waiting for network idle...");
const page = await browser.newPage();
await page.goto('https://space.bilibili.com/477676711', { waitUntil: 'networkidle', timeout: 0 });
let locator = await page.locator('a.cover');
for (let i = 0; i < await locator.count(); i++) {
console.log(`Downloading video #${i + 1}...`);
const url = await locator.nth(i).getAttribute('href');
try {
const { stdout, stderr } = await exec(`${ytDlp} ${url} -o "videos/bilibili/${nanoid()}.mp4" -S "codec:avc"`);
console.log(stdout, stderr)
} catch {
try {
const { stdout, stderr } = await exec(`${ytDlp} ${url} -o "videos/bilibili/${nanoid()}.mp4" -S "codec:avc"`);
console.log(stdout, stderr)
} catch {}
}
}
console.log('Closing the browser...');
await browser.close();
}
gettingVideos = false
}
/*process.on('SIGINT', async () => {
console.log('Closing the browser...');
await browser.close();
process.exit();
});*/
// Server
app.get('/douyin.mp4', async (req, res) => {
res.type('mp4');
if (gettingVideos) {
res.status(503);
res.send()
} else {
const videos = await fs.readdir('./videos/douyin/');
console.log(videos);
const videoPath = `./videos/douyin/${videos[Math.floor(Math.random() * (videos.length - 1))]}`;
const videoSize = await getFileSize(videoPath);
const video = await fs.readFile(videoPath);
res.set('Content-Length', videoSize);
res.send(video)
}
})
app.get('/bilibili.mp4', async (req, res) => {
res.type('mp4');
if (gettingVideos) {
res.status(503);
res.send()
} else {
const videos = await fs.readdir('./videos/bilibili/');
console.log(videos);
const videoPath = `./videos/bilibili/${videos[Math.floor(Math.random() * (videos.length - 1))]}`;
const videoSize = await getFileSize(videoPath);
const video = await fs.readFile(videoPath);
res.set('Content-Length', videoSize);
res.send(video)
}
})
app.listen(port, () => {
console.log('Server listening')
})
app.use(express.static('static'));
async function getFileSize(filename) {
const stats = await fs.stat(filename);
const fileSizeInBytes = stats.size;
return fileSizeInBytes;
}