-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
anilist_getMedia.py
232 lines (204 loc) · 10.7 KB
/
anilist_getMedia.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Imports
import os
from datetime import datetime
import array as arr
# Local Imports
import func.main as fMain
import func.anilist_request as fReq
fMain.logString("Imported func.anilist_getMedia", "")
# Main Function
def getMediaEntries(mediaType, paramvals):
# Vars and Objects
userMal = None
filepath = str(paramvals['root'])
entryLog = str(paramvals['log'])
accessToken = str(paramvals['access_tkn'])
userID = int(paramvals['user_id'])
useOAuth = bool(paramvals['use_auth'])
isSepNsfw = bool(paramvals['sep_nsfw'])
isClearFile = bool(paramvals['clear_files'])
if paramvals['user_mal']:
userMal = str(paramvals['user_mal'])
returnMedia = {}
entryID = [] # List of IDs, to prevent duplicates
jsonToDump = [] # List of Json dict object of results
jsonToDumpNsfw = [] # List of Json dict object of results, 18+ entries
isAdult = False # Bool for 'isAdult' flag from Anilist
isExportMal = True # Export to MAL backup.
nsfwToggle = 0 # 0=main, 1=nsfw. For arrays toggle
source = "anilist_get" + mediaType
fMain.logString("All vars are initiated", source)
if not userMal:
isExportMal = False
fMain.logString("Skipping MAL export.", source)
else:
fMain.logString(f"MAL Username: {userMal}", source)
# Declare filepaths
if mediaType == "ANIME":
outputMedia = os.path.join(filepath, "output", "anime_" + datetime.now().strftime("%Y-%m-%d") + ".json")
xmlMedia = os.path.join(filepath, "output", "anime_" + datetime.now().strftime("%Y-%m-%d") + ".xml")
outputMedia18 = os.path.join(filepath, "output", "nsfw_anime_" + datetime.now().strftime("%Y-%m-%d") + ".json")
xmlMedia18 = os.path.join(filepath, "output", "nsfw_anime_" + datetime.now().strftime("%Y-%m-%d") + ".xml")
else:
outputMedia = os.path.join(filepath, "output", "manga_" + datetime.now().strftime("%Y-%m-%d") + ".json")
xmlMedia = os.path.join(filepath, "output", "manga_" + datetime.now().strftime("%Y-%m-%d") + ".xml")
outputMedia18 = os.path.join(filepath, "output", "nsfw_manga_" + datetime.now().strftime("%Y-%m-%d") + ".json")
xmlMedia18 = os.path.join(filepath, "output", "nsfw_manga_" + datetime.now().strftime("%Y-%m-%d") + ".xml")
# Clear files if already existing
fMain.logString(f"Will clear output files: {isClearFile}")
if isClearFile:
try:
fMain.logString("Deleting output files..")
fMain.deleteFile(outputMedia)
fMain.deleteFile(xmlMedia)
fMain.deleteFile(outputMedia18)
fMain.deleteFile(xmlMedia18)
fMain.logString("Done deleting output files!")
except Exception as e:
fMain.logString(f"Clear File error: {e}", source)
# Check if not existing
if not (os.path.exists(outputMedia)):
# Get JSON object
if useOAuth:
jsonMedia = fReq.anilist_userlist(accessToken, userID, mediaType)
else:
jsonMedia = fReq.anilist_userlist_public(userID, mediaType)
# Check if not null
if jsonMedia is not None:
listMedia = jsonMedia["data"]["MediaListCollection"]["lists"]
# Create vars
# Count Manga entries
cTotal = arr.array('i', [0, 0])
cWatch = arr.array('i', [0, 0])
cComplete = arr.array('i', [0, 0])
cHold = arr.array('i', [0, 0])
cDrop = arr.array('i', [0, 0])
cPtw = arr.array('i', [0, 0])
# Start generating JSON and XML..
fMain.logString("Generating export files..", source)
# Log duplicate entries
fMain.logFile(entryLog, f'{mediaType} Entries')
entryID.clear() # Clear list
# Iterate over the MediaCollection List
for anime in listMedia:
# Get entries
animeInfo = anime["entries"]
# Iterate over the anime information, inside the entries
for entry in animeInfo:
# Get Anilist ID
anilistID = entry["media"]["id"]
# Get Anilist Status
AnilistStatus = fMain.validateStr(entry["status"])
# Get isAdult flag
isAdult = bool(entry["media"]["isAdult"])
# Check if already exists
if anilistID in entryID:
fMain.logFile(entryLog, f'Skipped: {str(anilistID)}, Duplicate {mediaType} entry.')
continue
else:
entryID.append(anilistID)
# Write to json file
if isAdult and isSepNsfw:
jsonToDumpNsfw.append(fMain.entry_json(entry, mediaType))
else:
jsonToDump.append(fMain.entry_json(entry, mediaType))
# Write to MAL Xml File
malID = fMain.validateInt(entry["media"]["idMal"])
if malID != '0' and isExportMal:
# Get XML strings
xmltoWrite = fMain.entry_xmlstr(mediaType, malID, entry, str(AnilistStatus))
# Write to xml file
if isAdult and isSepNsfw:
nsfwToggle = 1
fMain.write_append(xmlMedia18, xmltoWrite)
else:
nsfwToggle = 0
fMain.write_append(xmlMedia, xmltoWrite)
# Add count
cTotal[nsfwToggle] += 1
if (AnilistStatus == "COMPLETED"):
cComplete[nsfwToggle] += 1
elif (AnilistStatus == "PAUSED"):
cHold[nsfwToggle] += 1
elif (AnilistStatus == "CURRENT"):
cWatch[nsfwToggle] += 1
elif (AnilistStatus == "DROPPED"):
cDrop[nsfwToggle] += 1
elif (AnilistStatus == "PLANNING"):
cPtw[nsfwToggle] += 1
elif (AnilistStatus == "REPEATING"):
cWatch[nsfwToggle] += 1
# Dump JSON to file..
if (fMain.dumpToJson(jsonToDump, outputMedia)):
fMain.logString("Succesfully created json file!", source)
else:
fMain.logString("Error with creating json file!", source)
fMain.logString(f"Done with {mediaType} JSON file..", source)
# Dump JSON (nsfw) to file..
if isSepNsfw:
if (fMain.dumpToJson(jsonToDumpNsfw, outputMedia18)):
fMain.logString("Succesfully created json file!", source)
else:
fMain.logString("Error with creating json file!", source)
fMain.logString(f"Done with {mediaType} JSON file..", source)
# Write to MAL xml file
if isExportMal:
fMain.logString(f"Finalizing {mediaType} XML file..", source)
malprepend = ""
malprepend18 = ""
mediastring = ""
mediaexport = '0'
mediawatchread = ""
if mediaType == "ANIME":
mediastring = "anime"
mediaexport = '1'
mediawatchread = "watch"
else:
mediastring = "manga"
mediaexport = '2'
mediawatchread = "read"
fMain.write_append(xmlMedia, f'</my{mediastring}list>')
if isSepNsfw:
fMain.write_append(xmlMedia18, f'</my{mediastring}list>')
# Total counts for MAL
fMain.logString(f"Prepend 'myinfo' to {mediaType} XML file..", source)
malprepend = f'<?xml version="1.0" encoding="UTF-8" ?>\n<my{mediastring}list>\n'
malprepend += '\t<myinfo>\n'
malprepend += '\t\t' + fMain.toMalval('', 'user_id') + '\n'
malprepend += '\t\t' + fMain.toMalval(userMal, 'user_name') + '\n'
malprepend += '\t\t' + fMain.toMalval(mediaexport, 'user_export_type') + '\n'
malprepend18 = malprepend # Same prepend values as 'main'
# Count for 'main'
malprepend += '\t\t' + fMain.toMalval(str(cTotal[0]), f'user_total_{mediastring}') + '\n'
malprepend += '\t\t' + fMain.toMalval(str(cWatch[0]), f'user_total_{mediawatchread}ing') + '\n'
malprepend += '\t\t' + fMain.toMalval(str(cComplete[0]), 'user_total_completed') + '\n'
malprepend += '\t\t' + fMain.toMalval(str(cHold[0]), 'user_total_onhold') + '\n'
malprepend += '\t\t' + fMain.toMalval(str(cDrop[0]), 'user_total_dropped') + '\n'
malprepend += '\t\t' + fMain.toMalval(str(cPtw[0]), f'user_total_planto{mediawatchread}') + '\n'
# Count for 'nsfw'
if isSepNsfw:
malprepend18 += '\t\t' + fMain.toMalval(str(cTotal[1]), f'user_total_{mediastring}') + '\n'
malprepend18 += '\t\t' + fMain.toMalval(str(cWatch[1]), f'user_total_{mediawatchread}ing') + '\n'
malprepend18 += '\t\t' + fMain.toMalval(str(cComplete[1]), 'user_total_completed') + '\n'
malprepend18 += '\t\t' + fMain.toMalval(str(cHold[1]), 'user_total_onhold') + '\n'
malprepend18 += '\t\t' + fMain.toMalval(str(cDrop[1]), 'user_total_dropped') + '\n'
malprepend18 += '\t\t' + fMain.toMalval(str(cPtw[1]), f'user_total_planto{mediawatchread}') + '\n'
malprepend += '\t</myinfo>\n'
malprepend18 += '\t</myinfo>\n'
fMain.line_prepender(xmlMedia, malprepend)
if isSepNsfw:
fMain.line_prepender(xmlMedia18, malprepend18)
fMain.logString(f"Done with {mediaType} XML file..", source)
# Done anime/manga
fMain.logString("Done! File generated: " + outputMedia, source)
if isExportMal:
fMain.logString("Done! File generated: " + xmlMedia, source)
if isSepNsfw:
fMain.logString("Done! File generated: " + outputMedia18, source)
if isExportMal:
fMain.logString("Done! File generated: " + xmlMedia18, source)
# Already existing!
else:
fMain.logString(f"{mediaType} file already exist!: " + outputMedia, source)
returnMedia = {'main':outputMedia, 'nsfw':outputMedia18}
return returnMedia