From ce3d459f69d1e54f4d9d92b6da361979ea416b79 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 26 Jun 2020 11:57:42 -0400 Subject: [PATCH] MAL Scores Work Again --- README.md | 3 +++ commands/search/anime.js | 23 +++++++++++++++++++++-- commands/search/manga.js | 23 +++++++++++++++++++++-- package.json | 2 +- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d05ce199..cd491475 100644 --- a/README.md +++ b/README.md @@ -1251,6 +1251,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) - [Myinstants](https://www.myinstants.com/index/us/) * soundboard ([Various Meme Sounds](https://www.myinstants.com/search/?name=meme)) - [Mythbusters](https://go.discovery.com/tv-shows/mythbusters) diff --git a/commands/search/anime.js b/commands/search/anime.js index 897539f4..721f8070 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -1,8 +1,9 @@ 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 { cleanAnilistHTML, embedURL } = require('../../util/Util'); const ANILIST_USERNAME = process.env.ANILIST_USERNAME || 'dragonfire535'; const searchGraphQL = stripIndents` query ($search: String, $type: MediaType, $isAdult: Boolean) { @@ -21,6 +22,7 @@ const resultGraphQL = stripIndents` query media($id: Int, $type: MediaType) { Media(id: $id, type: $type) { id + idMal title { english romaji @@ -88,6 +90,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: [ @@ -109,6 +116,8 @@ module.exports = class AnimeCommand extends Command { const anime = await this.fetchAnime(id); if (!this.personalList) await this.fetchPersonalList(); const entry = this.personalList.find(ani => ani.mediaId === id); + const malScore = await this.fetchMALScore(anime.idMal); + const malURL = `https://myanimelist.net/anime/${anime.idMal}`; const embed = new MessageEmbed() .setColor(0x02A9FF) .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') @@ -119,8 +128,8 @@ module.exports = class AnimeCommand extends Command { .addField('❯ Status', statuses[anime.status], true) .addField('❯ Episodes', anime.episodes || '???', true) .addField('❯ Season', anime.season ? `${seasons[anime.season]} ${anime.startDate.year}` : '???', true) - .addField('❯ Mean Score', anime.meanScore ? `${anime.meanScore}%` : '???', true) .addField('❯ Average Score', anime.averageScore ? `${anime.averageScore}%` : '???', true) + .addField(`❯ MAL Score`, malScore ? embedURL(malScore, malURL) : '???', true) .addField(`❯ ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '???', true) .addField('❯ External Links', anime.externalLinks.length ? anime.externalLinks.map(link => `[${link.site}](${link.url})`).join(', ') @@ -158,6 +167,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 21ade2c4..e80ee677 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -1,8 +1,9 @@ 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 { cleanAnilistHTML, embedURL } = require('../../util/Util'); const ANILIST_USERNAME = process.env.ANILIST_USERNAME || 'dragonfire535'; const searchGraphQL = stripIndents` query ($search: String, $type: MediaType, $isAdult: Boolean) { @@ -21,6 +22,7 @@ const resultGraphQL = stripIndents` query media($id: Int, $type: MediaType) { Media(id: $id, type: $type) { id + idMal title { english romaji @@ -82,6 +84,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: [ @@ -103,6 +110,8 @@ module.exports = class MangaCommand extends Command { const manga = await this.fetchManga(id); if (!this.personalList) await this.fetchPersonalList(); const entry = this.personalList.find(ma => ma.mediaId === id); + const malScore = await this.fetchMALScore(manga.idMal); + const malURL = `https://myanimelist.net/manga/${manga.idMal}`; const embed = new MessageEmbed() .setColor(0x02A9FF) .setAuthor('AniList', 'https://i.imgur.com/iUIRC7v.png', 'https://anilist.co/') @@ -113,8 +122,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('❯ Mean Score', manga.meanScore ? `${manga.meanScore}%` : '???', true) .addField('❯ Average Score', manga.averageScore ? `${manga.averageScore}%` : '???', true) + .addField(`❯ MAL Score`, malScore ? embedURL(malScore, malURL) : '???', true) .addField(`❯ ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '???', true) .addField('❯ External Links', manga.externalLinks.length ? manga.externalLinks.map(link => `[${link.site}](${link.url})`).join(', ') @@ -152,6 +161,16 @@ 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; + } + } + async fetchPersonalList() { if (this.personalList) return this.personalList; const { body } = await request diff --git a/package.json b/package.json index 0607aad4..a6fb38dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.37.3", + "version": "116.37.4", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {