@jackwener/opencli 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLI-CREATOR.md +103 -142
- package/LICENSE +28 -0
- package/README.md +113 -63
- package/README.zh-CN.md +114 -63
- package/SKILL.md +21 -4
- package/dist/browser.d.ts +21 -2
- package/dist/browser.js +269 -15
- package/dist/browser.test.d.ts +1 -0
- package/dist/browser.test.js +43 -0
- package/dist/build-manifest.js +66 -2
- package/dist/cli-manifest.json +905 -109
- package/dist/clis/boss/search.js +186 -30
- package/dist/clis/twitter/delete.d.ts +1 -0
- package/dist/clis/twitter/delete.js +73 -0
- package/dist/clis/twitter/followers.d.ts +1 -0
- package/dist/clis/twitter/followers.js +104 -0
- package/dist/clis/twitter/following.d.ts +1 -0
- package/dist/clis/twitter/following.js +90 -0
- package/dist/clis/twitter/like.d.ts +1 -0
- package/dist/clis/twitter/like.js +69 -0
- package/dist/clis/twitter/notifications.d.ts +1 -0
- package/dist/clis/twitter/notifications.js +109 -0
- package/dist/clis/twitter/post.d.ts +1 -0
- package/dist/clis/twitter/post.js +63 -0
- package/dist/clis/twitter/reply.d.ts +1 -0
- package/dist/clis/twitter/reply.js +57 -0
- package/dist/clis/v2ex/daily.d.ts +1 -0
- package/dist/clis/v2ex/daily.js +98 -0
- package/dist/clis/v2ex/me.d.ts +1 -0
- package/dist/clis/v2ex/me.js +99 -0
- package/dist/clis/v2ex/notifications.d.ts +1 -0
- package/dist/clis/v2ex/notifications.js +72 -0
- package/dist/clis/xiaohongshu/search.d.ts +5 -2
- package/dist/clis/xiaohongshu/search.js +35 -41
- package/dist/doctor.d.ts +50 -0
- package/dist/doctor.js +372 -0
- package/dist/doctor.test.d.ts +1 -0
- package/dist/doctor.test.js +114 -0
- package/dist/main.js +47 -5
- package/dist/output.test.d.ts +1 -0
- package/dist/output.test.js +20 -0
- package/dist/registry.d.ts +4 -0
- package/dist/registry.js +1 -0
- package/dist/runtime.d.ts +3 -1
- package/dist/runtime.js +2 -2
- package/package.json +2 -2
- package/src/browser.test.ts +51 -0
- package/src/browser.ts +318 -22
- package/src/build-manifest.ts +67 -2
- package/src/clis/boss/search.ts +196 -29
- package/src/clis/twitter/delete.ts +78 -0
- package/src/clis/twitter/followers.ts +119 -0
- package/src/clis/twitter/following.ts +105 -0
- package/src/clis/twitter/like.ts +74 -0
- package/src/clis/twitter/notifications.ts +119 -0
- package/src/clis/twitter/post.ts +68 -0
- package/src/clis/twitter/reply.ts +62 -0
- package/src/clis/v2ex/daily.ts +105 -0
- package/src/clis/v2ex/me.ts +103 -0
- package/src/clis/v2ex/notifications.ts +77 -0
- package/src/clis/xiaohongshu/search.ts +41 -44
- package/src/doctor.test.ts +133 -0
- package/src/doctor.ts +424 -0
- package/src/main.ts +47 -4
- package/src/output.test.ts +27 -0
- package/src/registry.ts +5 -0
- package/src/runtime.ts +2 -1
package/dist/clis/boss/search.js
CHANGED
|
@@ -1,47 +1,203 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* BOSS直聘 job search — browser cookie API.
|
|
3
|
-
* Source: bb-sites/boss/search.js
|
|
4
3
|
*/
|
|
5
4
|
import { cli, Strategy } from '../../registry.js';
|
|
5
|
+
/** City name → BOSS Zhipin city code mapping */
|
|
6
|
+
const CITY_CODES = {
|
|
7
|
+
'全国': '100010000', '北京': '101010100', '上海': '101020100',
|
|
8
|
+
'广州': '101280100', '深圳': '101280600', '杭州': '101210100',
|
|
9
|
+
'成都': '101270100', '南京': '101190100', '武汉': '101200100',
|
|
10
|
+
'西安': '101110100', '苏州': '101190400', '长沙': '101250100',
|
|
11
|
+
'天津': '101030100', '重庆': '101040100', '郑州': '101180100',
|
|
12
|
+
'东莞': '101281600', '青岛': '101120200', '合肥': '101220100',
|
|
13
|
+
'佛山': '101280800', '宁波': '101210400', '厦门': '101230200',
|
|
14
|
+
'大连': '101070200', '珠海': '101280700', '无锡': '101190200',
|
|
15
|
+
'济南': '101120100', '福州': '101230100', '昆明': '101290100',
|
|
16
|
+
'哈尔滨': '101050100', '沈阳': '101070100', '石家庄': '101090100',
|
|
17
|
+
'贵阳': '101260100', '南宁': '101300100', '太原': '101100100',
|
|
18
|
+
'海口': '101310100', '兰州': '101160100', '乌鲁木齐': '101130100',
|
|
19
|
+
'长春': '101060100', '南昌': '101240100', '常州': '101191100',
|
|
20
|
+
'温州': '101210700', '嘉兴': '101210300', '徐州': '101190800',
|
|
21
|
+
'香港': '101320100',
|
|
22
|
+
};
|
|
23
|
+
const EXP_MAP = {
|
|
24
|
+
'不限': '0', '在校/应届': '108', '应届': '108', '1年以内': '101',
|
|
25
|
+
'1-3年': '102', '3-5年': '103', '5-10年': '104', '10年以上': '105',
|
|
26
|
+
};
|
|
27
|
+
const DEGREE_MAP = {
|
|
28
|
+
'不限': '0', '初中及以下': '209', '中专/中技': '208', '高中': '206',
|
|
29
|
+
'大专': '202', '本科': '203', '硕士': '204', '博士': '205',
|
|
30
|
+
};
|
|
31
|
+
const SALARY_MAP = {
|
|
32
|
+
'不限': '0', '3K以下': '401', '3-5K': '402', '5-10K': '403',
|
|
33
|
+
'10-15K': '404', '15-20K': '405', '20-30K': '406', '30-50K': '407', '50K以上': '408',
|
|
34
|
+
};
|
|
35
|
+
const INDUSTRY_MAP = {
|
|
36
|
+
'不限': '0', '互联网': '100020', '电子商务': '100021', '游戏': '100024',
|
|
37
|
+
'人工智能': '100901', '大数据': '100902', '金融': '100101',
|
|
38
|
+
'教育培训': '100200', '医疗健康': '100300',
|
|
39
|
+
};
|
|
40
|
+
function resolveCity(input) {
|
|
41
|
+
if (!input)
|
|
42
|
+
return '101010100';
|
|
43
|
+
if (/^\d+$/.test(input))
|
|
44
|
+
return input;
|
|
45
|
+
if (CITY_CODES[input])
|
|
46
|
+
return CITY_CODES[input];
|
|
47
|
+
for (const [name, code] of Object.entries(CITY_CODES)) {
|
|
48
|
+
if (name.includes(input))
|
|
49
|
+
return code;
|
|
50
|
+
}
|
|
51
|
+
return '101010100';
|
|
52
|
+
}
|
|
53
|
+
function resolveMap(input, map) {
|
|
54
|
+
if (!input)
|
|
55
|
+
return '';
|
|
56
|
+
if (map[input] !== undefined)
|
|
57
|
+
return map[input];
|
|
58
|
+
for (const [key, val] of Object.entries(map)) {
|
|
59
|
+
if (key.includes(input))
|
|
60
|
+
return val;
|
|
61
|
+
}
|
|
62
|
+
return input;
|
|
63
|
+
}
|
|
6
64
|
cli({
|
|
7
65
|
site: 'boss',
|
|
8
66
|
name: 'search',
|
|
9
67
|
description: 'BOSS直聘搜索职位',
|
|
10
68
|
domain: 'www.zhipin.com',
|
|
11
69
|
strategy: Strategy.COOKIE,
|
|
70
|
+
forceExtension: true, // BOSS Zhipin detects CDP mode — must use extension bridge
|
|
71
|
+
browser: true,
|
|
12
72
|
args: [
|
|
13
73
|
{ name: 'query', required: true, help: 'Search keyword (e.g. AI agent, 前端)' },
|
|
14
|
-
{ name: 'city', default: '
|
|
74
|
+
{ name: 'city', default: '北京', help: 'City name or code (e.g. 杭州, 上海, 101010100)' },
|
|
75
|
+
{ name: 'experience', default: '', help: 'Experience: 应届/1年以内/1-3年/3-5年/5-10年/10年以上' },
|
|
76
|
+
{ name: 'degree', default: '', help: 'Degree: 大专/本科/硕士/博士' },
|
|
77
|
+
{ name: 'salary', default: '', help: 'Salary: 3K以下/3-5K/5-10K/10-15K/15-20K/20-30K/30-50K/50K以上' },
|
|
78
|
+
{ name: 'industry', default: '', help: 'Industry code or name (e.g. 100020, 互联网)' },
|
|
79
|
+
{ name: 'page', type: 'int', default: 1, help: 'Page number' },
|
|
15
80
|
{ name: 'limit', type: 'int', default: 15, help: 'Number of results' },
|
|
16
81
|
],
|
|
17
|
-
columns: ['name', 'salary', 'company', '
|
|
82
|
+
columns: ['name', 'salary', 'company', 'area', 'experience', 'degree', 'skills', 'boss', 'url'],
|
|
18
83
|
func: async (page, kwargs) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
84
|
+
if (!page)
|
|
85
|
+
throw new Error('Browser page required');
|
|
86
|
+
const cityCode = resolveCity(kwargs.city);
|
|
87
|
+
if (process.env.OPENCLI_VERBOSE || process.env.DEBUG?.includes('opencli')) {
|
|
88
|
+
console.error(`[opencli:boss] Navigating to set referrer context...`);
|
|
89
|
+
}
|
|
90
|
+
// Navigate to the Web search view first to establish proper referrer context
|
|
91
|
+
// This is a lesson learned from boss-cli: referrer is important
|
|
92
|
+
await page.goto(`https://www.zhipin.com/web/geek/job?query=${encodeURIComponent(kwargs.query)}&city=${cityCode}`);
|
|
93
|
+
// Give the page a tiny bit of time to settle to avoid immediate 403s
|
|
94
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
95
|
+
const expVal = resolveMap(kwargs.experience, EXP_MAP);
|
|
96
|
+
const degreeVal = resolveMap(kwargs.degree, DEGREE_MAP);
|
|
97
|
+
const salaryVal = resolveMap(kwargs.salary, SALARY_MAP);
|
|
98
|
+
const industryVal = resolveMap(kwargs.industry, INDUSTRY_MAP);
|
|
99
|
+
const limit = kwargs.limit || 15;
|
|
100
|
+
let currentPage = kwargs.page || 1;
|
|
101
|
+
let allJobs = [];
|
|
102
|
+
const seenIds = new Set();
|
|
103
|
+
while (allJobs.length < limit) {
|
|
104
|
+
if (allJobs.length > 0) {
|
|
105
|
+
// Human-like pause between page fetches (1-3 seconds)
|
|
106
|
+
await new Promise(r => setTimeout(r, 1000 + Math.random() * 2000));
|
|
107
|
+
}
|
|
108
|
+
const qs = new URLSearchParams({
|
|
109
|
+
scene: '1',
|
|
110
|
+
query: kwargs.query,
|
|
111
|
+
city: cityCode,
|
|
112
|
+
page: String(currentPage),
|
|
113
|
+
pageSize: '15',
|
|
114
|
+
});
|
|
115
|
+
if (expVal)
|
|
116
|
+
qs.set('experience', expVal);
|
|
117
|
+
if (degreeVal)
|
|
118
|
+
qs.set('degree', degreeVal);
|
|
119
|
+
if (salaryVal)
|
|
120
|
+
qs.set('salary', salaryVal);
|
|
121
|
+
if (industryVal)
|
|
122
|
+
qs.set('industry', industryVal);
|
|
123
|
+
const targetUrl = `https://www.zhipin.com/wapi/zpgeek/search/joblist.json?${qs.toString()}`;
|
|
124
|
+
if (process.env.OPENCLI_VERBOSE || process.env.DEBUG?.includes('opencli')) {
|
|
125
|
+
console.error(`[opencli:boss] Fetching page ${currentPage}... (current jobs: ${allJobs.length})`);
|
|
126
|
+
}
|
|
127
|
+
const evaluateScript = `
|
|
128
|
+
async () => {
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
const xhr = new window.XMLHttpRequest();
|
|
131
|
+
xhr.open('GET', '${targetUrl}', true);
|
|
132
|
+
xhr.withCredentials = true;
|
|
133
|
+
xhr.timeout = 15000; // 15s timeout
|
|
134
|
+
xhr.setRequestHeader('Accept', 'application/json, text/plain, */*');
|
|
135
|
+
xhr.onload = () => {
|
|
136
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
137
|
+
try {
|
|
138
|
+
resolve(JSON.parse(xhr.responseText));
|
|
139
|
+
} catch (e) {
|
|
140
|
+
reject(new Error('Failed to parse JSON. Raw (200 chars): ' + xhr.responseText.substring(0, 200)));
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
reject(new Error('XHR HTTP Status: ' + xhr.status));
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
xhr.onerror = () => reject(new Error('XHR Network Error'));
|
|
147
|
+
xhr.ontimeout = () => reject(new Error('XHR Timeout'));
|
|
148
|
+
xhr.send();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
`;
|
|
152
|
+
let data;
|
|
153
|
+
try {
|
|
154
|
+
data = await page.evaluate(evaluateScript);
|
|
155
|
+
}
|
|
156
|
+
catch (e) {
|
|
157
|
+
throw new Error('API evaluate failed: ' + e.message);
|
|
158
|
+
}
|
|
159
|
+
if (data.code !== 0) {
|
|
160
|
+
if (data.code === 37) {
|
|
161
|
+
throw new Error('Cookie 已过期!请在当前 Chrome 浏览器中重新登录 BOSS 直聘。');
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`BOSS API error: ${data.message || 'Unknown'} (code=${data.code})\nRaw data: ${JSON.stringify(data)}`);
|
|
164
|
+
}
|
|
165
|
+
const zpData = data.zpData || {};
|
|
166
|
+
const batch = zpData.jobList || [];
|
|
167
|
+
if (batch.length === 0) {
|
|
168
|
+
break; // No more results
|
|
169
|
+
}
|
|
170
|
+
let addedInBatch = 0;
|
|
171
|
+
for (const j of batch) {
|
|
172
|
+
if (!j.encryptJobId || seenIds.has(j.encryptJobId))
|
|
173
|
+
continue;
|
|
174
|
+
seenIds.add(j.encryptJobId);
|
|
175
|
+
allJobs.push({
|
|
176
|
+
name: j.jobName,
|
|
177
|
+
salary: j.salaryDesc,
|
|
178
|
+
company: j.brandName,
|
|
179
|
+
area: [j.cityName, j.areaDistrict, j.businessDistrict].filter(Boolean).join('·'),
|
|
180
|
+
experience: j.jobExperience,
|
|
181
|
+
degree: j.jobDegree,
|
|
182
|
+
skills: (j.skills || []).join(','),
|
|
183
|
+
boss: j.bossName + ' · ' + j.bossTitle,
|
|
184
|
+
url: 'https://www.zhipin.com/job_detail/' + j.encryptJobId + '.html',
|
|
185
|
+
});
|
|
186
|
+
addedInBatch++;
|
|
187
|
+
if (allJobs.length >= limit)
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
if (addedInBatch === 0) {
|
|
191
|
+
// Boss API is repeating identical pages, we've hit the pagination limit
|
|
192
|
+
if (process.env.OPENCLI_VERBOSE)
|
|
193
|
+
console.error(`[opencli:boss] API returned duplicate page, stopping pagination at ${allJobs.length} items`);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
if (!zpData.hasMore) {
|
|
197
|
+
break; // API says no more pages
|
|
198
|
+
}
|
|
199
|
+
currentPage++;
|
|
200
|
+
}
|
|
201
|
+
return allJobs;
|
|
46
202
|
},
|
|
47
203
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
cli({
|
|
3
|
+
site: 'twitter',
|
|
4
|
+
name: 'delete',
|
|
5
|
+
description: 'Delete a specific tweet by URL',
|
|
6
|
+
domain: 'x.com',
|
|
7
|
+
strategy: Strategy.UI, // Utilizes internal DOM flows for interaction
|
|
8
|
+
browser: true,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'url', type: 'string', required: true, help: 'The URL of the tweet to delete' },
|
|
11
|
+
],
|
|
12
|
+
columns: ['status', 'message'],
|
|
13
|
+
func: async (page, kwargs) => {
|
|
14
|
+
if (!page)
|
|
15
|
+
throw new Error('Requires browser');
|
|
16
|
+
console.log(`Navigating to tweet: ${kwargs.url}`);
|
|
17
|
+
await page.goto(kwargs.url);
|
|
18
|
+
await page.wait(5); // Wait for tweet to load completely
|
|
19
|
+
const result = await page.evaluate(`(async () => {
|
|
20
|
+
try {
|
|
21
|
+
// Wait for caret button (which has 'More' aria-label) within the main tweet body
|
|
22
|
+
// Getting the first 'More' usually corresponds to the main displayed tweet of the URL
|
|
23
|
+
const moreMenu = document.querySelector('[aria-label="More"]');
|
|
24
|
+
if (!moreMenu) {
|
|
25
|
+
return { ok: false, message: 'Could not find the "More" context menu on this tweet. Are you sure you are logged in and looking at a valid tweet?' };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Click the 'More' 3 dots button to open the dropdown menu
|
|
29
|
+
moreMenu.click();
|
|
30
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
31
|
+
|
|
32
|
+
// Wait for dropdown pop-out to appear and look for the 'Delete' option
|
|
33
|
+
const items = document.querySelectorAll('[role="menuitem"]');
|
|
34
|
+
let deleteBtn = null;
|
|
35
|
+
for (const item of items) {
|
|
36
|
+
if (item.textContent.includes('Delete') && !item.textContent.includes('List')) {
|
|
37
|
+
deleteBtn = item;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!deleteBtn) {
|
|
43
|
+
// If there's no Delete button, it's not our tweet OR localization is not English.
|
|
44
|
+
// Assuming English default for now.
|
|
45
|
+
return { ok: false, message: 'This tweet does not seem to belong to you, or the Delete option is missing (not your tweet).' };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Click Delete
|
|
49
|
+
deleteBtn.click();
|
|
50
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
51
|
+
|
|
52
|
+
// Find and click the confirmation 'Delete' prompt inside the modal
|
|
53
|
+
const confirmBtn = document.querySelector('[data-testid="confirmationSheetConfirm"]');
|
|
54
|
+
if (confirmBtn) {
|
|
55
|
+
confirmBtn.click();
|
|
56
|
+
return { ok: true, message: 'Tweet successfully deleted.' };
|
|
57
|
+
} else {
|
|
58
|
+
return { ok: false, message: 'Delete confirmation dialog did not appear.' };
|
|
59
|
+
}
|
|
60
|
+
} catch (e) {
|
|
61
|
+
return { ok: false, message: e.toString() };
|
|
62
|
+
}
|
|
63
|
+
})()`);
|
|
64
|
+
if (result.ok) {
|
|
65
|
+
// Wait for the deletion request to be processed
|
|
66
|
+
await page.wait(2);
|
|
67
|
+
}
|
|
68
|
+
return [{
|
|
69
|
+
status: result.ok ? 'success' : 'failed',
|
|
70
|
+
message: result.message
|
|
71
|
+
}];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
cli({
|
|
3
|
+
site: 'twitter',
|
|
4
|
+
name: 'followers',
|
|
5
|
+
description: 'Get accounts following a Twitter/X user',
|
|
6
|
+
domain: 'x.com',
|
|
7
|
+
strategy: Strategy.INTERCEPT,
|
|
8
|
+
browser: true,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'user', type: 'string', required: false },
|
|
11
|
+
{ name: 'limit', type: 'int', default: 50 },
|
|
12
|
+
],
|
|
13
|
+
columns: ['screen_name', 'name', 'bio', 'followers'],
|
|
14
|
+
func: async (page, kwargs) => {
|
|
15
|
+
let targetUser = kwargs.user;
|
|
16
|
+
// If no user is specified, we must figure out the logged-in user's handle
|
|
17
|
+
if (!targetUser) {
|
|
18
|
+
await page.goto('https://x.com/home');
|
|
19
|
+
// wait for home page navigation
|
|
20
|
+
await page.wait(5);
|
|
21
|
+
const href = await page.evaluate(`() => {
|
|
22
|
+
const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
|
|
23
|
+
return link ? link.getAttribute('href') : null;
|
|
24
|
+
}`);
|
|
25
|
+
if (!href) {
|
|
26
|
+
throw new Error('Could not find logged-in user profile link. Are you logged in?');
|
|
27
|
+
}
|
|
28
|
+
targetUser = href.replace('/', '');
|
|
29
|
+
}
|
|
30
|
+
// 1. Navigate to user profile page
|
|
31
|
+
await page.goto(`https://x.com/${targetUser}`);
|
|
32
|
+
await page.wait(3);
|
|
33
|
+
// 2. Inject interceptor for Followers GraphQL API (or user_flow.json)
|
|
34
|
+
await page.installInterceptor('graphql');
|
|
35
|
+
// 3. Click the followers link inside the profile page
|
|
36
|
+
await page.evaluate(`() => {
|
|
37
|
+
const target = '${targetUser}';
|
|
38
|
+
const link = document.querySelector('a[href="/' + target + '/followers"]');
|
|
39
|
+
if (link) link.click();
|
|
40
|
+
}`);
|
|
41
|
+
await page.wait(3);
|
|
42
|
+
// 4. Trigger API by scrolling
|
|
43
|
+
await page.autoScroll({ times: Math.ceil(kwargs.limit / 20), delayMs: 2000 });
|
|
44
|
+
// 4. Retrieve data from opencli's registered interceptors
|
|
45
|
+
const allRequests = await page.getInterceptedRequests();
|
|
46
|
+
// Debug: Force dump all intercepted XHRs that match followers
|
|
47
|
+
if (!allRequests || allRequests.length === 0) {
|
|
48
|
+
console.log('No GraphQL requests captured by the interceptor backend.');
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
console.log('Intercepted keys:', allRequests.map((r) => {
|
|
52
|
+
try {
|
|
53
|
+
const u = new URL(r.url);
|
|
54
|
+
return u.pathname;
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return r.url;
|
|
58
|
+
}
|
|
59
|
+
}));
|
|
60
|
+
const requests = allRequests.filter((r) => r.url.includes('Followers'));
|
|
61
|
+
if (!requests || requests.length === 0) {
|
|
62
|
+
console.log('No specific Followers requests captured. Check keys printed above.');
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
let results = [];
|
|
66
|
+
for (const req of requests) {
|
|
67
|
+
try {
|
|
68
|
+
let instructions = req.data?.data?.user?.result?.timeline?.timeline?.instructions;
|
|
69
|
+
if (!instructions)
|
|
70
|
+
continue;
|
|
71
|
+
let addEntries = instructions.find((i) => i.type === 'TimelineAddEntries');
|
|
72
|
+
if (!addEntries) {
|
|
73
|
+
addEntries = instructions.find((i) => i.entries && Array.isArray(i.entries));
|
|
74
|
+
}
|
|
75
|
+
if (!addEntries)
|
|
76
|
+
continue;
|
|
77
|
+
for (const entry of addEntries.entries) {
|
|
78
|
+
if (!entry.entryId.startsWith('user-'))
|
|
79
|
+
continue;
|
|
80
|
+
const item = entry.content?.itemContent?.user_results?.result;
|
|
81
|
+
if (!item || item.__typename !== 'User')
|
|
82
|
+
continue;
|
|
83
|
+
// Twitter GraphQL sometimes nests `core` differently depending on the endpoint profile state
|
|
84
|
+
const core = item.core || {};
|
|
85
|
+
const legacy = item.legacy || {};
|
|
86
|
+
results.push({
|
|
87
|
+
screen_name: core.screen_name || legacy.screen_name || 'unknown',
|
|
88
|
+
name: core.name || legacy.name || 'unknown',
|
|
89
|
+
bio: legacy.description || item.profile_bio?.description || '',
|
|
90
|
+
followers: legacy.followers_count || legacy.normal_followers_count || 0
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
// ignore parsing errors for individual payloads
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Deduplicate by screen_name in case multiple scrolls caught the same
|
|
99
|
+
const unique = new Map();
|
|
100
|
+
results.forEach(r => unique.set(r.screen_name, r));
|
|
101
|
+
const deduplicatedResults = Array.from(unique.values());
|
|
102
|
+
return deduplicatedResults.slice(0, kwargs.limit);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
cli({
|
|
3
|
+
site: 'twitter',
|
|
4
|
+
name: 'following',
|
|
5
|
+
description: 'Get accounts a Twitter/X user is following',
|
|
6
|
+
domain: 'x.com',
|
|
7
|
+
strategy: Strategy.INTERCEPT,
|
|
8
|
+
browser: true,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'user', type: 'string', required: false },
|
|
11
|
+
{ name: 'limit', type: 'int', default: 50 },
|
|
12
|
+
],
|
|
13
|
+
columns: ['screen_name', 'name', 'bio', 'followers'],
|
|
14
|
+
func: async (page, kwargs) => {
|
|
15
|
+
let targetUser = kwargs.user;
|
|
16
|
+
// If no user is specified, we must figure out the logged-in user's handle
|
|
17
|
+
if (!targetUser) {
|
|
18
|
+
await page.goto('https://x.com/home');
|
|
19
|
+
// wait for home page navigation
|
|
20
|
+
await page.wait(5);
|
|
21
|
+
const href = await page.evaluate(`() => {
|
|
22
|
+
const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
|
|
23
|
+
return link ? link.getAttribute('href') : null;
|
|
24
|
+
}`);
|
|
25
|
+
if (!href) {
|
|
26
|
+
throw new Error('Could not find logged-in user profile link. Are you logged in?');
|
|
27
|
+
}
|
|
28
|
+
targetUser = href.replace('/', '');
|
|
29
|
+
}
|
|
30
|
+
// 1. Navigate to user profile page
|
|
31
|
+
await page.goto(`https://x.com/${targetUser}`);
|
|
32
|
+
await page.wait(3);
|
|
33
|
+
// 2. Inject interceptor for Following GraphQL API
|
|
34
|
+
await page.installInterceptor('Following');
|
|
35
|
+
// 3. Click the following link inside the profile page
|
|
36
|
+
await page.evaluate(`() => {
|
|
37
|
+
const target = '${targetUser}';
|
|
38
|
+
const link = document.querySelector('a[href="/' + target + '/following"]');
|
|
39
|
+
if (link) link.click();
|
|
40
|
+
}`);
|
|
41
|
+
await page.wait(3);
|
|
42
|
+
// 4. Trigger API by scrolling
|
|
43
|
+
await page.autoScroll({ times: Math.ceil(kwargs.limit / 20), delayMs: 2000 });
|
|
44
|
+
// 4. Retrieve data from opencli's registered interceptors
|
|
45
|
+
const requests = await page.getInterceptedRequests();
|
|
46
|
+
// Debug: Force dump all intercepted XHRs that match following
|
|
47
|
+
if (!requests || requests.length === 0) {
|
|
48
|
+
console.log('No Following requests captured by the interceptor backend.');
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
let results = [];
|
|
52
|
+
for (const req of requests) {
|
|
53
|
+
try {
|
|
54
|
+
let instructions = req.data?.data?.user?.result?.timeline?.timeline?.instructions;
|
|
55
|
+
if (!instructions)
|
|
56
|
+
continue;
|
|
57
|
+
let addEntries = instructions.find((i) => i.type === 'TimelineAddEntries');
|
|
58
|
+
if (!addEntries) {
|
|
59
|
+
addEntries = instructions.find((i) => i.entries && Array.isArray(i.entries));
|
|
60
|
+
}
|
|
61
|
+
if (!addEntries)
|
|
62
|
+
continue;
|
|
63
|
+
for (const entry of addEntries.entries) {
|
|
64
|
+
if (!entry.entryId.startsWith('user-'))
|
|
65
|
+
continue;
|
|
66
|
+
const item = entry.content?.itemContent?.user_results?.result;
|
|
67
|
+
if (!item || item.__typename !== 'User')
|
|
68
|
+
continue;
|
|
69
|
+
// Twitter GraphQL sometimes nests `core` differently depending on the endpoint profile state
|
|
70
|
+
const core = item.core || {};
|
|
71
|
+
const legacy = item.legacy || {};
|
|
72
|
+
results.push({
|
|
73
|
+
screen_name: core.screen_name || legacy.screen_name || 'unknown',
|
|
74
|
+
name: core.name || legacy.name || 'unknown',
|
|
75
|
+
bio: legacy.description || item.profile_bio?.description || '',
|
|
76
|
+
followers: legacy.followers_count || legacy.normal_followers_count || 0
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
// ignore parsing errors for individual payloads
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Deduplicate by screen_name in case multiple scrolls caught the same
|
|
85
|
+
const unique = new Map();
|
|
86
|
+
results.forEach(r => unique.set(r.screen_name, r));
|
|
87
|
+
const deduplicatedResults = Array.from(unique.values());
|
|
88
|
+
return deduplicatedResults.slice(0, kwargs.limit);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { cli, Strategy } from '../../registry.js';
|
|
2
|
+
cli({
|
|
3
|
+
site: 'twitter',
|
|
4
|
+
name: 'like',
|
|
5
|
+
description: 'Like a specific tweet',
|
|
6
|
+
domain: 'x.com',
|
|
7
|
+
strategy: Strategy.UI, // Utilizes internal DOM flows for interaction
|
|
8
|
+
browser: true,
|
|
9
|
+
args: [
|
|
10
|
+
{ name: 'url', type: 'string', required: true, help: 'The URL of the tweet to like' },
|
|
11
|
+
],
|
|
12
|
+
columns: ['status', 'message'],
|
|
13
|
+
func: async (page, kwargs) => {
|
|
14
|
+
if (!page)
|
|
15
|
+
throw new Error('Requires browser');
|
|
16
|
+
console.log(`Navigating to tweet: ${kwargs.url}`);
|
|
17
|
+
await page.goto(kwargs.url);
|
|
18
|
+
await page.wait(5); // Wait for tweet to load completely
|
|
19
|
+
const result = await page.evaluate(`(async () => {
|
|
20
|
+
try {
|
|
21
|
+
// Poll for the tweet to render
|
|
22
|
+
let attempts = 0;
|
|
23
|
+
let likeBtn = null;
|
|
24
|
+
let unlikeBtn = null;
|
|
25
|
+
|
|
26
|
+
while (attempts < 20) {
|
|
27
|
+
unlikeBtn = document.querySelector('[data-testid="unlike"]');
|
|
28
|
+
likeBtn = document.querySelector('[data-testid="like"]');
|
|
29
|
+
|
|
30
|
+
if (unlikeBtn || likeBtn) break;
|
|
31
|
+
|
|
32
|
+
await new Promise(r => setTimeout(r, 500));
|
|
33
|
+
attempts++;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Check if it's already liked
|
|
37
|
+
if (unlikeBtn) {
|
|
38
|
+
return { ok: true, message: 'Tweet is already liked.' };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!likeBtn) {
|
|
42
|
+
return { ok: false, message: 'Could not find the Like button on this tweet after waiting 10 seconds. Are you logged in?' };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Click Like
|
|
46
|
+
likeBtn.click();
|
|
47
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
48
|
+
|
|
49
|
+
// Verify success by checking if the 'unlike' button appeared
|
|
50
|
+
const verifyBtn = document.querySelector('[data-testid="unlike"]');
|
|
51
|
+
if (verifyBtn) {
|
|
52
|
+
return { ok: true, message: 'Tweet successfully liked.' };
|
|
53
|
+
} else {
|
|
54
|
+
return { ok: false, message: 'Like action was initiated but UI did not update as expected.' };
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return { ok: false, message: e.toString() };
|
|
58
|
+
}
|
|
59
|
+
})()`);
|
|
60
|
+
if (result.ok) {
|
|
61
|
+
// Wait for the like network request to be processed
|
|
62
|
+
await page.wait(2);
|
|
63
|
+
}
|
|
64
|
+
return [{
|
|
65
|
+
status: result.ok ? 'success' : 'failed',
|
|
66
|
+
message: result.message
|
|
67
|
+
}];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|