-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-crawl.js
More file actions
165 lines (142 loc) · 5.16 KB
/
test-crawl.js
File metadata and controls
165 lines (142 loc) · 5.16 KB
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
#!/usr/bin/env node
import { execSync } from 'child_process';
import { writeFileSync, mkdirSync, existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const BASE_URL = 'https://app.workback.ai';
const ISSUES_DIR = join(__dirname, 'issues');
const extractScriptPath = join(__dirname, 'extract-issue.js');
function execAgentBrowser(command) {
try {
return execSync(`agent-browser ${command}`, {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe']
});
} catch (error) {
throw new Error(`agent-browser command failed: ${command}\n${error.message}`);
}
}
function getCookieFile() {
const out = execAgentBrowser('cookies --json');
let json;
try {
const start = out.indexOf('{');
json = JSON.parse(start >= 0 ? out.slice(start) : '{}');
} catch {
return null;
}
const cookies = json?.data?.cookies || json?.cookies || [];
const workback = cookies.filter(
c => c.domain === 'app.workback.ai' || (c.domain?.endsWith && c.domain.endsWith('.workback.ai'))
);
if (workback.length === 0) return null;
const lines = [
'# Netscape HTTP Cookie File',
...workback.map(c => {
const domain = (c.domain?.startsWith('.') ? '' : '.') + (c.domain || 'app.workback.ai');
const path = c.path || '/';
const secure = c.secure ? 'TRUE' : 'FALSE';
const expires = Math.floor((c.expires > 0 ? c.expires : Date.now() / 1000 + 86400));
return `${domain}\tTRUE\t${path}\t${secure}\t${expires}\t${c.name}\t${c.value}`;
})
];
const cookieFile = join(ISSUES_DIR, '.workback-cookies.txt');
writeFileSync(cookieFile, lines.join('\n'));
return cookieFile;
}
function downloadImage(imageUrl, outputPath) {
try {
const cookieFile = getCookieFile();
if (!cookieFile) {
console.error('No Workback.ai cookies - ensure browser is connected and logged in');
return false;
}
execSync(`curl -s -L -b "${cookieFile}" "${imageUrl}" -o "${outputPath}"`, { encoding: 'utf-8' });
const buf = readFileSync(outputPath, { start: 0, end: 99 });
if (buf.toString('utf8').trimStart().startsWith('<')) {
console.error('Got HTML instead of image - session may have expired');
return false;
}
return true;
} catch (error) {
console.error(`Failed to download image ${imageUrl}: ${error.message}`);
return false;
}
}
// Test with issue 4226
const issueId = 4226;
const issueUrl = `${BASE_URL}/issues/${issueId}/`;
console.log(`Testing crawl for issue ${issueId}...`);
// Connect to browser
execAgentBrowser('connect 9222');
// Navigate to issue page
console.log('Navigating to issue page...');
execAgentBrowser(`open ${issueUrl}`);
// Wait for page to load
execAgentBrowser('wait --load networkidle');
execAgentBrowser('wait 2000');
// Extract content
console.log('Extracting content...');
const contentJson = execAgentBrowser(`eval "$(cat "${extractScriptPath}")"`);
// Handle double-encoded JSON (sometimes agent-browser wraps in quotes)
let jsonStr = contentJson.trim();
if (jsonStr.startsWith('"') && jsonStr.endsWith('"')) {
jsonStr = JSON.parse(jsonStr); // Unwrap outer quotes
}
const content = JSON.parse(jsonStr);
console.log('Extracted:', JSON.stringify(content, null, 2));
// Create directory
const issueDir = join(ISSUES_DIR, issueId.toString());
mkdirSync(issueDir, { recursive: true });
// Download images
const imagePaths = [];
content.images.forEach((image) => {
const imagePath = join(issueDir, image.name);
if (downloadImage(image.url, imagePath)) {
imagePaths.push({
originalName: image.name,
localPath: image.name,
alt: image.alt
});
console.log(`Downloaded: ${image.name}`);
}
});
// Create markdown
const frontmatter = `---
id: ${issueId}
title: "${content.title.replace(/"/g, '\\"')}"
status: ${content.metadata.status}
stage: ${content.metadata.stage}
severity: ${content.metadata.severity}
wcag: ${content.metadata.wcag}
flow: "${content.metadata.flow.replace(/"/g, '\\"')}"
pageUrl: "${content.metadata.pageUrl.replace(/"/g, '\\"')}"
auditDate: "${content.metadata.auditDate}"
---
`;
let markdown = frontmatter + `# ${content.title}\n\n`;
markdown += `## Metadata\n\n`;
markdown += `- **WCAG**: ${content.metadata.wcag}\n`;
markdown += `- **Severity**: ${content.metadata.severity}\n`;
markdown += `- **Status**: ${content.metadata.status}\n`;
markdown += `- **Stage**: ${content.metadata.stage}\n`;
markdown += `- **Flow**: ${content.metadata.flow}\n`;
markdown += `- **Page URL**: ${content.metadata.pageUrl}\n`;
markdown += `- **Audit Date**: ${content.metadata.auditDate}\n\n`;
Object.entries(content.sections).forEach(([sectionName, sectionContent]) => {
markdown += `## ${sectionName}\n\n`;
markdown += sectionContent + '\n\n';
});
if (imagePaths.length > 0) {
markdown += `## Images\n\n`;
imagePaths.forEach(img => {
markdown += `\n\n`;
});
}
const mdPath = join(issueDir, `issue-${issueId}.md`);
writeFileSync(mdPath, markdown);
console.log(`\n✓ Created ${mdPath}`);
console.log(`✓ Downloaded ${imagePaths.length} images`);