Have you ever wanted to quickly view your CodeChef and GitHub statistics right from your console? In this blog, I’ll show you how to build a simple yet powerful Node.js script that does just that. This tool scrapes CodeChef user data and fetches GitHub user statistics using APIs, combining the power of axios and cheerio libraries.
Prerequisites
Before diving into the code, ensure you have the following installed:
Node.js (with npm or yarn)
Basic understanding of JavaScript
The Tech Stack
Axios: To make HTTP requests.
Cheerio: To parse and manipulate HTML (for web scraping).
readline: To create a console interface for user input.
How It Works
1. Scraping CodeChef Statistics
We use Cheerio to scrape CodeChef user profile pages. Cheerio allows us to parse the HTML response and extract elements like user ratings and star ratings directly from the page.
2. Fetching GitHub Statistics
GitHub provides a public API to fetch user details like followers and repository count. This eliminates the need for scraping and ensures more reliable data.
3. User Interaction
The script uses Node.js's readline
module to interact with users, asking for their CodeChef and GitHub usernames in the console.
The Code
// Import the axios library to make HTTP requests
const axios = require('axios');
// Import the cheerio library to parse and manipulate HTML
const cheerio = require('cheerio');
// Define an asynchronous function to fetch and extract CodeChef user statistics
async function getCodechefStats(username) {
const url = `https://www.codechef.com/users/${username}`;
const headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
};
try {
const response = await axios.get(url, { headers });
const $ = cheerio.load(response.data);
const rating = $('.rating-number').text().trim();
var starText = '';
if (rating < 1400)
starText = '★';
else if (rating < 1600)
starText = '★★';
else if (rating < 1800)
starText = '★★★';
else if (rating < 2000)
starText = '★★★★';
else if (rating < 2200)
starText = '★★★★★';
else if (rating < 2500)
starText = '★★★★★★';
else
starText = '★★★★★★★';
return { username, rating, stars: starText };
} catch (error) {
return `Error fetching data: ${error.message}`;
}
}
// Define an asynchronous function to fetch GitHub user statistics
async function getGithubStats(username) {
const url = `https://api.github.com/users/${username}`;
try {
const response = await axios.get(url);
const { followers, public_repos } = response.data;
return { username, followers, repositories: public_repos };
} catch (error) {
return `Error fetching data: ${error.message}`;
}
}
// Create a readline interface to interact with the user via the console
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// Ask the user for their CodeChef and GitHub usernames
readline.question('Enter CodeChef username: ', async (codechefUsername) => {
const codechefStats = await getCodechefStats(codechefUsername);
console.log('\nCodeChef Stats:');
if (typeof codechefStats === 'string') {
console.log(codechefStats); // Display error message if any
} else {
console.log('Username:', codechefStats.username);
console.log('Rating:', codechefStats.rating);
console.log('Stars:', codechefStats.stars);
}
readline.question('\nEnter GitHub username: ', async (githubUsername) => {
const githubStats = await getGithubStats(githubUsername);
console.log('\nGitHub Stats:');
if (typeof githubStats === 'string') {
console.log(githubStats); // Display error message if any
} else {
console.log('Username:', githubStats.username);
console.log('Followers:', githubStats.followers);
console.log('Repositories:', githubStats.repositories);
}
readline.close();
});
});
Key Features
CodeChef Scraper
Fetches user rating from their profile page.
Determines the star level based on the rating.
GitHub API Integration
- Fetches follower count and public repository count using GitHub’s REST API.
Interactive Console
- Asks the user for their CodeChef and GitHub usernames.
How to Run
Save the code to a file, for example,
stats.js
.Install dependencies:
npm install axios cheerio
Run the script:
node stats.js
Enter your CodeChef and GitHub usernames when prompted.
Example Output
Enter CodeChef username: aradhya_srivastava
CodeChef Stats:
Username: aradhya_srivastava
Rating: 929
Stars: ★
Enter GitHub username: octocat
GitHub Stats:
Username: octocat
Followers: 3492
Repositories: 8
Final Thoughts
This project is a great way to get started with web scraping and API integrations in Node.js. It demonstrates how to combine different tools and libraries to create a practical utility.
Feel free to customize and expand this script—maybe add more features like fetching stats from other platforms or integrating a UI!
Happy Coding! 😊