From fc0d59178af509c1d0b54354172e1e39ef140079 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 21 Mar 2020 20:42:56 -0400 Subject: [PATCH] Add Personal Score to Manga --- commands/search/anime.js | 12 ++------- commands/search/manga.js | 57 ++++++++++++++++++++++++++++++++++++---- package.json | 2 +- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/commands/search/anime.js b/commands/search/anime.js index 642a926b..30f2ecac 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -69,20 +69,12 @@ const statuses = { NOT_YET_RELEASED: 'Unreleased', CANCELLED: 'Cancelled' }; -const userStatuses = { - CURRENT: 'Watching', - PLANNING: 'Planning', - COMPLETED: 'Completed', - DROPPED: 'Dropped', - PAUSED: 'Paused', - REPEATING: 'Rewatching' -}; module.exports = class AnimeCommand extends Command { constructor(client) { super(client, { name: 'anime', - aliases: ['anilist-anime', 'anilist'], + aliases: ['anilist-anime', 'anilist', 'ani', 'myanimelist', 'mal', 'mal-score'], group: 'search', memberName: 'anime', description: 'Searches AniList for your query, getting anime results.', @@ -117,7 +109,7 @@ module.exports = class AnimeCommand extends Command { const id = await this.search(query); if (!id) return msg.say('Could not find any results.'); const anime = await this.fetchAnime(id); - await this.fetchPersonalList(); + if (!this.personalList) await this.fetchPersonalList(); const entry = this.personalList.find(ani => ani.mediaId === id); const malScore = await this.fetchMALScore(anime.idMal); const embed = new MessageEmbed() diff --git a/commands/search/manga.js b/commands/search/manga.js index 28db0e80..babed36d 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -4,10 +4,17 @@ 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'; const searchGraphQL = stripIndents` query ($search: String, $type: MediaType, $isAdult: Boolean) { - anime: Page (perPage: 1) { - results: media (type: $type, isAdult: $isAdult, search: $search) { id } + anime: Page (perPage: 10) { + results: media (type: $type, isAdult: $isAdult, search: $search) { + id + title { + english + romaji + } + } } } `; @@ -18,9 +25,12 @@ const resultGraphQL = stripIndents` idMal title { english - userPreferred + romaji + } + coverImage { + large + medium } - coverImage { large } startDate { year } description(asHtml: false) siteUrl @@ -33,6 +43,20 @@ const resultGraphQL = stripIndents` } } `; +const personalGraphQL = stripIndents` + query ($name: String, $type: MediaType) { + MediaListCollection(userName: $name, type: $type) { + lists { + entries { + mediaId + score(format: POINT_10) + status + } + name + } + } + } +`; const statuses = { FINISHED: 'Finished', RELEASING: 'Releasing', @@ -70,6 +94,8 @@ module.exports = class MangaCommand extends Command { } ] }); + + this.personalList = null; } async run(msg, { query }) { @@ -77,18 +103,21 @@ module.exports = class MangaCommand extends Command { const id = await this.search(query); if (!id) return msg.say('Could not find any results.'); const manga = await this.fetchManga(id); + if (!this.personalList) await this.fetchPersonalList(); + const entry = this.personalList.find(manga => manga.mediaId === 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/') .setURL(manga.siteUrl) .setThumbnail(manga.coverImage.large || manga.coverImage.medium || null) - .setTitle(manga.title.english || manga.title.userPreferred) + .setTitle(manga.title.english || manga.title.romaji) .setDescription(manga.description ? cleanAnilistHTML(manga.description) : 'No description.') .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(`❯ ${ANILIST_USERNAME}'s Score`, entry && entry.score ? `${entry.score}/10` : '?/10', true) .addField(`❯ MAL Score`, malScore ? `${malScore}/10` : '???', true); return msg.embed(embed); } catch (err) { @@ -132,4 +161,22 @@ module.exports = class MangaCommand extends Command { return null; } } + + async fetchPersonalList() { + if (this.personalList) return this.personalList; + const { body } = await request + .post('https://graphql.anilist.co/') + .send({ + variables: { + name: ANILIST_USERNAME, + type: 'MANGA' + }, + query: personalGraphQL + }); + const { lists } = body.data.MediaListCollection; + this.personalList = []; + for (const list of Object.values(lists)) this.personalList.push(...list.entries); + setTimeout(() => { this.personalList = null; }, 3.6e+6); + return this.personalList; + } }; diff --git a/package.json b/package.json index 1b7b6d28..f3510e14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.12.2", + "version": "112.12.3", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {