|
1 |
| -from pip import main |
2 | 1 | import argparse
|
| 2 | +from enum import Enum |
| 3 | +from types import NoneType |
| 4 | +from typing import Optional |
| 5 | + |
3 | 6 | from anilist_tools.upcoming_sequels import get_user_id_by_name
|
4 | 7 | from anilist_tools.utils import depaginated_request
|
5 | 8 |
|
6 |
| -STATUSES = ['CURRENT', 'PLANNING', 'COMPLETED', 'DROPPED', 'PAUSED', 'REPEATING'] |
| 9 | +class MediaType(Enum): |
| 10 | + MatchAll = 'ALL' |
| 11 | + Anime = 'ANIME' |
| 12 | + Manga = 'MANGA' |
7 | 13 |
|
8 |
| -def get_user_media(user_id, status='COMPLETED', type='ANIME'): |
9 |
| - """Given an AniList user ID, fetch the user's anime list, returning a list of shows and details.""" |
10 |
| - query = ''' |
| 14 | +class MediaStatus(Enum): |
| 15 | + MatchAll = 'ALL' |
| 16 | + Watching = 'CURRENT' |
| 17 | + Planning = 'PLANNING' |
| 18 | + Completed = 'COMPLETED' |
| 19 | + Dropped = 'DROPPED' |
| 20 | + Paused = 'PAUSED' |
| 21 | + Rewatching = 'REPEATING' |
| 22 | + |
| 23 | +QUERY_SPECIFIC_TYPE_AND_STATUS = ''' |
11 | 24 | query ($userId: Int, $type: MediaType, $status: MediaListStatus, $page: Int, $perPage: Int) {
|
12 | 25 | Page (page: $page, perPage: $perPage) {
|
13 | 26 | pageInfo {
|
14 | 27 | hasNextPage
|
15 | 28 | }
|
16 | 29 | # Note that a MediaList object is actually a single list entry, hence the need for pagination
|
17 |
| - mediaList(userId: $userId, type: $type, status: $status, sort: SCORE_DESC) { |
| 30 | + mediaList(userId: $userId, type: $type, status: $status, sort: MEDIA_ID) { |
| 31 | + media { |
| 32 | + id |
| 33 | + title { |
| 34 | + english |
| 35 | + romaji |
| 36 | + } |
| 37 | + type |
| 38 | + format |
| 39 | + episodes |
| 40 | + } |
| 41 | + score |
| 42 | + status |
| 43 | + progress |
| 44 | + repeat |
| 45 | + startedAt { |
| 46 | + year |
| 47 | + month |
| 48 | + day |
| 49 | + } |
| 50 | + completedAt { |
| 51 | + year |
| 52 | + month |
| 53 | + day |
| 54 | + } |
| 55 | + updatedAt |
| 56 | + notes |
| 57 | + hiddenFromStatusLists |
| 58 | + customLists |
| 59 | + } |
| 60 | + } |
| 61 | +}''' |
| 62 | + |
| 63 | +QUERY_SPECIFIC_TYPE = ''' |
| 64 | +query ($userId: Int, $type: MediaType, $page: Int, $perPage: Int) { |
| 65 | + Page (page: $page, perPage: $perPage) { |
| 66 | + pageInfo { |
| 67 | + hasNextPage |
| 68 | + } |
| 69 | + # Note that a MediaList object is actually a single list entry, hence the need for pagination |
| 70 | + mediaList(userId: $userId, type: $type, sort: MEDIA_ID) { |
| 71 | + media { |
| 72 | + id |
| 73 | + title { |
| 74 | + english |
| 75 | + romaji |
| 76 | + } |
| 77 | + type |
| 78 | + format |
| 79 | + episodes |
| 80 | + } |
| 81 | + score |
| 82 | + status |
| 83 | + progress |
| 84 | + repeat |
| 85 | + startedAt { |
| 86 | + year |
| 87 | + month |
| 88 | + day |
| 89 | + } |
| 90 | + completedAt { |
| 91 | + year |
| 92 | + month |
| 93 | + day |
| 94 | + } |
| 95 | + updatedAt |
| 96 | + notes |
| 97 | + hiddenFromStatusLists |
| 98 | + customLists |
| 99 | + } |
| 100 | + } |
| 101 | +}''' |
| 102 | + |
| 103 | +QUERY_SPECIFIC_STATUS = ''' |
| 104 | +query ($userId: Int, $status: MediaListStatus, $page: Int, $perPage: Int) { |
| 105 | + Page (page: $page, perPage: $perPage) { |
| 106 | + pageInfo { |
| 107 | + hasNextPage |
| 108 | + } |
| 109 | + # Note that a MediaList object is actually a single list entry, hence the need for pagination |
| 110 | + mediaList(userId: $userId, status: $status, sort: MEDIA_ID) { |
| 111 | + media { |
| 112 | + id |
| 113 | + title { |
| 114 | + english |
| 115 | + romaji |
| 116 | + } |
| 117 | + type |
| 118 | + format |
| 119 | + episodes |
| 120 | + } |
| 121 | + score |
| 122 | + status |
| 123 | + progress |
| 124 | + repeat |
| 125 | + startedAt { |
| 126 | + year |
| 127 | + month |
| 128 | + day |
| 129 | + } |
| 130 | + completedAt { |
| 131 | + year |
| 132 | + month |
| 133 | + day |
| 134 | + } |
| 135 | + updatedAt |
| 136 | + notes |
| 137 | + hiddenFromStatusLists |
| 138 | + customLists |
| 139 | + } |
| 140 | + } |
| 141 | +}''' |
| 142 | + |
| 143 | +QUERY_RETURN_ALL = ''' |
| 144 | +query ($userId: Int, $page: Int, $perPage: Int) { |
| 145 | + Page (page: $page, perPage: $perPage) { |
| 146 | + pageInfo { |
| 147 | + hasNextPage |
| 148 | + } |
| 149 | + # Note that a MediaList object is actually a single list entry, hence the need for pagination |
| 150 | + mediaList(userId: $userId, sort: MEDIA_ID) { |
18 | 151 | media {
|
19 | 152 | id
|
20 | 153 | title {
|
@@ -47,8 +180,23 @@ def get_user_media(user_id, status='COMPLETED', type='ANIME'):
|
47 | 180 | }
|
48 | 181 | }'''
|
49 | 182 |
|
50 |
| - return [list_entry for list_entry in depaginated_request(query=query, |
51 |
| - variables={'userId': user_id, 'type': type, 'status': status})] |
| 183 | + |
| 184 | +def get_user_media(user_id: int, |
| 185 | + status: Optional[MediaStatus]=MediaStatus.MatchAll, |
| 186 | + type: Optional[MediaType]=MediaType.MatchAll) -> list[dict]: |
| 187 | + """Given an AniList user ID, fetch the user's anime list, returning a list of shows and details.""" |
| 188 | + if status is not MediaStatus.MatchAll and type is not MediaType.MatchAll: |
| 189 | + return [list_entry for list_entry in depaginated_request(query=QUERY_SPECIFIC_TYPE_AND_STATUS, |
| 190 | + variables={'userId': user_id, 'type': type.value, 'status': status.value})] |
| 191 | + elif status is not MediaStatus.MatchAll: |
| 192 | + return [list_entry for list_entry in depaginated_request(query=QUERY_SPECIFIC_STATUS, |
| 193 | + variables={'userId': user_id, 'status': status.value})] |
| 194 | + elif type is not MediaType.MatchAll: |
| 195 | + return [list_entry for list_entry in depaginated_request(query=QUERY_SPECIFIC_TYPE, |
| 196 | + variables={'userId': user_id, 'type': type.value})] |
| 197 | + else: |
| 198 | + return [list_entry for list_entry in depaginated_request(query=QUERY_RETURN_ALL, |
| 199 | + variables={'userId': user_id})] |
52 | 200 |
|
53 | 201 | def main(username: str):
|
54 | 202 | user_id = get_user_id_by_name(username)
|
|
0 commit comments