-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.ts
More file actions
58 lines (45 loc) · 1.63 KB
/
scraper.ts
File metadata and controls
58 lines (45 loc) · 1.63 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
import axios from "axios";
import * as fs from "fs";
import * as yaml from "js-yaml";
interface LanguageEntry {
rank: number;
name: string;
percentage: number;
change: string;
}
async function scrapeTiobe(): Promise<LanguageEntry[]> {
const url = "https://www.tiobe.com/tiobe-index/";
try {
const response = await axios.get(url);
const htmlContent = response.data;
const regex = /<tr>.*?<td>(\d+)<\/td>.*?<td class="td-top20"><img [^>]+alt="([^"]+)".*?<\/td><td>([^<]+)%<\/td><td>([\+\-\d.]+%)<\/td>/g;
const languagesList: LanguageEntry[] = [];
let match: RegExpExecArray | null;
while ((match = regex.exec(htmlContent)) !== null) {
const rank = parseInt(match[1], 10);
const languageAlt = match[2];
const language = languageAlt.split(" page")[0].trim();
const percentage = parseFloat(match[3]);
const change = match[4];
languagesList.push({ rank, name: language, percentage, change });
}
return languagesList;
} catch (error) {
console.error("Error fetching TIOBE index:", error);
return [];
}
}
async function saveData() {
const languages = await scrapeTiobe();
console.log(languages);
if (languages.length === 0) {
console.error("No data fetched.");
return;
}
const jsonOutput = JSON.stringify(languages, null, 2);
const yamlOutput = yaml.dump(languages);
fs.writeFileSync("tiobe.json", jsonOutput);
fs.writeFileSync("tiobe.yaml", yamlOutput);
console.log("Data saved as tiobe.json and tiobe.yaml");
}
saveData();