From a63d8af88f6691a1b172418e54f167a5671afdc1 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 21 Mar 2020 20:32:38 -0400 Subject: [PATCH] Add MAL Score to Anime/Manga --- README.md | 3 +++ commands/search/anime.js | 21 +++++++++++++++++++-- commands/search/manga.js | 25 ++++++++++++++++++++++--- package.json | 2 +- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3597b7a4..fd0b983a 100644 --- a/README.md +++ b/README.md @@ -843,6 +843,9 @@ here. * just-do-it ([Original Motivational Speech](https://www.youtube.com/watch?v=ZXsQAXx_ao0)) - [muffinlabs - Today in History](http://history.muffinlabs.com/) * today-in-history ([API](http://history.muffinlabs.com/#api)) +- [MyAnimeList](https://myanimelist.net/) + * anime (Score Data) + * manga (Score Data) - [Mythbusters](https://go.discovery.com/tv-shows/mythbusters) * doors (Concept) - [NASA](https://www.nasa.gov/) diff --git a/commands/search/anime.js b/commands/search/anime.js index c26f5cfa..642a926b 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); +const cheerio = require('cheerio'); const { stripIndents } = require('common-tags'); const { cleanAnilistHTML } = require('../../util/Util'); const ANILIST_USERNAME = process.env.ANILIST_USERNAME || 'dragonfire535'; @@ -21,6 +22,7 @@ const resultGraphQL = stripIndents` query media($id: Int, $type: MediaType) { Media(id: $id, type: $type) { id + idMal title { english romaji @@ -91,6 +93,11 @@ module.exports = class AnimeCommand extends Command { url: 'https://anilist.co/', reason: 'API', reasonURL: 'https://anilist.gitbook.io/anilist-apiv2-docs/' + }, + { + name: 'MyAnimeList', + url: 'https://myanimelist.net/', + reason: 'Score Data' } ], args: [ @@ -112,6 +119,7 @@ module.exports = class AnimeCommand extends Command { const anime = await this.fetchAnime(id); await this.fetchPersonalList(); const entry = this.personalList.find(ani => ani.mediaId === id); + const malScore = await this.fetchMALScore(anime.idMal); const embed = new MessageEmbed() .setColor(0x02A9FF) .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') @@ -124,8 +132,7 @@ module.exports = class AnimeCommand extends Command { .addField('❯ Season', anime.season ? `${seasons[anime.season]} ${anime.startDate.year}` : '???', true) .addField('❯ Average Score', anime.meanScore ? `${anime.meanScore}/100` : '???', true) .addField(`❯ ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '?/10', true) - .addField(`❯ ${ANILIST_USERNAME}'s Progress`, - entry && entry.status ? userStatuses[entry.status] : 'Not Planned', true); + .addField(`❯ MAL Score`, malScore ? `${malScore}/10` : '???', true); return msg.embed(embed); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); @@ -159,6 +166,16 @@ module.exports = class AnimeCommand extends Command { return body.data.Media; } + async fetchMALScore(id) { + try { + const { text } = await request.get(`https://myanimelist.net/anime/${id}`); + const $ = cheerio.load(text); + return $('span[itemprop="ratingValue"]').first().text(); + } catch { + return null; + } + } + async fetchPersonalList() { if (this.personalList) return this.personalList; const { body } = await request diff --git a/commands/search/manga.js b/commands/search/manga.js index f5b913a5..28db0e80 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); +const cheerio = require('cheerio'); const { stripIndents } = require('common-tags'); const { cleanAnilistHTML } = require('../../util/Util'); const searchGraphQL = stripIndents` @@ -14,6 +15,7 @@ const resultGraphQL = stripIndents` query media($id: Int, $type: MediaType) { Media(id: $id, type: $type) { id + idMal title { english userPreferred @@ -53,6 +55,11 @@ module.exports = class MangaCommand extends Command { url: 'https://anilist.co/', reason: 'API', reasonURL: 'https://anilist.gitbook.io/anilist-apiv2-docs/' + }, + { + name: 'MyAnimeList', + url: 'https://myanimelist.net/', + reason: 'Score Data' } ], args: [ @@ -69,7 +76,8 @@ module.exports = class MangaCommand extends Command { try { const id = await this.search(query); if (!id) return msg.say('Could not find any results.'); - const manga = await this.fetchAnime(id); + const manga = await this.fetchManga(id); + const malScore = await this.fetchMALScore(manga.idMal); const embed = new MessageEmbed() .setColor(0x02A9FF) .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') @@ -80,7 +88,8 @@ module.exports = class MangaCommand extends Command { .addField('❯ Status', statuses[manga.status], true) .addField('❯ Chapters / Volumes', `${manga.chapters || '???'}/${manga.volumes || '???'}`, true) .addField('❯ Year', manga.startDate.year || '???', true) - .addField('❯ Average Score', manga.meanScore ? `${manga.meanScore}/100` : '???', true); + .addField('❯ Average Score', manga.meanScore ? `${manga.meanScore}/100` : '???', true) + .addField(`❯ MAL Score`, malScore ? `${malScore}/10` : '???', true); return msg.embed(embed); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); @@ -101,7 +110,7 @@ module.exports = class MangaCommand extends Command { return body.data.anime.results[0].id; } - async fetchAnime(id) { + async fetchManga(id) { const { body } = await request .post('https://graphql.anilist.co/') .send({ @@ -113,4 +122,14 @@ module.exports = class MangaCommand extends Command { }); return body.data.Media; } + + async fetchMALScore(id) { + try { + const { text } = await request.get(`https://myanimelist.net/manga/${id}`); + const $ = cheerio.load(text); + return $('span[itemprop="ratingValue"]').first().text(); + } catch { + return null; + } + } }; diff --git a/package.json b/package.json index db8d320c..1b7b6d28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.12.1", + "version": "112.12.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {