Last active
October 16, 2017 10:24
-
-
Save zfbx/7d6db82caa0af4574042baab842e1e2d to your computer and use it in GitHub Desktop.
nodejs module for interfacing with MyAnimeList profile and my discord bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const got = require('got'); | |
const parseString = require('xml2js').parseString; | |
/** | |
* @typedef {Object} profile | |
* @property {string} search - The string used to search | |
* @property {string} id - User ID | |
* @property {string} name - username in proper camelCase (potentially same as search) | |
* @property {string} days - Total number of days watching | |
* @property {string} watching - Number of shows watching | |
* @property {string} completed - Number of shows completed | |
* @property {string} onhold - Number of shows on-hold | |
* @property {string} planned - Number of shows planned | |
* @property {string} picture - URL of the profile picture | |
* @property {string} url - URL of the profile | |
* @property {string} anime - URL of the anime list | |
* @property {string} manga - URL of the manga list | |
*/ | |
/** | |
* @param {string} search - The user profile to get the data of. | |
* @param {boolean} [debug=false] - optional console output for checking errors | |
* @return {profile} - Output of the profile Object | |
*/ | |
module.exports = function (search, debug = false) { | |
return new Promise(function (fulfill, reject) { | |
let profile = {}; | |
if(!search) { | |
if (debug) | |
console.error('No username provided.'); | |
return fulfill("No username provided."); | |
} | |
profile.search = search.replace(/[^A-Za-z0-9_-]/g,''); | |
if(profile.search === ""){ | |
if (debug) | |
console.error('Invalid username.'); | |
return fulfill("Invalid username."); | |
} | |
got("https://myanimelist.net/malappinfo.php?u=" + search) | |
.then(res => { | |
parseString(res.body, function(err, result) { | |
if (!result.myanimelist) { | |
if (debug) | |
console.log("This doesn't seem to be a real MyAnimeList Profile.."); | |
return fulfill("This doesn't seem to be a real MyAnimeList Profile.."); | |
} | |
profile.id = result.myanimelist.myinfo[0].user_id.toString(); | |
profile.name = result.myanimelist.myinfo[0].user_name.toString(); | |
profile.days = result.myanimelist.myinfo[0].user_days_spent_watching.toString(); | |
profile.watching = result.myanimelist.myinfo[0].user_watching.toString(); | |
profile.completed = result.myanimelist.myinfo[0].user_completed.toString(); | |
profile.onhold = result.myanimelist.myinfo[0].user_onhold.toString(); | |
profile.planned = result.myanimelist.myinfo[0].user_plantowatch.toString(); | |
profile.dropped = result.myanimelist.myinfo[0].user_dropped.toString(); | |
profile.picture = "https://myanimelist.cdn-dena.com/images/userimages/"+profile.id+".jpg"; | |
profile.url = "https://myanimelist.net/profile/" + search; | |
profile.anime = "https://myanimelist.net/animelist/" + search; | |
profile.manga = "https://myanimelist.net/mangalist/" + search; | |
if (debug) | |
console.log(profile); | |
return fulfill(profile); | |
}); | |
}).catch(error => { | |
console.error(error) | |
return fulfill("Error grabbing profile."); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment