The great refactor (#82)
This commit is contained in:
40
source/plugins/anilist/README.md
Normal file
40
source/plugins/anilist/README.md
Normal file
@@ -0,0 +1,40 @@
|
||||
### 🌸 Anilist <sup>🚧 pre-release on <code>@master</code></sup>
|
||||
|
||||
The *anilist* plugin lets you display your favorites animes, mangas and characters from your [AniList](https://anilist.co) account.
|
||||
|
||||
<table>
|
||||
<td align="center">
|
||||
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.anilist.svg">
|
||||
<details><summary>Manga version</summary>
|
||||
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.anilist.manga.svg">
|
||||
</details>
|
||||
<details open><summary>Favorites characters version</summary>
|
||||
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.anilist.characters.svg">
|
||||
</details>
|
||||
<img width="900" height="1" alt="">
|
||||
</td>
|
||||
</table>
|
||||
|
||||
This plugin is composed of the following sections, which can be displayed or hidden through `plugin_anilist_sections` option:
|
||||
- `favorites` will display your favorites mangas and animes
|
||||
- `watching` will display animes currently in your watching list
|
||||
- `reading` will display manga currently in your reading list
|
||||
- `characters` will display characters you liked
|
||||
|
||||
These sections can also be filtered by media type, which can be either `anime`, `manga` or both.
|
||||
|
||||
#### ℹ️ Examples workflows
|
||||
|
||||
[➡️ Available options for this plugin](metadata.yml)
|
||||
|
||||
```yaml
|
||||
- uses: lowlighter/metrics@latest
|
||||
with:
|
||||
# ... other options
|
||||
plugin_anilist: yes
|
||||
plugin_anilist_medias: anime, manga # Display both animes and mangas
|
||||
plugin_anilist_sections: favorites, characters # Display only favorites and characters sections
|
||||
plugin_anilist_limit: 2 # Limit to 2 entry per section (characters section excluded)
|
||||
plugin_anilist_shuffle: yes # Shuffle data for more varied outputs
|
||||
plugin_anilist_user: .user.login # Use same username as GitHub login
|
||||
```
|
||||
@@ -1,42 +1,33 @@
|
||||
//Setup
|
||||
export default async function ({login, imports, q}, {enabled = false} = {}) {
|
||||
export default async function ({login, data, queries, imports, q, account}, {enabled = false} = {}) {
|
||||
//Plugin execution
|
||||
try {
|
||||
//Check if plugin is enabled and requirements are met
|
||||
if ((!enabled)||(!q.anilist))
|
||||
return null
|
||||
//Parameters override
|
||||
let {"anilist.medias":medias = ["anime", "manga"], "anilist.sections":sections = ["favorites"], "anilist.limit":limit = 2, "anilist.shuffle":shuffle = true, "anilist.user":user = login} = q
|
||||
//Medias types
|
||||
medias = decodeURIComponent(medias).split(",").map(x => x.trim().toLocaleLowerCase()).filter(x => ["anime", "manga"].includes(x))
|
||||
//Sections
|
||||
sections = decodeURIComponent(sections).split(",").map(x => x.trim().toLocaleLowerCase()).filter(x => ["favorites", "watching", "reading", "characters"].includes(x))
|
||||
//Limit medias
|
||||
limit = Math.max(0, Number(limit))
|
||||
//GraphQL queries
|
||||
const query = {
|
||||
statistics:`${await imports.fs.readFile(`${imports.__module(import.meta.url)}/queries/statistics.graphql`)}`,
|
||||
characters:`${await imports.fs.readFile(`${imports.__module(import.meta.url)}/queries/characters.graphql`)}`,
|
||||
medias:`${await imports.fs.readFile(`${imports.__module(import.meta.url)}/queries/medias.graphql`)}`,
|
||||
favorites:`${await imports.fs.readFile(`${imports.__module(import.meta.url)}/queries/favorites.graphql`)}`,
|
||||
}
|
||||
|
||||
//Load inputs
|
||||
let {limit, medias, sections, shuffle, user} = imports.metadata.plugins.anilist.inputs({data, account, q})
|
||||
|
||||
//Initialization
|
||||
const result = {user:{stats:null, genres:[]}, lists:Object.fromEntries(medias.map(type => [type, {}])), characters:[], sections}
|
||||
|
||||
//User statistics
|
||||
{
|
||||
//Query API
|
||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (user statistics)`)
|
||||
const {data:{data:{User:{statistics:stats}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user}, query:query.statistics})
|
||||
const {data:{data:{User:{statistics:stats}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user}, query:queries.anilist.statistics()})
|
||||
//Format and save results
|
||||
result.user.stats = stats
|
||||
result.user.genres = [...new Set([...stats.anime.genres.map(({genre}) => genre), ...stats.manga.genres.map(({genre}) => genre)])]
|
||||
}
|
||||
|
||||
//Medias lists
|
||||
if ((sections.includes("watching"))||(sections.includes("reading"))) {
|
||||
for (const type of medias) {
|
||||
//Query API
|
||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (medias lists - ${type})`)
|
||||
const {data:{data:{MediaListCollection:{lists}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, type:type.toLocaleUpperCase()}, query:query.medias})
|
||||
const {data:{data:{MediaListCollection:{lists}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, type:type.toLocaleUpperCase()}, query:queries.anilist.medias()})
|
||||
//Format and save results
|
||||
for (const {name, entries} of lists) {
|
||||
//Format results
|
||||
@@ -50,6 +41,7 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Favorites anime/manga
|
||||
if (sections.includes("favorites")) {
|
||||
for (const type of medias) {
|
||||
@@ -60,7 +52,7 @@
|
||||
let next = false
|
||||
do {
|
||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites ${type}s - page ${page})`)
|
||||
const {data:{data:{User:{favourites:{[type]:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:query.favorites.replace(/[$]type/g, type)})
|
||||
const {data:{data:{User:{favourites:{[type]:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.favorites({type})})
|
||||
page = cursor.currentPage
|
||||
next = cursor.hasNextPage
|
||||
list.push(...await Promise.all(nodes.map(media => format({media:{progess:null, score:null, media}, imports}))))
|
||||
@@ -74,6 +66,7 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Favorites characters
|
||||
if (sections.includes("characters")) {
|
||||
//Query API
|
||||
@@ -83,7 +76,7 @@
|
||||
let next = false
|
||||
do {
|
||||
console.debug(`metrics/compute/${login}/plugins > anilist > querying api (favorites characters - page ${page})`)
|
||||
const {data:{data:{User:{favourites:{characters:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:query.characters})
|
||||
const {data:{data:{User:{favourites:{characters:{nodes, pageInfo:cursor}}}}}} = await imports.axios.post("https://graphql.anilist.co", {variables:{name:user, page}, query:queries.anilist.characters()})
|
||||
page = cursor.currentPage
|
||||
next = cursor.hasNextPage
|
||||
for (const {name:{full:name}, image:{medium:artwork}} of nodes)
|
||||
@@ -92,6 +85,7 @@
|
||||
//Format and save results
|
||||
result.characters = shuffle ? imports.shuffle(characters) : characters
|
||||
}
|
||||
|
||||
//Results
|
||||
return result
|
||||
}
|
||||
|
||||
55
source/plugins/anilist/metadata.yml
Normal file
55
source/plugins/anilist/metadata.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
name: "🌸 Anilist"
|
||||
cost: N/A
|
||||
supports:
|
||||
- user
|
||||
- organization
|
||||
inputs:
|
||||
|
||||
# Enable or disable plugin
|
||||
plugin_anilist:
|
||||
description: Display data from your AniList account
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# Types of medias to display
|
||||
plugin_anilist_medias:
|
||||
description: Medias types to display
|
||||
type: array
|
||||
format: comma-separated
|
||||
default: anime, manga
|
||||
values:
|
||||
- anime
|
||||
- manga
|
||||
|
||||
# Sections to display
|
||||
# Values from "plugin_anilist_medias" may impact displayed sections
|
||||
plugin_anilist_sections:
|
||||
description: Sections to display
|
||||
type: array
|
||||
format: comma-separated
|
||||
default: favorites
|
||||
values:
|
||||
- favorites # Favorites animes and mangas (depending on plugin_anilist_medias values)
|
||||
- watching # Animes in your watching list
|
||||
- reading # Mangas in your reading list
|
||||
- characters # Favorites characters
|
||||
|
||||
# Number of entries to display per section (this does not impacts characters section)
|
||||
# Set to 0 to disable limitations
|
||||
plugin_anilist_limit:
|
||||
description: Maximum number of entries to display per section
|
||||
type: number
|
||||
default: 2
|
||||
min: 0
|
||||
|
||||
# Shuffle AniList data for varied outputs
|
||||
plugin_anilist_shuffle:
|
||||
description: Shuffle AniList data
|
||||
type: boolean
|
||||
default: yes
|
||||
|
||||
# Username on AniList
|
||||
plugin_anilist_user:
|
||||
type: string
|
||||
description: AniList login
|
||||
default: .user.login
|
||||
Reference in New Issue
Block a user