codewars-scraper
v1.0.14
Published
**codewars-scraper** is a library that allows you to fetch personalized statistics for users of [Codewars](https://www.codewars.com). You can easily retrieve information such as honor points, rank, and the number of completed challenges for one or more us
Readme
codewars-scraper
codewars-scraper is a library that allows you to fetch personalized statistics for users of Codewars. You can easily retrieve information such as honor points, rank, and the number of completed challenges for one or more users.
Installation
First, install the library via npm:
npm install codewars-scraperFeatures
With codewars-scraper, you can:
- Retrieve statistics for a single user (honor, rank, completed challenges).
- Read user data from a JSON file and process it.
- Write user statistics sorted by honor points to a JSON file.
Usage
1. Import the Library
Import the functions from the library:
import { writeDataToJSON, getStats, getDataFromJSON } from 'codewars-scraper';2. Fetch Statistics for a Single User
const username = 'someUser';
const stats = await getStats(username);
console.log(stats);
// Output: { honor: 1234, rank: 5, completeNumber: 42 }3. Fetch Statistics from a JSON File
If you have a JSON file containing a list of users, you can fetch their statistics.
Example JSON File
users.json:
[
{
"name": "Luca",
"alias": "LucaCodewars"
},
{
"name": "Maria",
"alias": "MariaKata"
}
]Fetch Data from the JSON File:
const userStats = await getDataFromJSON('./users.json');
console.log(userStats);
/* Output:
[
{ name: "Luca", honor: 1500, rank: 3, completeNumber: 50 },
{ name: "Maria", honor: 1200, rank: 5, completeNumber: 40 }
]
*/4. Write Data to a Sorted JSON File
You can take user data from a JSON input file, sort it by honor points, and write the results to a new JSON file.
Example:
await writeDataToJSON('./users.json', './output.json');Generated File (output.json):
[
"Name Honor Rank Completed Challenges",
"LUCA 1500 3 50",
"MARIA 1200 5 40"
]API
getStats(username: string): Promise<{ honor: number, rank: number, completeNumber: number }>
Fetch the statistics for a single Codewars user.
- Parameters:
username(string): The alias of the user on Codewars.
- Returns:
- An object with the following properties:
honor(number): The user’s total honor points.rank(number): The user’s rank.completeNumber(number): The total number of completed challenges.
- An object with the following properties:
getCompleatedKata(username: string): Promise<number>
Fetch the number of challenges completed by a user.
getHonorAndRank(username: string): Promise<{ honor: number, rank: number }>
Fetch the honor points and rank for a user.
getDataFromJSON(jsonPath: string): Promise<User[]>
Reads a JSON file containing a list of users and retrieves their statistics.
- Parameters:
jsonPath(string): Path to the input JSON file.
- Returns:
- An array of
Userobjects with the user statistics.
- An array of
writeDataToJSON(inputPath: string, outputPath: string): Promise<void>
Reads a JSON file containing user data, retrieves their statistics, sorts the data by honor points, and writes the results to a new JSON file.
- Parameters:
inputPath(string): Path to the input JSON file.outputPath(string): Path to the output JSON file.
Available Types
User
type User = {
name: string;
honor: number;
rank: number;
completeNumber: number;
};UserJSON
type UserJSON = {
name: string;
alias: string;
};Dependencies
- axios: For making HTTP requests.
- cheerio: For extracting and parsing HTML content.
License
This package is distributed under the ISC license. Feel free to use and modify it as you see fit.
