Skip to content

Commit bdbd91f

Browse files
committed
Refactor get_user_media to add more parameters
Added MediaType and MediaStatus enums. Added the ability to filter by type or status, or to return the users entire list.
1 parent 72e36d2 commit bdbd91f

File tree

1 file changed

+156
-8
lines changed

1 file changed

+156
-8
lines changed

get_list.py

Lines changed: 156 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,153 @@
1-
from pip import main
21
import argparse
2+
from enum import Enum
3+
from types import NoneType
4+
from typing import Optional
5+
36
from anilist_tools.upcoming_sequels import get_user_id_by_name
47
from anilist_tools.utils import depaginated_request
58

6-
STATUSES = ['CURRENT', 'PLANNING', 'COMPLETED', 'DROPPED', 'PAUSED', 'REPEATING']
9+
class MediaType(Enum):
10+
MatchAll = 'ALL'
11+
Anime = 'ANIME'
12+
Manga = 'MANGA'
713

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 = '''
1124
query ($userId: Int, $type: MediaType, $status: MediaListStatus, $page: Int, $perPage: Int) {
1225
Page (page: $page, perPage: $perPage) {
1326
pageInfo {
1427
hasNextPage
1528
}
1629
# 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) {
18151
media {
19152
id
20153
title {
@@ -47,8 +180,23 @@ def get_user_media(user_id, status='COMPLETED', type='ANIME'):
47180
}
48181
}'''
49182

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})]
52200

53201
def main(username: str):
54202
user_id = get_user_id_by_name(username)

0 commit comments

Comments
 (0)