-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanilist.groovy
97 lines (89 loc) · 2.91 KB
/
anilist.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//file:noinspection unused
//file:noinspection GrMethodMayBeStatic
package lib
import net.filebot.Logging
//--- VERSION 1.1.2
// https://http-builder-ng.github.io/http-builder-ng/
@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.4')
// I'm not sure why these *NEED* to be import static, but it blows up otherwise.
import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.ContentTypes.JSON
import groovyx.net.http.*
/**
* Function to LinkedHashMap of Anilist ID and Title
*
* @param animeSeason The Anime Season
* @param animeYear The Year
* @return Results
*/
Map anilistGetSeasonTitles(String animeSeason, Integer animeYear){
// ---------- Variable Declaration ---------- //
Integer queryPage = 1
LinkedHashMap anilistSeasonTitles = [:]
// ---------- Get Json Response to Request ---------- //
Map anilistQueryResponseJson = queryAnilistForSeason(queryPage, animeSeason, animeYear)
// ---------- Populate Season Titles ---------- //
anilistQueryResponseJson.data.Page.media.each { title ->
Logging.log.finest "ID: ${title.id}, Title: ${title.title.romaji}"
anilistSeasonTitles.put(title.id, title.title.romaji)
}
// log.finest "${anilistSeasonTitles}"
// ---------- Get/Parse additional pages as needed ---------- //
while ( anilistQueryResponseJson.data.Page.pageInfo.hasNextPage == true ) {
Logging.log.finest 'Need to fetch Another page'
queryPage++
Logging.log.finest "queryPage: ${queryPage}"
anilistQueryResponseJson = queryAnilistForSeason(queryPage, animeSeason, animeYear)
anilistQueryResponseJson.data.Page.media.each { title ->
Logging.log.finest "ID: ${title.id}, Title: ${title.title.romaji}"
anilistSeasonTitles.put(title.id, title.title.romaji)
}
}
Logging.log.finest "${anilistSeasonTitles}"
return anilistSeasonTitles
}
/**
* Function to return JSON results of Season Query
*
* @param queryPage Query Page
* @param animeSeason The Anime Season
* @param animeYear The Year
* @return Query results
*/
Map queryAnilistForSeason(Integer queryPage, String animeSeason, Integer animeYear ) {
String query = """\
query (\$season: MediaSeason, \$year: Int) {
Page(page: ${queryPage}, perPage: 50) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media(season: \$season, seasonYear: \$year) {
id
title {
romaji
}
}
}
}
"""
String queryVariables = """\
{
"season": "${animeSeason}",
"year": ${animeYear}
}
"""
return postResponse = configure {
request.uri = 'https://graphql.anilist.co/'
request.contentType = JSON[0]
}.post {
request.body = [query: query, variables: queryVariables] // Works!
response.failure { FromServer resp, Object body ->
Logging.log.severe "POST request failed, HTTP - ${resp.properties}"
Logging.log.severe "${body}"
}
}
}