-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (55 loc) · 2.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
import { mkdirSync, writeFileSync } from 'fs';
import fetch from 'node-fetch';
import { argv } from 'process';
import puppeteer from 'puppeteer';
import { DOWNLOADS_PATH } from './constants.js';
const account = argv[2];
account ?? process.exit(console.log('Please supply an account name.'));
const accountData = [];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`https://www.tiktok.com/@${account}`);
const videoLinks = await page.$$eval(`a`, e => e.map(v => v.getAttribute('href')).filter(v => /^https:\/\/www\.tiktok\.com\/@.+\/video\/\d+$/.test(v)));
// videoLinks.splice(2);
console.log(`${videoLinks.length} videos found.`)
mkdirSync(DOWNLOADS_PATH, { recursive: true });
for (const link of videoLinks) {
console.log(link);
}
for (const link of videoLinks) {
try {
const videoId = link.replace(/^.*\/(\d+)$/g, (u, v) => v);
// console.log(videoId);
const videoPage = await browser.newPage();
await videoPage.goto(link);
const videoFileName = `${account}_${videoId}.mp4`
console.log(`Downloading ${videoFileName}...`)
const videoUrl = await videoPage.$eval(`video`, e => e.getAttribute('src'));
const likeCount = await videoPage.$eval(`strong[data-e2e="like-count"]`, e => e.textContent);
const commentCount = await videoPage.$eval(`strong[data-e2e="comment-count"]`, e => e.textContent);
const shareCount = await videoPage.$eval(`strong[data-e2e="share-count"]`, e => e.textContent);
const videoDescription = await videoPage.$eval(`div[data-e2e="browse-video-desc"]`, e => e.textContent);
const publicationDate = await videoPage.$eval(`span[data-e2e="browser-nickname"] span:nth-child(2)`, e => e.textContent);
const videoData = {
username: account,
videoFileName: videoFileName,
pageUrl: link,
videoUrl: videoUrl,
likeCount: likeCount,
commentCount: commentCount,
shareCount: shareCount,
videoDescription: videoDescription,
publicationDate: publicationDate,
};
// console.log(videoData);
accountData.push(videoData);
writeFileSync(`${DOWNLOADS_PATH}/${account}_data.json`, JSON.stringify(accountData));
await videoPage.close();
const videoBytes = await fetch(videoUrl).then(res => res.arrayBuffer()).then(arrayBuffer => Buffer.from(arrayBuffer));
writeFileSync(`${DOWNLOADS_PATH}/${videoFileName}`, videoBytes);
console.log(`Done.`)
} catch {
console.log(`${link}: failure`);
}
}
await browser.close();