-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetQueryId.js
169 lines (139 loc) · 6.3 KB
/
GetQueryId.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import axios from 'axios';
import crypto from 'crypto';
class GetQueryId {
constructor(botConfig) {
this.botConfig = botConfig;
this.graphQLClient = axios.create({
baseURL: 'https://twitter.com',
withCredentials: true,
headers: {
'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
'Content-Type': 'application/json',
'X-Csrf-Token': this.botConfig.TwitterCSRFToken,
'cookie': `auth_token=${this.botConfig.TwitterAuthToken}; ct0=${this.botConfig.TwitterCSRFToken}; guest_id=v1%3A${crypto.createHash('md5').update(new Date().toISOString()).digest('hex')}`
}
});
// 添加響應攔截器來處理重定向
this.graphQLClient.interceptors.response.use(async (response) => {
if (response.status === 200 && response.data) {
const redirectUrl = this.extractRedirectUrl(response.data);
if (redirectUrl) {
//console.log(`Redirecting to: ${redirectUrl}`);
return await this.graphQLClient.get(redirectUrl);
}
}
return response;
}, (error) => {
return Promise.reject(error);
});
this.apiQueryData = {};
this.apiQueryData.apiId = {};
this.apiQueryData.queryInfo = {};
}
extractRedirectUrl(html) {
const regex = /<meta http-equiv="refresh" content="0; url = (.*?)"/;
const match = html.match(regex);
return match ? match[1] : null;
}
async getQueryIdAndFeatureSwitches() {
const response = await this.graphQLClient.get('/', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Referer': 'https://twitter.com/'
}
});
const webContext = response.data;
for (const item of ['main', 'modules.audio']) {
await this.addApiQueryData(webContext, item);
}
console.log('New Twitter API Query Data Found!');
console.log(`Total Query Data: ${Object.keys(this.apiQueryData).length}`);
}
async addApiQueryData(webContext, fileType) {
try {
let match;
let fileName;
if (fileType === 'main') {
match = webContext.match(/main\.([^"]+)\.js/);
if (!match) throw new Error(`Failed to get ${fileType} version`);
fileName = match[0];
this.apiQueryData.apiId['mainJsId'] = fileName;
} else {
match = webContext.match(new RegExp(`"${fileType.replace('.', '\\.')}"\\s*:\\s*"([^"]+)"`));
if (!match) throw new Error(`Failed to get ${fileType} version`);
fileName = `${fileType}.${match[1]}a.js`;
this.apiQueryData.apiId['audiomoduleId'] = fileName;
}
const type = webContext.includes('-legacy') ? 'client-web-legacy' : 'client-web';
const mainJsResponse = await axios.get(`https://abs.twimg.com/responsive-web/${type}/${fileName}`);
const mainJsText = mainJsResponse.data;
const apiQueryRegex = /{queryId:"([^"]+)",operationName:"([^"]+)",operationType:"([^"]+)",metadata:{featureSwitches:\[([^\]]+)]/g;
let queryMatch;
while ((queryMatch = apiQueryRegex.exec(mainJsText)) !== null) {
const queryId = queryMatch[1];
const featureSwitches = queryMatch[4].split(',').map(s => s.replace(/"/g, ''));
this.apiQueryData.queryInfo[queryMatch[2]] = { queryId, queryToken: featureSwitches};
}
} catch (error) {
console.error(`Error adding API query data for ${fileType}`, error);
throw error;
}
}
async getQueryId(queryName) {
if (!this.apiQueryData.queryInfo[queryName]) {
throw new Error(`Query ${queryName} not found`);
}
return this.apiQueryData.queryInfo[queryName].queryId;
}
async getQueryToken(queryName) {
if (!this.apiQueryData.queryInfo[queryName]) {
throw new Error(`Query ${queryName} not found`);
}
return this.apiQueryData.queryInfo[queryName].queryToken;
}
async getQueryIdAndToken(queryName) {
return {
queryId: await this.getQueryId(queryName),
queryToken: await this.getQueryToken(queryName)
};
}
async tryUpdate() {
const response = await this.graphQLClient.get('/', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Referer': 'https://twitter.com/'
}
});
const webContext = response.data;
for (const item of ['main', 'modules.audio']) {
await this.checkAndTryUpdateData(webContext, item);
}
}
async checkAndTryUpdateData(webContext, fileType) {
try {
let match;
let fileName;
let outdated = false;
if (fileType === 'main') {
match = webContext.match(/main\.([^"]+)\.js/);
if (!match) throw new Error(`Failed to get ${fileType} version`);
fileName = match[0];
if(fileName !== this.apiQueryData.apiId['mainJsId']) outdated = true;
} else {
match = webContext.match(new RegExp(`"${fileType.replace('.', '\\.')}"\\s*:\\s*"([^"]+)"`));
if (!match) throw new Error(`Failed to get ${fileType} version`);
fileName = `${fileType}.${match[1]}a.js`;
if(fileName !== this.apiQueryData.apiId['audiomoduleId']) outdated = true;
}
if(outdated) {
await this.addApiQueryData(webContext, fileType);
}
}
catch (error) {
console.error(`Error checking version for ${fileType}`, error);
throw error;
}
}
}
export default GetQueryId;