Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// This approach will use web scraping to fetch user information given a Twitter handle.
// We'll fetch the public Twitter profile page and extract the required information.
import cheerio from "https://esm.sh/cheerio@1.0.0-rc.12";
export default async function server(req: Request): Promise<Response> {
const url = new URL(req.url);
const formData = await req.formData().catch(() => null);
const handle = formData?.get('handle') as string | null;
if (!handle) {
return new Response(`
<html>
<head>
<title>Twitter User Info</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
h1 { color: #1DA1F2; }
form { margin-top: 20px; }
input[type="text"] { padding: 5px; font-size: 16px; width: 200px; }
input[type="submit"] { padding: 5px 10px; font-size: 16px; background-color: #1DA1F2; color: white; border: none; cursor: pointer; }
</style>
</head>
<body>
<h1>Twitter User Info</h1>
<form method="POST">
<input type="text" name="handle" placeholder="Enter Twitter handle" required>
<input type="submit" value="Get Info">
</form>
<p><a href="${import.meta.url.replace("esm.town", "val.town")}">View Source</a></p>
</body>
</html>
`, {
headers: { 'Content-Type': 'text/html' },
});
}
try {
console.log(`Fetching data for handle: ${handle}`);
const response = await fetch(`https://twitter.com/${handle}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}. The Twitter handle may not exist.`);
}
const html = await response.text();
console.log(`Received HTML response. Length: ${html.length}`);
const $ = cheerio.load(html);
// Extract name and follower count using multiple selectors
let name = '';
let followerText = '';
// Try different selectors for name
const nameSelectors = [
'h1[data-testid="UserName"] span:not([data-testid])',
'meta[property="og:title"]',
'title',
];
for (const selector of nameSelectors) {
if (selector === 'meta[property="og:title"]') {
name = $(selector).attr('content')?.split(' (')[0]?.trim() || '';
} else if (selector === 'title') {
name = $(selector).text().split(' (')[0]?.trim() || '';
} else {
name = $(selector).first().text().trim();
}
if (name) break;
}
// Try different selectors for follower count
const followerSelectors = [
'a[href$="/followers"] span[data-testid="FollowerCount"]',
'span:contains("followers")',
'a[href*="/followers"]',
];
for (const selector of followerSelectors) {
if (selector === 'span:contains("followers")') {
followerText = $(selector).prev('span').text().trim();
} else {
followerText = $(selector).first().text().trim();
}
if (followerText) break;
}
console.log(`Extracted name: ${name}`);
console.log(`Extracted follower text: ${followerText}`);
if (!name && !followerText) {
throw new Error('Could not find user information. The page structure might have changed.');
}
const followers = followerText ? parseFollowerCount(followerText) : 'Not found';
console.log(`Parsed followers: ${followers}`);
return new Response(`
<html>
<head>
shawnbasquiat-fetchtwitteruserinfobroken.web.val.run
August 30, 2024