This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
forked from carcabot/tiktok-signature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (119 loc) · 3.27 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
133
134
135
136
137
138
139
140
141
const { webkit, devices } = require("playwright-webkit");
const iPhone11 = devices["iPhone 11 Pro"];
class Signer {
userAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
args = [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-infobars",
"--window-position=0,0",
"--ignore-certifcate-errors",
"--ignore-certifcate-errors-spki-list",
];
constructor(userAgent, tac, browser) {
if (userAgent) {
this.userAgent = userAgent;
}
if (tac) {
this.tac = tac;
}
if (browser) {
this.browser = browser;
this.isExternalBrowser = true;
}
this.args.push(`--user-agent="${this.userAgent}"`);
this.options = {
args: [],
ignoreDefaultArgs: ["--mute-audio", "--hide-scrollbars"],
headless: true,
ignoreHTTPSErrors: true,
};
}
async init() {
if (!this.browser) {
this.browser = await webkit.launch(this.options);
}
let emulateTemplate = { ...iPhone11 };
emulateTemplate.viewport.width = getRandomInt(320, 1920);
emulateTemplate.viewport.height = getRandomInt(320, 1920);
this.context = await this.browser.newContext({
...emulateTemplate,
deviceScaleFactor: getRandomInt(1, 3),
isMobile: Math.random() > 0.5,
hasTouch: Math.random() > 0.5,
userAgent: this.userAgent,
});
this.page = await this.context.newPage();
await this.page.goto("https://www.tiktok.com/@rihanna?lang=en", {
waitUntil: "load",
});
// Uncomment the following line for unwanted audio
// await this.page.click(".swiper-wrapper");
if (this.tac) {
await this.page.evaluate((x) => {
window.tac = x;
}, this.tac);
}
await this.page.evaluate(() => {
if (typeof window.byted_acrawler.sign !== "function") {
throw "No function found";
}
window.generateSignature = function generateSignature(
url,
verifyFp = null
) {
let newUrl = url;
if (verifyFp) {
newUrl = newUrl + "&verifyFp=" + verifyFp;
}
return window.byted_acrawler.sign({ url: newUrl });
};
}, this.tac);
return this;
}
async sign(str) {
let verifyFp = await this.getVerifyFp();
let res = await this.page.evaluate(
`generateSignature("${str}", "${verifyFp}")`
);
return res;
}
async getVerifyFp() {
var content = await this.context.cookies();
for (let cookie of content) {
if (cookie.name == "s_v_web_id") {
return cookie.value;
}
}
return null;
}
async getCookie(str) {
var content = await this.context.cookies();
for (let cookie of content) {
if (cookie.name == str) {
return cookie.value;
}
}
return null;
}
async getCookies() {
return this.page.evaluate('document.cookie;');
}
async close() {
if (this.browser && !this.isExternalBrowser) {
await this.browser.close();
this.browser = null;
}
if (this.page) {
this.page = null;
}
}
}
function getRandomInt(a, b) {
const min = Math.min(a, b);
const max = Math.max(a, b);
const diff = max - min + 1;
return min + Math.floor(Math.random() * Math.floor(diff));
}
module.exports = Signer;