@xkwdstore/agent 2.0.1 → 2.0.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.
@@ -1,138 +1,138 @@
1
- 'use strict';
2
-
3
- /**
4
- * Comment action — post a reply/comment on a tweet.
5
- * Ported from backend/routes/browser.js doComment().
6
- */
7
-
8
- const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage } = require('./helpers');
9
-
10
- async function comment(page, payload) {
11
- const { tweetUrl, commentText } = payload;
12
- if (!tweetUrl || !commentText) throw new Error('tweetUrl and commentText required');
13
-
14
- // Navigate to the tweet
15
- await page.goto(tweetUrl, { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
16
- await randDelay(1800, 3200);
17
-
18
- if (page.url().includes('/login') || page.url().includes('/i/flow')) {
19
- throw new Error('Not logged in — open browser and login first');
20
- }
21
-
22
- // Find and click the reply button
23
- const tweetIdMatch = tweetUrl.match(/\/status\/(\d+)/);
24
- const tweetId = tweetIdMatch ? tweetIdMatch[1] : null;
25
- const replySelectors = [
26
- 'button[data-testid="reply"]',
27
- '[data-testid="reply"]',
28
- 'button[aria-label*="Reply"]',
29
- ];
30
-
31
- let replyClicked = false;
32
- for (const sel of replySelectors) {
33
- const scoped = tweetId ? `article:has(a[href*="/status/${tweetId}"]) ${sel}` : sel;
34
- const btn = page.locator(scoped).first();
35
- if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
36
- await humanClick(page, btn);
37
- replyClicked = true;
38
- break;
39
- }
40
- }
41
-
42
- if (!replyClicked && tweetId) {
43
- // Fallback for tweet detail pages where the target article does not include
44
- // a status link back to itself.
45
- for (const sel of replySelectors) {
46
- const btn = page.locator(sel).first();
47
- if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
48
- await humanClick(page, btn);
49
- replyClicked = true;
50
- break;
51
- }
52
- }
53
- }
54
-
55
- if (!replyClicked) throw new Error('Could not find reply button');
56
-
57
- // Wait for dialog or inline editor
58
- await sleep(1500);
59
- const editorSelectors = [
60
- 'div[role="dialog"] [data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
61
- '[data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
62
- '.public-DraftEditor-content',
63
- ];
64
-
65
- let editor = null;
66
- for (const sel of editorSelectors) {
67
- const el = page.locator(sel).first();
68
- if (await el.isVisible({ timeout: 5000 }).catch(() => false)) {
69
- editor = el;
70
- break;
71
- }
72
- }
73
-
74
- if (!editor) throw new Error('Could not find comment editor');
75
- await editor.click();
76
- await humanDelay(300, 600);
77
-
78
- // Clear any existing text
79
- await page.keyboard.press('Control+a');
80
- await page.keyboard.press('Backspace');
81
-
82
- // Type comment
83
- const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(commentText);
84
- if (isRTL) {
85
- await page.evaluate(() => {
86
- const el = document.querySelector('[data-testid="tweetTextarea_0"]');
87
- if (el) el.setAttribute('dir', 'rtl');
88
- });
89
- const words = commentText.split(' ');
90
- for (const word of words) {
91
- await page.evaluate((w) => document.execCommand('insertText', false, w + ' '), word);
92
- await humanDelay(50, 100);
93
- }
94
- } else {
95
- await humanType(page, commentText, { delayMin: 50, delayMax: 150 });
96
- }
97
-
98
- await humanDelay(500, 1000);
99
-
100
- // Click send/reply button
101
- const sendSelectors = [
102
- 'div[role="dialog"] [data-testid="tweetButton"]',
103
- '[data-testid="tweetButton"]',
104
- '[data-testid="tweetButtonInline"]',
105
- ];
106
-
107
- let sent = false;
108
- for (const sel of sendSelectors) {
109
- const btn = page.locator(sel).first();
110
- if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
111
- await humanClick(page, btn);
112
- sent = true;
113
- break;
114
- }
115
- }
116
-
117
- if (!sent) {
118
- await page.keyboard.press('Control+Enter');
119
- }
120
-
121
- // Wait for dialog to close
122
- try {
123
- await page.waitForSelector('div[role="dialog"]', { state: 'detached', timeout: 15000 });
124
- } catch {
125
- await page.keyboard.press('Control+Enter');
126
- await sleep(3000);
127
- }
128
-
129
- // Check for failure
130
- const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
131
- if (/something went wrong|try again|failed/i.test(bodyText)) {
132
- throw new Error('Comment submit reported a failure');
133
- }
134
-
135
- return { success: true, message: 'Comment posted' };
136
- }
137
-
1
+ 'use strict';
2
+
3
+ /**
4
+ * Comment action — post a reply/comment on a tweet.
5
+ * Ported from backend/routes/browser.js doComment().
6
+ */
7
+
8
+ const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage } = require('./helpers');
9
+
10
+ async function comment(page, payload) {
11
+ const { tweetUrl, commentText } = payload;
12
+ if (!tweetUrl || !commentText) throw new Error('tweetUrl and commentText required');
13
+
14
+ // Navigate to the tweet
15
+ await page.goto(tweetUrl, { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
16
+ await randDelay(1800, 3200);
17
+
18
+ if (page.url().includes('/login') || page.url().includes('/i/flow')) {
19
+ throw new Error('Not logged in — open browser and login first');
20
+ }
21
+
22
+ // Find and click the reply button
23
+ const tweetIdMatch = tweetUrl.match(/\/status\/(\d+)/);
24
+ const tweetId = tweetIdMatch ? tweetIdMatch[1] : null;
25
+ const replySelectors = [
26
+ 'button[data-testid="reply"]',
27
+ '[data-testid="reply"]',
28
+ 'button[aria-label*="Reply"]',
29
+ ];
30
+
31
+ let replyClicked = false;
32
+ for (const sel of replySelectors) {
33
+ const scoped = tweetId ? `article:has(a[href*="/status/${tweetId}"]) ${sel}` : sel;
34
+ const btn = page.locator(scoped).first();
35
+ if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
36
+ await humanClick(page, btn);
37
+ replyClicked = true;
38
+ break;
39
+ }
40
+ }
41
+
42
+ if (!replyClicked && tweetId) {
43
+ // Fallback for tweet detail pages where the target article does not include
44
+ // a status link back to itself.
45
+ for (const sel of replySelectors) {
46
+ const btn = page.locator(sel).first();
47
+ if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
48
+ await humanClick(page, btn);
49
+ replyClicked = true;
50
+ break;
51
+ }
52
+ }
53
+ }
54
+
55
+ if (!replyClicked) throw new Error('Could not find reply button');
56
+
57
+ // Wait for dialog or inline editor
58
+ await sleep(1500);
59
+ const editorSelectors = [
60
+ 'div[role="dialog"] [data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
61
+ '[data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
62
+ '.public-DraftEditor-content',
63
+ ];
64
+
65
+ let editor = null;
66
+ for (const sel of editorSelectors) {
67
+ const el = page.locator(sel).first();
68
+ if (await el.isVisible({ timeout: 5000 }).catch(() => false)) {
69
+ editor = el;
70
+ break;
71
+ }
72
+ }
73
+
74
+ if (!editor) throw new Error('Could not find comment editor');
75
+ await editor.click();
76
+ await humanDelay(300, 600);
77
+
78
+ // Clear any existing text
79
+ await page.keyboard.press('Control+a');
80
+ await page.keyboard.press('Backspace');
81
+
82
+ // Type comment
83
+ const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(commentText);
84
+ if (isRTL) {
85
+ await page.evaluate(() => {
86
+ const el = document.querySelector('[data-testid="tweetTextarea_0"]');
87
+ if (el) el.setAttribute('dir', 'rtl');
88
+ });
89
+ const words = commentText.split(' ');
90
+ for (const word of words) {
91
+ await page.evaluate((w) => document.execCommand('insertText', false, w + ' '), word);
92
+ await humanDelay(50, 100);
93
+ }
94
+ } else {
95
+ await humanType(page, commentText, { delayMin: 50, delayMax: 150 });
96
+ }
97
+
98
+ await humanDelay(500, 1000);
99
+
100
+ // Click send/reply button
101
+ const sendSelectors = [
102
+ 'div[role="dialog"] [data-testid="tweetButton"]',
103
+ '[data-testid="tweetButton"]',
104
+ '[data-testid="tweetButtonInline"]',
105
+ ];
106
+
107
+ let sent = false;
108
+ for (const sel of sendSelectors) {
109
+ const btn = page.locator(sel).first();
110
+ if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
111
+ await humanClick(page, btn);
112
+ sent = true;
113
+ break;
114
+ }
115
+ }
116
+
117
+ if (!sent) {
118
+ await page.keyboard.press('Control+Enter');
119
+ }
120
+
121
+ // Wait for dialog to close
122
+ try {
123
+ await page.waitForSelector('div[role="dialog"]', { state: 'detached', timeout: 15000 });
124
+ } catch {
125
+ await page.keyboard.press('Control+Enter');
126
+ await sleep(3000);
127
+ }
128
+
129
+ // Check for failure
130
+ const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
131
+ if (/something went wrong|try again|failed/i.test(bodyText)) {
132
+ throw new Error('Comment submit reported a failure');
133
+ }
134
+
135
+ return { success: true, message: 'Comment posted' };
136
+ }
137
+
138
138
  module.exports = { comment };
@@ -1,150 +1,150 @@
1
- 'use strict';
2
-
3
- /**
4
- * Death By Captcha solver for the Local Agent.
5
- *
6
- * Uses the DBC HTTP API to solve image-based CAPTCHAs encountered
7
- * during Twitter/X login or challenge flows.
8
- *
9
- * The agent receives DBC credentials from the VPS in the startSession
10
- * command options (dbcUsername, dbcPassword).
11
- */
12
-
13
- const https = require('https');
14
- const http = require('http');
15
- const { URL } = require('url');
16
-
17
- const DBC_API_BASE = 'http://api.dbcapi.me/api';
18
- const DBC_TIMEOUT_MS = 90000;
19
- const DBC_POLL_INTERVAL_MS = 4000;
20
-
21
- let _username = null;
22
- let _password = null;
23
-
24
- /**
25
- * Configure DBC credentials (called when a session starts).
26
- */
27
- function configure(username, password) {
28
- _username = username || null;
29
- _password = password || null;
30
- }
31
-
32
- /**
33
- * Check if DBC is enabled.
34
- */
35
- function isEnabled() {
36
- return !!(_username && _password);
37
- }
38
-
39
- /**
40
- * Make an HTTP request and return the response body as a string.
41
- */
42
- function httpRequest(url, options = {}) {
43
- return new Promise((resolve, reject) => {
44
- const parsed = new URL(url);
45
- const lib = parsed.protocol === 'https:' ? https : http;
46
- const req = lib.request(url, {
47
- method: options.method || 'GET',
48
- headers: options.headers || {},
49
- timeout: options.timeout || 30000,
50
- }, (res) => {
51
- let data = '';
52
- res.on('data', (chunk) => { data += chunk; });
53
- res.on('end', () => {
54
- try { resolve(JSON.parse(data)); }
55
- catch { resolve(data); }
56
- });
57
- });
58
- req.on('error', reject);
59
- req.on('timeout', () => { req.destroy(); reject(new Error('Request timed out')); });
60
- if (options.body) req.write(options.body);
61
- req.end();
62
- });
63
- }
64
-
65
- /**
66
- * Solve an image-based CAPTCHA.
67
- * @param {string|Buffer} image - Base64 string or Buffer of the captcha image
68
- * @param {object} options - { timeoutMs }
69
- * @returns {Promise<{captcha:string, text:string}>}
70
- */
71
- async function solveImageCaptcha(image, options = {}) {
72
- if (!isEnabled()) throw new Error('DBC credentials not configured');
73
-
74
- const timeoutMs = options.timeoutMs || DBC_TIMEOUT_MS;
75
- let base64Image;
76
- if (Buffer.isBuffer(image)) {
77
- base64Image = image.toString('base64');
78
- } else if (typeof image === 'string') {
79
- base64Image = image.replace(/^data:image\/\w+;base64,/, '');
80
- } else {
81
- throw new Error('Invalid image input for DBC solve');
82
- }
83
-
84
- // Upload captcha
85
- const params = new URLSearchParams();
86
- params.append('username', _username);
87
- params.append('password', _password);
88
- params.append('type', '4'); // image CAPTCHA
89
- params.append('captchafile', `base64:${base64Image}`);
90
-
91
- console.log('[dbc] Uploading image captcha to DBC...');
92
- const upload = await httpRequest(`${DBC_API_BASE}/captcha`, {
93
- method: 'POST',
94
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
95
- body: params.toString(),
96
- timeout: 30000,
97
- });
98
-
99
- if (!upload || !upload.captcha) {
100
- throw new Error(`DBC upload failed: ${JSON.stringify(upload)}`);
101
- }
102
-
103
- const captchaId = upload.captcha;
104
- console.log(`[dbc] Captcha uploaded: ${captchaId}`);
105
-
106
- // Poll for answer
107
- const deadline = Date.now() + timeoutMs;
108
- while (Date.now() < deadline) {
109
- await new Promise(r => setTimeout(r, DBC_POLL_INTERVAL_MS));
110
-
111
- const status = await httpRequest(`${DBC_API_BASE}/captcha/${captchaId}`, {
112
- method: 'GET',
113
- timeout: 15000,
114
- });
115
-
116
- if (status && status.text) {
117
- console.log(`[dbc] Captcha solved: ${status.text}`);
118
- return { captcha: captchaId, text: status.text };
119
- }
120
-
121
- if (status && status.is_correct === 0 && status.text) {
122
- throw new Error(`DBC captcha rejected: ${status.text}`);
123
- }
124
- }
125
-
126
- throw new Error('DBC captcha solve timed out');
127
- }
128
-
129
- /**
130
- * Report an incorrectly solved captcha for refund.
131
- */
132
- async function reportBad(captchaId) {
133
- if (!isEnabled() || !captchaId) return;
134
- try {
135
- await httpRequest(`${DBC_API_BASE}/captcha/${captchaId}/report`, {
136
- method: 'POST',
137
- timeout: 15000,
138
- });
139
- console.log(`[dbc] Reported bad captcha: ${captchaId}`);
140
- } catch (err) {
141
- console.log(`[dbc] Failed to report bad captcha ${captchaId}: ${err.message}`);
142
- }
143
- }
144
-
145
- module.exports = {
146
- configure,
147
- isEnabled,
148
- solveImageCaptcha,
149
- reportBad,
1
+ 'use strict';
2
+
3
+ /**
4
+ * Death By Captcha solver for the Local Agent.
5
+ *
6
+ * Uses the DBC HTTP API to solve image-based CAPTCHAs encountered
7
+ * during Twitter/X login or challenge flows.
8
+ *
9
+ * The agent receives DBC credentials from the VPS in the startSession
10
+ * command options (dbcUsername, dbcPassword).
11
+ */
12
+
13
+ const https = require('https');
14
+ const http = require('http');
15
+ const { URL } = require('url');
16
+
17
+ const DBC_API_BASE = 'http://api.dbcapi.me/api';
18
+ const DBC_TIMEOUT_MS = 90000;
19
+ const DBC_POLL_INTERVAL_MS = 4000;
20
+
21
+ let _username = null;
22
+ let _password = null;
23
+
24
+ /**
25
+ * Configure DBC credentials (called when a session starts).
26
+ */
27
+ function configure(username, password) {
28
+ _username = username || null;
29
+ _password = password || null;
30
+ }
31
+
32
+ /**
33
+ * Check if DBC is enabled.
34
+ */
35
+ function isEnabled() {
36
+ return !!(_username && _password);
37
+ }
38
+
39
+ /**
40
+ * Make an HTTP request and return the response body as a string.
41
+ */
42
+ function httpRequest(url, options = {}) {
43
+ return new Promise((resolve, reject) => {
44
+ const parsed = new URL(url);
45
+ const lib = parsed.protocol === 'https:' ? https : http;
46
+ const req = lib.request(url, {
47
+ method: options.method || 'GET',
48
+ headers: options.headers || {},
49
+ timeout: options.timeout || 30000,
50
+ }, (res) => {
51
+ let data = '';
52
+ res.on('data', (chunk) => { data += chunk; });
53
+ res.on('end', () => {
54
+ try { resolve(JSON.parse(data)); }
55
+ catch { resolve(data); }
56
+ });
57
+ });
58
+ req.on('error', reject);
59
+ req.on('timeout', () => { req.destroy(); reject(new Error('Request timed out')); });
60
+ if (options.body) req.write(options.body);
61
+ req.end();
62
+ });
63
+ }
64
+
65
+ /**
66
+ * Solve an image-based CAPTCHA.
67
+ * @param {string|Buffer} image - Base64 string or Buffer of the captcha image
68
+ * @param {object} options - { timeoutMs }
69
+ * @returns {Promise<{captcha:string, text:string}>}
70
+ */
71
+ async function solveImageCaptcha(image, options = {}) {
72
+ if (!isEnabled()) throw new Error('DBC credentials not configured');
73
+
74
+ const timeoutMs = options.timeoutMs || DBC_TIMEOUT_MS;
75
+ let base64Image;
76
+ if (Buffer.isBuffer(image)) {
77
+ base64Image = image.toString('base64');
78
+ } else if (typeof image === 'string') {
79
+ base64Image = image.replace(/^data:image\/\w+;base64,/, '');
80
+ } else {
81
+ throw new Error('Invalid image input for DBC solve');
82
+ }
83
+
84
+ // Upload captcha
85
+ const params = new URLSearchParams();
86
+ params.append('username', _username);
87
+ params.append('password', _password);
88
+ params.append('type', '4'); // image CAPTCHA
89
+ params.append('captchafile', `base64:${base64Image}`);
90
+
91
+ console.log('[dbc] Uploading image captcha to DBC...');
92
+ const upload = await httpRequest(`${DBC_API_BASE}/captcha`, {
93
+ method: 'POST',
94
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
95
+ body: params.toString(),
96
+ timeout: 30000,
97
+ });
98
+
99
+ if (!upload || !upload.captcha) {
100
+ throw new Error(`DBC upload failed: ${JSON.stringify(upload)}`);
101
+ }
102
+
103
+ const captchaId = upload.captcha;
104
+ console.log(`[dbc] Captcha uploaded: ${captchaId}`);
105
+
106
+ // Poll for answer
107
+ const deadline = Date.now() + timeoutMs;
108
+ while (Date.now() < deadline) {
109
+ await new Promise(r => setTimeout(r, DBC_POLL_INTERVAL_MS));
110
+
111
+ const status = await httpRequest(`${DBC_API_BASE}/captcha/${captchaId}`, {
112
+ method: 'GET',
113
+ timeout: 15000,
114
+ });
115
+
116
+ if (status && status.text) {
117
+ console.log(`[dbc] Captcha solved: ${status.text}`);
118
+ return { captcha: captchaId, text: status.text };
119
+ }
120
+
121
+ if (status && status.is_correct === 0 && status.text) {
122
+ throw new Error(`DBC captcha rejected: ${status.text}`);
123
+ }
124
+ }
125
+
126
+ throw new Error('DBC captcha solve timed out');
127
+ }
128
+
129
+ /**
130
+ * Report an incorrectly solved captcha for refund.
131
+ */
132
+ async function reportBad(captchaId) {
133
+ if (!isEnabled() || !captchaId) return;
134
+ try {
135
+ await httpRequest(`${DBC_API_BASE}/captcha/${captchaId}/report`, {
136
+ method: 'POST',
137
+ timeout: 15000,
138
+ });
139
+ console.log(`[dbc] Reported bad captcha: ${captchaId}`);
140
+ } catch (err) {
141
+ console.log(`[dbc] Failed to report bad captcha ${captchaId}: ${err.message}`);
142
+ }
143
+ }
144
+
145
+ module.exports = {
146
+ configure,
147
+ isEnabled,
148
+ solveImageCaptcha,
149
+ reportBad,
150
150
  };