-
Notifications
You must be signed in to change notification settings - Fork 3
/
dump-account.js
140 lines (117 loc) · 4.06 KB
/
dump-account.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
133
134
135
136
137
138
139
140
/* eslint-disable camelcase */
require('dotenv').config({ path: '.env' })
const fs = require('fs')
const path = require('path')
const axios = require('axios')
// const Signer = require('./lib/signer')
const { downloadMedia, sleep } = require('./lib/utils')
const UA = process.env.USERAGENT
const COOKIES = process.env.COOKIES
const precheck = async (username) => {
if (!COOKIES) {
console.log('[warn] No cookies have been supplied')
console.log('[warn] Press Ctrl+C and edit .env if you need cookies')
await sleep(3)
}
if (!fs.existsSync(path.join(__dirname, 'downloads/'))) fs.mkdirSync(path.join(__dirname, 'downloads/'))
if (!fs.existsSync(path.join(__dirname, `downloads/${username}/`))) fs.mkdirSync(path.join(__dirname, `downloads/${username}/`))
}
const dumpUserPosts = async (username, cursor = 0) => {
// const signer = new Signer(UA)
let secuid = username
if (cursor === 0) {
cursor = new Date().getTime()
precheck(username)
}
// eslint-disable-next-line no-negated-condition
if (!username.startsWith('MS4wLjABAAAA')) {
const userAxiosConfig = {
method: 'GET',
url: `https://api.tik.fail/v2/search/user?q=${username}`,
headers: {
'User-Agent': 'TikTokScripts/dump-user (https://github.com/tikfail/tiktok-scripts)'
},
responseType: 'json',
responseEncoding: 'utf8'
}
const { data } = await axios(userAxiosConfig)
const meta = data.data[0]
secuid = meta.sec_uid
console.log(meta)
if (!data.success || !secuid.startsWith('MS4wLjABAAAA')) {
console.log('[error] Couldn\'t get user sec_uid')
console.log(meta)
process.exit(1)
}
}
const tiktokAxiosConfig = {
method: 'GET',
url: 'https://www.tiktok.com/api/creator/item_list/',
headers: {
'Origin': 'https://www.tiktok.com',
'User-Agent': UA,
'Cookie': COOKIES
},
params: {
aid: 1988,
count: 15,
cursor: cursor,
secUid: secuid,
type: 1,
verifyFp: 'verify_'
},
responseType: 'json',
responseEncoding: 'utf8'
}
const { data } = await axios(tiktokAxiosConfig)
if (!data.itemList) {
console.log(`[warn] Got hasMore but itemList is empty | ${cursor}`)
console.log(data)
return process.exit(1)
}
const total = data.itemList.length
const nextCursor = Math.floor(Number(data.itemList.at(-1).createTime) * 1000)
console.log(`[----] Got ${total} Total Items | hasMore: ${data.hasMorePrevious}`)
for (let i = 0; i < total; ++i) {
const v = data.itemList[i]
const vid = v.id
// Write Metadata
fs.writeFileSync(path.join(__dirname, `downloads/${v.author.uniqueId}/${vid}.json`), JSON.stringify(v, null, 2))
// Slideshows
if (v.imagePost) {
const totalImages = v.imagePost.images.length
for (let img = 0; img < totalImages; img++) {
const image = v.imagePost.images[img].imageURL.urlList[0]
await downloadMedia(image, `downloads/${v.author.uniqueId}/${vid}-${String(img + 1).padStart(2, '0')}.jpg`, COOKIES)
}
console.log(`[image] ${vid} - ${totalImages} Images`)
continue
}
// Video
try {
await downloadMedia(v.video.playAddr, `downloads/${v.author.uniqueId}/${vid}.mp4`, COOKIES)
console.log(`[video] ${vid}`)
} catch (error) {
console.log(`[error] Failed to download video: ${vid}`)
console.log(error)
}
// Video Thumbnails
if (process.env.THUMBNAILS) {
await downloadMedia(v.video.dynamicCover, `downloads/${v.author.uniqueId}/${vid}.webp`, COOKIES)
await downloadMedia(v.video.cover, `downloads/${v.author.uniqueId}/${vid}.jpg`, COOKIES)
}
}
if (data.hasMorePrevious) {
console.log(`=========================[ Cursor: ${cursor} --> ${nextCursor} | hasMore: ${data.hasMorePrevious} ]=========================`)
return dumpUserPosts(secuid, nextCursor)
}
console.log('[info] Done')
return process.exit(0)
}
if (require.main === module) {
if (process.argv[2]) {
dumpUserPosts(process.argv[2])
} else {
console.log('[error] Usage: node dump-account.sh <username>')
}
}