theSoberSobber / AlertBot

Alert Bot for Colleges! (yes, for all of em!)
https://alertbot.vercel.app
GNU General Public License v3.0
4 stars 0 forks source link

Leetcode Plugin [Rating Predictor] #22

Closed theSoberSobber closed 3 months ago

theSoberSobber commented 4 months ago
const fs = require('fs/promises');

class Leetcode {
    /**
     * Creates an instance of Leetcode.
     * @param {string} userDataFile - The path to the file containing user data.
     * @param {string} groupId - The ID of the user group.
     */
    constructor(userDataFile, groupId) {
        this.userDataFile = userDataFile;
        this.groupId = groupId;
        this.LEETCODE_PREDICT_URL = "https://lccn.lbao.site/api/v1";
        this.LEETCODE_MULTIPLE_USER_PREDICTIONS = this.LEETCODE_PREDICT_URL + "/contest-records/predicted-rating";
        this.userData = [];
    }

    async initializeUserData() {
        try {
            const userData = await fs.readFile(this.userDataFile, 'utf8');
            const userGroups = JSON.parse(userData);
            this.userData = userGroups[this.groupId] || [];
        } catch (error) {
            console.error('Error initializing user data:', error);
        }
    }

    async fetchMultipleRatingPredictions(contestName) {
        try {
            const url = this.LEETCODE_MULTIPLE_USER_PREDICTIONS;
            const response = await fetch(url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    contest_name: contestName,
                    users: this.userData.map(user => ({ username: user, data_region: 'US' }))
                })
            });
            const jsonData = await response.json();
            const predictions = {};
            jsonData.forEach(jsonRow => {
                if (jsonRow && this.userData.includes(jsonRow.username)) {
                    predictions[jsonRow.username] = { newRating: jsonRow.new_rating, deltaRating: jsonRow.delta_rating };
                }
            });
            return predictions;
        } catch (error) {
            console.error('Error:', error);
            return {};
        }
    }

    async getFormattedMessage(contest) {
        try {
            if (this.userData.length === 0) await this.initializeUserData();
            const predictions = await this.fetchMultipleRatingPredictions(contest);
            let message = 'PREDICTIONS\n';
            for (const [username, prediction] of Object.entries(predictions)) {
                message += `${username}: ${prediction.newRating}\n`;
            }
            return message;
        } catch (error) {
            console.error('Error generating message:', error);
            return 'Error generating message';
        }
    }
}

// Example usage:
const leetcode = new Leetcode('./data/lc-users.txt', 'groupId1');
const contestName = "someContestName";
leetcode.getFormattedMessage(contestName)
    .then(message => console.log(message))
    .catch(error => console.error('Error:', error));

module.exports = Leetcode;
theSoberSobber commented 3 months ago

Done.