-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfetch_tweets.mjs
More file actions
75 lines (62 loc) · 1.95 KB
/
fetch_tweets.mjs
File metadata and controls
75 lines (62 loc) · 1.95 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
import { chromium } from 'playwright';
async function fetchTweet(url) {
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
});
const page = await context.newPage();
try {
console.log(`\n=== Fetching: ${url} ===\n`);
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
// Wait a bit for dynamic content
await page.waitForTimeout(3000);
// Get page title
const title = await page.title();
console.log(`Title: ${title}\n`);
// Try multiple selectors for tweets
const tweetText = await page.evaluate(() => {
// Try to get all text content from tweets
const selectors = [
'[data-testid="tweetText"]',
'article[data-testid="tweet"]',
'[role="article"]'
];
let results = [];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
if (elements.length > 0) {
elements.forEach(el => {
const text = el.textContent?.trim();
if (text && text.length > 10) {
results.push(text);
}
});
if (results.length > 0) break;
}
}
// If nothing found, get all visible text
if (results.length === 0) {
const body = document.body.innerText;
return body.slice(0, 5000); // First 5000 chars
}
return results.join('\n\n---\n\n');
});
console.log('CONTENT:');
console.log(tweetText);
console.log('\n');
} catch (error) {
console.error(`Error fetching ${url}:`, error.message);
} finally {
await browser.close();
}
}
async function main() {
const urls = [
'https://x.com/deronin_/status/2032796569808830921',
'https://x.com/arscontexta/status/2023957499183829467'
];
for (const url of urls) {
await fetchTweet(url);
}
}
main();