-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallgfgfquestion.js
More file actions
68 lines (57 loc) · 1.95 KB
/
allgfgfquestion.js
File metadata and controls
68 lines (57 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
const axios = require('axios');
const fs = require('fs');
const BASE_URL = "https://practiceapi.geeksforgeeks.org/api/vr/problems/";
const MAX_PAGES = 151;
// Function to fetch data for a single page
const fetchPageData = async (page) => {
try {
const response = await axios.get(`${BASE_URL}?pageMode=explore&page=${page}&sortBy=submissions`);
return response.data.results.map(problem => ({
name: problem.problem_name,
difficulty: problem.difficulty,
tags: problem.tags.topic_tags.join(", "), // Include only topic tags
url: problem.problem_url
}));
} catch (error) {
console.error(`Error fetching page ${page}:`, error.message);
return []; // Return empty array on error to avoid breaking the script
}
};
// Function to fetch data for all pages
const fetchAllPages = async () => {
const allData = [];
let globalId = 1; // Initialize the sequential ID
for (let page = 1; page <= MAX_PAGES; page++) {
console.log(`Fetching page ${page}...`);
const pageData = await fetchPageData(page);
// Add the sequential ID to each problem
pageData.forEach(problem => {
problem.id = globalId++;
});
allData.push(...pageData);
}
return allData;
};
// Function to convert data to CSV
const saveToCSV = (data) => {
const headers = "ID,Problem Name,Difficulty,Tags,URL\n";
const rows = data.map(row =>
`${row.id},"${row.name}","${row.difficulty}","${row.tags}","${row.url}"`
).join("\n");
const csvContent = headers + rows;
fs.writeFile("all_problems_with_topic_tags.csv", csvContent, (err) => {
if (err) {
console.error("Error writing to CSV file:", err);
} else {
console.log("CSV file saved as all_problems_with_topic_tags.csv");
}
});
};
// Main function to execute the script
const main = async () => {
console.log("Starting data extraction...");
const allData = await fetchAllPages();
console.log("Data fetched. Saving to CSV...");
saveToCSV(allData);
};
main();