-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
31 lines (29 loc) · 871 Bytes
/
index.ts
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
import axios from 'axios';
async function scrapeOoklaData(url: string) {
try {
const res = await axios.get(url);
const match = res.data.match(/window\.OOKLA\.INIT_DATA\s*=\s*(\{.*?\});/);
if (match) {
const data = JSON.parse(match[1]);
return {
download: data.result.download / 1000, // in Mbps
upload: data.result.download / 1000, // in Mbps
};
}
return {
download: 0,
upload: 0,
};
} catch (error) {
return {
download: 0,
upload: 0,
};
}
}
// Example usage:
const url = 'https://www.speedtest.net/result/your-test-id';
scrapeOoklaData(url).then((data) => {
console.log('Download speed:', data.download, 'Mbps');
console.log('Upload speed:', data.upload, 'Mbps');
});