@irfanshadikrishad/anilist 1.9.8 → 2.0.0-forbidden.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,18 +12,69 @@ import { readFile } from 'fs/promises';
12
12
  import inquirer from 'inquirer';
13
13
  import { jsonrepair } from 'jsonrepair';
14
14
  import { join } from 'path';
15
- import Spinner from 'tiny-spinner';
16
15
  import { Auth } from './auth.js';
17
16
  import { fetcher } from './fetcher.js';
18
- import { addAnimeToListMutation, addMangaToListMutation, moveListMutation, saveAnimeWithProgressMutation, saveMangaWithProgressMutation, } from './mutations.js';
17
+ import { addAnimeToListMutation, addMangaToListMutation, saveAnimeWithProgressMutation, saveMangaWithProgressMutation, } from './mutations.js';
19
18
  import { animeDetailsQuery, animeSearchQuery, currentUserAnimeList, currentUserMangaList, malIdToAnilistAnimeId, malIdToAnilistMangaId, mangaDetailsQuery, mangaSearchQuery, popularQuery, trendingQuery, upcomingAnimesQuery, userActivityQuery, userFollowersQuery, userFollowingQuery, userQuery, } from './queries.js';
20
19
  import { responsiveOutput } from './truncate.js';
21
20
  import { AniListMediaStatus, } from './types.js';
22
21
  import { Validate } from './validation.js';
23
22
  import { anidbToanilistMapper, formatDateObject, getDownloadFolderPath, getNextSeasonAndYear, getTitle, logUserDetails, removeHtmlAndMarkdown, saveJSONasCSV, saveJSONasJSON, saveJSONasXML, selectFile, simpleDateFormat, timestampToTimeAgo, } from './workers.js';
24
- const spinner = new Spinner();
25
23
  class AniList {
26
- static importAnime() {
24
+ /**
25
+ * Prompt the user for a list status (Planning/Watching-Reading/Completed/Paused/Dropped)
26
+ */
27
+ static promptListType(kind) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const label = kind === 'ANIME' ? 'anime' : 'manga';
30
+ const { selectedListType } = yield inquirer.prompt([
31
+ {
32
+ type: 'list',
33
+ name: 'selectedListType',
34
+ message: `Select the list where you want to save this ${label}:`,
35
+ choices: [
36
+ { name: 'Planning', value: 'PLANNING' },
37
+ {
38
+ name: kind === 'ANIME' ? 'Watching' : 'Reading',
39
+ value: 'CURRENT',
40
+ },
41
+ { name: 'Completed', value: 'COMPLETED' },
42
+ { name: 'Paused', value: 'PAUSED' },
43
+ { name: 'Dropped', value: 'DROPPED' },
44
+ ],
45
+ },
46
+ ]);
47
+ return selectedListType;
48
+ });
49
+ }
50
+ /**
51
+ * Save a media entry to the authenticated user's list, logging the outcome.
52
+ */
53
+ static saveEntryToList(mutation, mediaId, status) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ var _a, _b, _c;
56
+ if (!(yield Auth.isLoggedIn())) {
57
+ console.error(`\nPlease log in first to use this feature.`);
58
+ return;
59
+ }
60
+ const response = yield fetcher(mutation, {
61
+ mediaId,
62
+ status,
63
+ });
64
+ const saved = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.SaveMediaListEntry;
65
+ if (saved) {
66
+ console.log(`\nEntry ${saved.id}. Saved as ${saved.status}.`);
67
+ }
68
+ else {
69
+ console.error(`\nFailed to save the entry. ${((_c = (_b = response === null || response === void 0 ? void 0 : response.errors) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) || 'Unknown error'}`);
70
+ }
71
+ });
72
+ }
73
+ /**
74
+ * Import a previously exported AniList JSON file, saving each entry with
75
+ * progress via the given mutation.
76
+ */
77
+ static importJSONList(mutation, buildVariables, label) {
27
78
  return __awaiter(this, void 0, void 0, function* () {
28
79
  try {
29
80
  const filename = yield selectFile('.json');
@@ -41,141 +92,114 @@ class AniList {
41
92
  const batchSize = 1;
42
93
  for (let i = 0; i < importedData.length; i += batchSize) {
43
94
  const batch = importedData.slice(i, i + batchSize);
44
- yield Promise.all(batch.map((anime) => __awaiter(this, void 0, void 0, function* () {
95
+ yield Promise.all(batch.map((item) => __awaiter(this, void 0, void 0, function* () {
45
96
  var _a, _b;
46
- const query = saveAnimeWithProgressMutation;
47
- const variables = {
48
- mediaId: anime === null || anime === void 0 ? void 0 : anime.id,
49
- progress: anime === null || anime === void 0 ? void 0 : anime.progress,
50
- status: anime === null || anime === void 0 ? void 0 : anime.status,
51
- hiddenFromStatusLists: false,
52
- };
53
97
  try {
54
- const save = yield fetcher(query, variables);
98
+ const save = yield fetcher(mutation, buildVariables(item));
55
99
  if (save) {
56
100
  const id = (_b = (_a = save === null || save === void 0 ? void 0 : save.data) === null || _a === void 0 ? void 0 : _a.SaveMediaListEntry) === null || _b === void 0 ? void 0 : _b.id;
57
101
  count++;
58
- console.log(`[${count}]\t${id}\t${anime === null || anime === void 0 ? void 0 : anime.id} ✔`);
102
+ console.log(`[${count}]\t${id}\t${item === null || item === void 0 ? void 0 : item.id} ✅`);
59
103
  }
60
104
  else {
61
- console.error(`\nError saving ${anime === null || anime === void 0 ? void 0 : anime.id}`);
105
+ console.error(`\nError saving ${item === null || item === void 0 ? void 0 : item.id}`);
62
106
  }
63
107
  }
64
108
  catch (error) {
65
- console.error(`\nError saving ${anime === null || anime === void 0 ? void 0 : anime.id}: ${error.message}`);
109
+ console.error(`\nError saving ${item === null || item === void 0 ? void 0 : item.id}: ${error.message}`);
66
110
  }
67
111
  })));
68
112
  }
69
- console.log(`\nTotal ${count} anime(s) imported successfully.`);
113
+ console.log(`\nTotal ${count} ${label}(s) imported successfully.`);
70
114
  }
71
115
  catch (error) {
72
116
  console.error(`\n${error.message}`);
73
117
  }
74
118
  });
75
119
  }
120
+ static importAnime() {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ return AniList.importJSONList(saveAnimeWithProgressMutation, (anime) => ({
123
+ mediaId: anime === null || anime === void 0 ? void 0 : anime.id,
124
+ progress: anime === null || anime === void 0 ? void 0 : anime.progress,
125
+ status: anime === null || anime === void 0 ? void 0 : anime.status,
126
+ hiddenFromStatusLists: false,
127
+ }), 'anime');
128
+ });
129
+ }
76
130
  static importManga() {
77
131
  return __awaiter(this, void 0, void 0, function* () {
78
- try {
79
- const filename = yield selectFile('.json');
80
- if (!filename) {
81
- return;
82
- }
83
- const filePath = join(getDownloadFolderPath(), filename);
84
- const fileContent = yield readFile(filePath, 'utf8');
85
- const importedData = JSON.parse(fileContent);
86
- if (!Validate.Import_JSON(importedData)) {
87
- console.error(`\nInvalid JSON file.`);
88
- return;
89
- }
90
- let count = 0;
91
- const batchSize = 1;
92
- for (let i = 0; i < importedData.length; i += batchSize) {
93
- const batch = importedData.slice(i, i + batchSize);
94
- yield Promise.all(batch.map((manga) => __awaiter(this, void 0, void 0, function* () {
95
- var _a, _b;
96
- const query = saveMangaWithProgressMutation;
97
- const variables = {
98
- mediaId: manga === null || manga === void 0 ? void 0 : manga.id,
99
- progress: manga === null || manga === void 0 ? void 0 : manga.progress,
100
- status: manga === null || manga === void 0 ? void 0 : manga.status,
101
- hiddenFromStatusLists: false,
102
- private: manga === null || manga === void 0 ? void 0 : manga.private,
103
- };
104
- try {
105
- const save = yield fetcher(query, variables);
106
- if (save) {
107
- const id = (_b = (_a = save === null || save === void 0 ? void 0 : save.data) === null || _a === void 0 ? void 0 : _a.SaveMediaListEntry) === null || _b === void 0 ? void 0 : _b.id;
108
- count++;
109
- console.log(`[${count}]\t${id}\t${manga === null || manga === void 0 ? void 0 : manga.id} ✅`);
110
- }
111
- }
112
- catch (err) {
113
- console.error(`\nError saving ${manga === null || manga === void 0 ? void 0 : manga.id}: ${err.message}`);
114
- }
115
- })));
116
- }
117
- console.log(`\nTotal ${count} manga(s) imported successfully.`);
118
- }
119
- catch (error) {
120
- console.error(`\nError: ${error.message}`);
121
- }
132
+ return AniList.importJSONList(saveMangaWithProgressMutation, (manga) => ({
133
+ mediaId: manga === null || manga === void 0 ? void 0 : manga.id,
134
+ progress: manga === null || manga === void 0 ? void 0 : manga.progress,
135
+ status: manga === null || manga === void 0 ? void 0 : manga.status,
136
+ hiddenFromStatusLists: false,
137
+ private: manga === null || manga === void 0 ? void 0 : manga.private,
138
+ }), 'manga');
122
139
  });
123
140
  }
124
- static exportAnime() {
141
+ /**
142
+ * Prompt for CSV/JSON/XML and dispatch to the matching exporter.
143
+ */
144
+ static promptExportFormatAndSave(mediaWithProgress, dataType, xmlLabel, exportAsXML) {
125
145
  return __awaiter(this, void 0, void 0, function* () {
126
- var _a, _b, _c;
127
- if (!(yield Auth.isLoggedIn())) {
128
- console.error(`\nMust login to use this feature.`);
129
- return;
130
- }
131
146
  const { exportType } = yield inquirer.prompt([
132
147
  {
133
- type: 'select',
148
+ type: 'list',
134
149
  name: 'exportType',
135
150
  message: 'Choose export type:',
136
151
  choices: [
137
152
  { name: 'CSV', value: 1 },
138
153
  { name: 'JSON', value: 2 },
139
- { name: 'XML (MyAnimeList/AniDB)', value: 3 },
154
+ { name: xmlLabel, value: 3 },
140
155
  ],
141
156
  pageSize: 10,
142
157
  },
143
158
  ]);
159
+ switch (exportType) {
160
+ case 1:
161
+ yield saveJSONasCSV(mediaWithProgress, dataType);
162
+ break;
163
+ case 2:
164
+ yield saveJSONasJSON(mediaWithProgress, dataType);
165
+ break;
166
+ case 3:
167
+ yield exportAsXML();
168
+ break;
169
+ default:
170
+ console.log(`\nInvalid export type. ${exportType}`);
171
+ break;
172
+ }
173
+ });
174
+ }
175
+ static exportAnime() {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ var _a, _b, _c;
178
+ if (!(yield Auth.isLoggedIn())) {
179
+ console.error(`\nMust login to use this feature.`);
180
+ return;
181
+ }
144
182
  const animeList = yield fetcher(currentUserAnimeList, {
145
183
  id: yield Auth.MyUserId(),
146
184
  });
147
- if (animeList) {
148
- const lists = (_c = (_b = (_a = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) !== null && _c !== void 0 ? _c : [];
149
- const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
150
- var _a, _b, _c, _d, _e, _f, _g;
151
- return ({
152
- id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
153
- title: (_c = (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : {},
154
- episodes: (_d = entry === null || entry === void 0 ? void 0 : entry.media) === null || _d === void 0 ? void 0 : _d.episodes,
155
- siteUrl: (_e = entry === null || entry === void 0 ? void 0 : entry.media) === null || _e === void 0 ? void 0 : _e.siteUrl,
156
- progress: (_f = entry.progress) !== null && _f !== void 0 ? _f : 0,
157
- status: (_g = entry === null || entry === void 0 ? void 0 : entry.status) !== null && _g !== void 0 ? _g : '',
158
- hiddenFromStatusLists: entry.hiddenFromStatusLists,
159
- });
160
- }));
161
- switch (exportType) {
162
- case 1:
163
- yield saveJSONasCSV(mediaWithProgress, 'anime');
164
- break;
165
- case 2:
166
- yield saveJSONasJSON(mediaWithProgress, 'anime');
167
- break;
168
- case 3:
169
- yield MyAnimeList.exportAnime();
170
- break;
171
- default:
172
- console.log(`\nInvalid export type. ${exportType}`);
173
- break;
174
- }
175
- }
176
- else {
185
+ if (!animeList) {
177
186
  console.error(`\nNo anime(s) found in your lists.`);
187
+ return;
178
188
  }
189
+ const lists = (_c = (_b = (_a = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) !== null && _c !== void 0 ? _c : [];
190
+ const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
191
+ var _a, _b, _c, _d;
192
+ return ({
193
+ id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
194
+ title: (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.title,
195
+ episodes: (_c = entry === null || entry === void 0 ? void 0 : entry.media) === null || _c === void 0 ? void 0 : _c.episodes,
196
+ siteUrl: (_d = entry === null || entry === void 0 ? void 0 : entry.media) === null || _d === void 0 ? void 0 : _d.siteUrl,
197
+ progress: entry.progress,
198
+ status: entry === null || entry === void 0 ? void 0 : entry.status,
199
+ hiddenFromStatusLists: entry.hiddenFromStatusLists,
200
+ });
201
+ }));
202
+ yield AniList.promptExportFormatAndSave(mediaWithProgress, 'anime', 'XML (MyAnimeList/AniDB)', MyAnimeList.exportAnime);
179
203
  });
180
204
  }
181
205
  static exportManga() {
@@ -193,76 +217,56 @@ class AniList {
193
217
  return;
194
218
  }
195
219
  const lists = ((_b = (_a = mangaLists === null || mangaLists === void 0 ? void 0 : mangaLists.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) || [];
196
- if (lists.length > 0) {
197
- const { exportType } = yield inquirer.prompt([
198
- {
199
- type: 'select',
200
- name: 'exportType',
201
- message: 'Choose export type:',
202
- choices: [
203
- { name: 'CSV', value: 1 },
204
- { name: 'JSON', value: 2 },
205
- { name: 'XML (MyAnimeList)', value: 3 },
206
- ],
207
- pageSize: 10,
208
- },
209
- ]);
210
- const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
211
- var _a, _b, _c, _d, _e;
212
- return ({
213
- id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
214
- title: (_c = (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : {},
215
- private: entry.private,
216
- chapters: entry.media.chapters,
217
- progress: (_d = entry.progress) !== null && _d !== void 0 ? _d : 0,
218
- status: (_e = entry === null || entry === void 0 ? void 0 : entry.status) !== null && _e !== void 0 ? _e : '',
219
- hiddenFromStatusLists: entry.hiddenFromStatusLists,
220
- });
221
- }));
222
- switch (exportType) {
223
- case 1:
224
- yield saveJSONasCSV(mediaWithProgress, 'manga');
225
- break;
226
- case 2:
227
- yield saveJSONasJSON(mediaWithProgress, 'manga');
228
- break;
229
- case 3:
230
- yield MyAnimeList.exportManga();
231
- break;
232
- default:
233
- console.log(`\nInvalid export type. ${exportType}`);
234
- break;
235
- }
236
- }
237
- else {
220
+ if (lists.length === 0) {
238
221
  console.log(`\nList seems to be empty.`);
222
+ return;
239
223
  }
224
+ const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
225
+ var _a, _b;
226
+ return ({
227
+ id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
228
+ title: (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.title,
229
+ private: entry.private,
230
+ chapters: entry.media.chapters,
231
+ progress: entry.progress,
232
+ status: entry === null || entry === void 0 ? void 0 : entry.status,
233
+ hiddenFromStatusLists: entry.hiddenFromStatusLists,
234
+ });
235
+ }));
236
+ yield AniList.promptExportFormatAndSave(mediaWithProgress, 'manga', 'XML (MyAnimeList)', MyAnimeList.exportManga);
240
237
  });
241
238
  }
242
- static MyAnime() {
239
+ /**
240
+ * Pick an existing entry from one of the user's own lists and re-save it
241
+ * under a (possibly different) list status.
242
+ */
243
+ static pickFromMyListAndResave(listQuery, addMutation, kind) {
243
244
  return __awaiter(this, void 0, void 0, function* () {
244
- var _a, _b, _c, _d, _e;
245
+ var _a, _b, _c, _d;
245
246
  try {
247
+ const label = kind === 'ANIME' ? 'anime' : 'manga';
246
248
  if (!(yield Auth.isLoggedIn())) {
247
249
  return console.error(`\nPlease log in first to access your lists.`);
248
250
  }
249
251
  const userId = yield Auth.MyUserId();
250
252
  if (!userId) {
251
- return console.log(`\nFailed getting current user Id.`);
253
+ return console.error(`\nFailed getting current user Id.`);
252
254
  }
253
- const data = yield fetcher(currentUserAnimeList, { id: userId });
254
- if (data === null || data === void 0 ? void 0 : data.errors) {
255
- return console.log(`\nSomething went wrong. ${(_b = (_a = data === null || data === void 0 ? void 0 : data.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message}`);
255
+ const response = yield fetcher(listQuery, {
256
+ id: userId,
257
+ });
258
+ if (!(response === null || response === void 0 ? void 0 : response.data)) {
259
+ return console.error(`\nSomething went wrong. ${((_b = (_a = response === null || response === void 0 ? void 0 : response.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error'}`);
256
260
  }
257
- const lists = (_d = (_c = data === null || data === void 0 ? void 0 : data.data) === null || _c === void 0 ? void 0 : _c.MediaListCollection) === null || _d === void 0 ? void 0 : _d.lists;
261
+ const lists = (_d = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.MediaListCollection) === null || _d === void 0 ? void 0 : _d.lists;
258
262
  if (!lists || lists.length === 0) {
259
- return console.log(`\nYou seem to have no anime(s) in your lists.`);
263
+ return console.log(`\nYou seem to have no ${label}(s) in your lists.`);
260
264
  }
261
265
  const { selectedList } = yield inquirer.prompt([
262
266
  {
263
267
  type: 'list',
264
268
  name: 'selectedList',
265
- message: 'Select an anime list:',
269
+ message: `Select a${kind === 'ANIME' ? 'n' : ''} ${label} list:`,
266
270
  choices: lists.map((list) => list.name),
267
271
  },
268
272
  ]);
@@ -271,11 +275,11 @@ class AniList {
271
275
  return console.log(`\nNo entries found or not available at this moment.`);
272
276
  }
273
277
  console.log(`\nEntries for '${selectedEntries.name}':`);
274
- const { selectedAnime } = yield inquirer.prompt([
278
+ const { selectedMedia } = yield inquirer.prompt([
275
279
  {
276
- type: 'select',
277
- name: 'selectedAnime',
278
- message: 'Select anime to add to the list:',
280
+ type: 'list',
281
+ name: 'selectedMedia',
282
+ message: `Select ${label} to add to the list:`,
279
283
  choices: selectedEntries.entries.map((entry, idx) => ({
280
284
  name: `[${idx + 1}] ${getTitle(entry.media.title)}`,
281
285
  value: entry.media.id,
@@ -283,141 +287,54 @@ class AniList {
283
287
  pageSize: 10,
284
288
  },
285
289
  ]);
286
- const { selectedListType } = yield inquirer.prompt([
287
- {
288
- type: 'list',
289
- name: 'selectedListType',
290
- message: 'Select the list where you want to save this anime:',
291
- choices: [
292
- { name: 'Planning', value: 'PLANNING' },
293
- { name: 'Watching', value: 'CURRENT' },
294
- { name: 'Completed', value: 'COMPLETED' },
295
- { name: 'Paused', value: 'PAUSED' },
296
- { name: 'Dropped', value: 'DROPPED' },
297
- ],
298
- },
299
- ]);
300
- const saveResponse = yield fetcher(addAnimeToListMutation, {
301
- mediaId: selectedAnime,
302
- status: selectedListType,
303
- });
304
- if (saveResponse) {
305
- const savedEntry = (_e = saveResponse.data) === null || _e === void 0 ? void 0 : _e.SaveMediaListEntry;
306
- console.log(`\nEntry ${savedEntry === null || savedEntry === void 0 ? void 0 : savedEntry.id}. Saved as ${savedEntry === null || savedEntry === void 0 ? void 0 : savedEntry.status}.`);
307
- }
308
- else {
309
- console.error(`\nPlease log in first to use this feature.`);
310
- }
290
+ const selectedListType = yield AniList.promptListType(kind);
291
+ yield AniList.saveEntryToList(addMutation, selectedMedia, selectedListType);
311
292
  }
312
293
  catch (error) {
313
294
  console.log(`\nSomething went wrong. ${error.message}`);
314
295
  }
315
296
  });
316
297
  }
298
+ static MyAnime() {
299
+ return __awaiter(this, void 0, void 0, function* () {
300
+ return AniList.pickFromMyListAndResave(currentUserAnimeList, addAnimeToListMutation, 'ANIME');
301
+ });
302
+ }
317
303
  static MyManga() {
318
304
  return __awaiter(this, void 0, void 0, function* () {
319
- var _a, _b, _c, _d, _e, _f, _g;
320
- try {
321
- if (!(yield Auth.isLoggedIn())) {
322
- return console.error(`\nPlease log in first to access your lists.`);
323
- }
324
- const userId = yield Auth.MyUserId();
325
- if (!userId) {
326
- return console.error(`\nFailed to get the current user ID.`);
327
- }
328
- const response = yield fetcher(currentUserMangaList, { id: userId });
329
- if (!(response === null || response === void 0 ? void 0 : response.data)) {
330
- return console.error(`\nFailed to fetch manga lists. ${((_b = (_a = response === null || response === void 0 ? void 0 : response.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error'}`);
331
- }
332
- const lists = (_d = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.MediaListCollection) === null || _d === void 0 ? void 0 : _d.lists;
333
- if (!lists || lists.length === 0) {
334
- return console.log("\nYou don't seem to have any manga in your lists.");
335
- }
336
- const { selectedList } = yield inquirer.prompt([
337
- {
338
- type: 'list',
339
- name: 'selectedList',
340
- message: 'Select a manga list:',
341
- choices: lists.map((list) => list.name),
342
- },
343
- ]);
344
- const selectedEntries = lists.find((list) => list.name === selectedList);
345
- if (!selectedEntries || selectedEntries.entries.length === 0) {
346
- return console.log('\nNo manga entries found in the selected list.');
347
- }
348
- console.log(`\nEntries for '${selectedEntries.name}':`);
349
- const { selectedManga } = yield inquirer.prompt([
350
- {
351
- type: 'select',
352
- name: 'selectedManga',
353
- message: 'Select a manga to add to the list:',
354
- choices: selectedEntries.entries.map((entry, idx) => {
355
- var _a;
356
- return ({
357
- name: `[${idx + 1}] ${getTitle(entry.media.title)}`,
358
- value: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
359
- });
360
- }),
361
- pageSize: 10,
362
- },
363
- ]);
364
- const { selectedListType } = yield inquirer.prompt([
365
- {
366
- type: 'list',
367
- name: 'selectedListType',
368
- message: 'Select the list where you want to save this manga:',
369
- choices: [
370
- { name: 'Planning', value: 'PLANNING' },
371
- { name: 'Reading', value: 'CURRENT' },
372
- { name: 'Completed', value: 'COMPLETED' },
373
- { name: 'Paused', value: 'PAUSED' },
374
- { name: 'Dropped', value: 'DROPPED' },
375
- ],
376
- },
377
- ]);
378
- const saveResponse = yield fetcher(addMangaToListMutation, {
379
- mediaId: selectedManga,
380
- status: selectedListType,
381
- });
382
- const saved = (_e = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _e === void 0 ? void 0 : _e.SaveMediaListEntry;
383
- if (saved) {
384
- console.log(`\nEntry ${saved.id}. Saved as ${saved.status}.`);
385
- }
386
- else {
387
- console.error(`\nFailed to save the manga. ${((_g = (_f = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.errors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.message) || 'Unknown error'}`);
388
- }
389
- }
390
- catch (error) {
391
- console.error(`\nSomething went wrong. ${error.message}`);
392
- }
305
+ return AniList.pickFromMyListAndResave(currentUserMangaList, addMangaToListMutation, 'MANGA');
393
306
  });
394
307
  }
395
- static getTrendingAnime(count) {
308
+ /**
309
+ * Page through a media browse query (trending/popular/upcoming), letting the
310
+ * user "see more" or pick an entry to add to one of their lists.
311
+ */
312
+ static browseAndAddAnime(query, count, extraVariables, emptyMessage) {
396
313
  return __awaiter(this, void 0, void 0, function* () {
397
- var _a, _b, _c, _d, _e, _f, _g;
314
+ var _a, _b, _c, _d;
398
315
  try {
399
316
  let page = 1;
400
- let allTrending = [];
317
+ let allMedia = [];
401
318
  while (true) {
402
- const response = yield fetcher(trendingQuery, { page, perPage: count });
403
- if (response === null || response === void 0 ? void 0 : response.errors) {
319
+ const response = yield fetcher(query, Object.assign({ page, perPage: count }, extraVariables));
320
+ if (!(response === null || response === void 0 ? void 0 : response.data)) {
404
321
  console.error(`\nSomething went wrong. ${((_b = (_a = response === null || response === void 0 ? void 0 : response.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error'}`);
405
322
  return;
406
323
  }
407
324
  const media = (_d = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.Page) === null || _d === void 0 ? void 0 : _d.media;
408
325
  if (!media || media.length === 0) {
409
- console.log(`\nNo more trending anime available.`);
326
+ console.log(`\n${emptyMessage}`);
410
327
  break;
411
328
  }
412
- allTrending = [...allTrending, ...media];
413
- const choices = allTrending.map((anime, idx) => ({
329
+ allMedia = [...allMedia, ...media];
330
+ const choices = allMedia.map((anime, idx) => ({
414
331
  name: `[${idx + 1}] ${getTitle(anime === null || anime === void 0 ? void 0 : anime.title)}`,
415
332
  value: String(anime === null || anime === void 0 ? void 0 : anime.id),
416
333
  }));
417
334
  choices.push({ name: 'See more', value: 'see_more' });
418
335
  const { selectedAnime } = yield inquirer.prompt([
419
336
  {
420
- type: 'select',
337
+ type: 'list',
421
338
  name: 'selectedAnime',
422
339
  message: 'Select anime to add to the list:',
423
340
  choices,
@@ -428,36 +345,9 @@ class AniList {
428
345
  page++;
429
346
  continue;
430
347
  }
431
- else {
432
- const { selectedListType } = yield inquirer.prompt([
433
- {
434
- type: 'list',
435
- name: 'selectedListType',
436
- message: 'Select the list where you want to save this anime:',
437
- choices: [
438
- { name: 'Planning', value: 'PLANNING' },
439
- { name: 'Watching', value: 'CURRENT' },
440
- { name: 'Completed', value: 'COMPLETED' },
441
- { name: 'Paused', value: 'PAUSED' },
442
- { name: 'Dropped', value: 'DROPPED' },
443
- ],
444
- },
445
- ]);
446
- if (!(yield Auth.isLoggedIn())) {
447
- console.error(`\nPlease log in first to use this feature.`);
448
- return;
449
- }
450
- const variables = { mediaId: selectedAnime, status: selectedListType };
451
- const saveResponse = yield fetcher(addAnimeToListMutation, variables);
452
- const saved = (_e = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _e === void 0 ? void 0 : _e.SaveMediaListEntry;
453
- if (saved) {
454
- console.log(`\nEntry ${saved.id}. Saved as ${saved.status}.`);
455
- }
456
- else {
457
- console.error(`\nFailed to save the anime. ${((_g = (_f = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.errors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.message) || 'Unknown error'}`);
458
- }
459
- break;
460
- }
348
+ const selectedListType = yield AniList.promptListType('ANIME');
349
+ yield AniList.saveEntryToList(addAnimeToListMutation, selectedAnime, selectedListType);
350
+ break;
461
351
  }
462
352
  }
463
353
  catch (error) {
@@ -465,154 +355,20 @@ class AniList {
465
355
  }
466
356
  });
467
357
  }
358
+ static getTrendingAnime(count) {
359
+ return __awaiter(this, void 0, void 0, function* () {
360
+ return AniList.browseAndAddAnime(trendingQuery, count, {}, 'No more trending anime available.');
361
+ });
362
+ }
468
363
  static getPopularAnime(count) {
469
364
  return __awaiter(this, void 0, void 0, function* () {
470
- var _a, _b, _c, _d, _e, _f, _g;
471
- try {
472
- let page = 1;
473
- let allMedia = [];
474
- while (true) {
475
- const response = yield fetcher(popularQuery, { page, perPage: count });
476
- if (!(response === null || response === void 0 ? void 0 : response.data)) {
477
- console.error(`\nSomething went wrong. ${((_b = (_a = response === null || response === void 0 ? void 0 : response.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error'}`);
478
- return;
479
- }
480
- const newMedia = (_d = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.Page) === null || _d === void 0 ? void 0 : _d.media;
481
- if (!newMedia || newMedia.length === 0) {
482
- console.log(`\nNo more popular anime available.`);
483
- break;
484
- }
485
- allMedia = [...allMedia, ...newMedia];
486
- const choices = allMedia.map((anime, idx) => ({
487
- name: `[${idx + 1}] ${getTitle(anime === null || anime === void 0 ? void 0 : anime.title)}`,
488
- value: String(anime === null || anime === void 0 ? void 0 : anime.id),
489
- }));
490
- choices.push({ name: 'See more', value: 'see_more' });
491
- const { selectedAnime } = yield inquirer.prompt([
492
- {
493
- type: 'select',
494
- name: 'selectedAnime',
495
- message: 'Select anime to add to the list:',
496
- choices,
497
- pageSize: choices.length,
498
- },
499
- ]);
500
- if (selectedAnime === 'see_more') {
501
- page++;
502
- continue;
503
- }
504
- else {
505
- const { selectedListType } = yield inquirer.prompt([
506
- {
507
- type: 'list',
508
- name: 'selectedListType',
509
- message: 'Select the list where you want to save this anime:',
510
- choices: [
511
- { name: 'Planning', value: 'PLANNING' },
512
- { name: 'Watching', value: 'CURRENT' },
513
- { name: 'Completed', value: 'COMPLETED' },
514
- { name: 'Paused', value: 'PAUSED' },
515
- { name: 'Dropped', value: 'DROPPED' },
516
- ],
517
- },
518
- ]);
519
- if (!(yield Auth.isLoggedIn())) {
520
- return console.error(`\nPlease log in first to use this feature.`);
521
- }
522
- const variables = { mediaId: selectedAnime, status: selectedListType };
523
- const saveResponse = yield fetcher(addAnimeToListMutation, variables);
524
- const saved = (_e = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _e === void 0 ? void 0 : _e.SaveMediaListEntry;
525
- if (saved) {
526
- console.log(`\nEntry ${saved.id}. Saved as ${saved.status}.`);
527
- }
528
- else {
529
- console.error(`\nFailed to save the anime. ${((_g = (_f = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.errors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.message) || 'Unknown error'}`);
530
- }
531
- break;
532
- }
533
- }
534
- }
535
- catch (error) {
536
- console.error(`\nSomething went wrong. ${error.message}`);
537
- }
365
+ return AniList.browseAndAddAnime(popularQuery, count, {}, 'No more popular anime available.');
538
366
  });
539
367
  }
540
368
  static getUpcomingAnime(count) {
541
369
  return __awaiter(this, void 0, void 0, function* () {
542
- var _a, _b, _c, _d, _e, _f;
543
- try {
544
- const { nextSeason, nextYear } = getNextSeasonAndYear();
545
- let page = 1;
546
- let allUpcoming = [];
547
- while (true) {
548
- const request = yield fetcher(upcomingAnimesQuery, {
549
- nextSeason,
550
- nextYear,
551
- page,
552
- perPage: count,
553
- });
554
- if (!request || !request.data) {
555
- console.error(`\nSomething went wrong. ${((_b = (_a = request === null || request === void 0 ? void 0 : request.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error'}`);
556
- return;
557
- }
558
- const newUpcoming = (_c = request.data.Page.media) !== null && _c !== void 0 ? _c : [];
559
- if (newUpcoming.length === 0) {
560
- console.log(`\nNo more upcoming anime available.`);
561
- break;
562
- }
563
- allUpcoming = [...allUpcoming, ...newUpcoming];
564
- const choices = allUpcoming.map((anime, idx) => ({
565
- name: `[${idx + 1}] ${getTitle(anime === null || anime === void 0 ? void 0 : anime.title)}`,
566
- value: String(anime === null || anime === void 0 ? void 0 : anime.id),
567
- }));
568
- choices.push({ name: 'See more', value: 'see_more' });
569
- const { selectedAnime } = yield inquirer.prompt([
570
- {
571
- type: 'select',
572
- name: 'selectedAnime',
573
- message: 'Select anime to add to the list:',
574
- choices,
575
- pageSize: choices.length + 2,
576
- },
577
- ]);
578
- if (selectedAnime === 'see_more') {
579
- page++;
580
- continue;
581
- }
582
- else {
583
- const { selectedListType } = yield inquirer.prompt([
584
- {
585
- type: 'list',
586
- name: 'selectedListType',
587
- message: 'Select the list where you want to save this anime:',
588
- choices: [
589
- { name: 'Planning', value: 'PLANNING' },
590
- { name: 'Watching', value: 'CURRENT' },
591
- { name: 'Completed', value: 'COMPLETED' },
592
- { name: 'Paused', value: 'PAUSED' },
593
- { name: 'Dropped', value: 'DROPPED' },
594
- ],
595
- },
596
- ]);
597
- if (!(yield Auth.isLoggedIn())) {
598
- return console.error(`\nPlease log in first to use this feature.`);
599
- }
600
- const variables = { mediaId: selectedAnime, status: selectedListType };
601
- const saveResponse = yield fetcher(addAnimeToListMutation, variables);
602
- const saved = (_d = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _d === void 0 ? void 0 : _d.SaveMediaListEntry;
603
- if (saved) {
604
- console.log(`\nEntry ${saved.id}. Saved as ${saved.status}.`);
605
- }
606
- else {
607
- console.error(`\nFailed to save the anime. ${((_f = (_e = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.errors) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.message) || 'Unknown error'}`);
608
- }
609
- break;
610
- }
611
- }
612
- }
613
- catch (error) {
614
- console.error(`\nError getting upcoming animes. ${error.message}`);
615
- }
370
+ const { nextSeason, nextYear } = getNextSeasonAndYear();
371
+ return AniList.browseAndAddAnime(upcomingAnimesQuery, count, { nextSeason, nextYear }, 'No more upcoming anime available.');
616
372
  });
617
373
  }
618
374
  static getUserByUsername(username) {
@@ -706,128 +462,56 @@ class AniList {
706
462
  }
707
463
  });
708
464
  }
709
- static searchAnime(search, count) {
465
+ /**
466
+ * Search AniList and let the user pick a result to add to one of their lists.
467
+ */
468
+ static searchAndAddToList(query, addMutation, search, count, kind) {
710
469
  return __awaiter(this, void 0, void 0, function* () {
711
- var _a, _b, _c;
712
- const searchResults = yield fetcher(animeSearchQuery, {
713
- search,
714
- page: 1,
715
- perPage: count,
716
- });
717
- if (searchResults) {
718
- const results = (_b = (_a = searchResults === null || searchResults === void 0 ? void 0 : searchResults.data) === null || _a === void 0 ? void 0 : _a.Page) === null || _b === void 0 ? void 0 : _b.media;
719
- if (results && results.length > 0) {
720
- const { selectedAnime } = yield inquirer.prompt([
721
- {
722
- type: 'select',
723
- name: 'selectedAnime',
724
- message: 'Select anime to add to your list:',
725
- choices: results.map((res, idx) => ({
726
- name: `[${idx + 1}] ${getTitle(res === null || res === void 0 ? void 0 : res.title)}`,
727
- value: res === null || res === void 0 ? void 0 : res.id,
728
- })),
729
- pageSize: 10,
730
- },
731
- ]);
732
- const { selectedListType } = yield inquirer.prompt([
733
- {
734
- type: 'list',
735
- name: 'selectedListType',
736
- message: 'Select the list where you want to save this anime:',
737
- choices: [
738
- { name: 'Planning', value: 'PLANNING' },
739
- { name: 'Watching', value: 'CURRENT' },
740
- { name: 'Completed', value: 'COMPLETED' },
741
- { name: 'Paused', value: 'PAUSED' },
742
- { name: 'Dropped', value: 'DROPPED' },
743
- ],
744
- },
745
- ]);
746
- // Save selected anime to chosen list type
747
- if (yield Auth.isLoggedIn()) {
748
- const response = yield fetcher(addAnimeToListMutation, {
749
- mediaId: selectedAnime,
750
- status: selectedListType,
751
- });
752
- if (response) {
753
- const saved = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.SaveMediaListEntry;
754
- console.log(`\nEntry ${saved === null || saved === void 0 ? void 0 : saved.id}. Saved as ${saved === null || saved === void 0 ? void 0 : saved.status}.`);
755
- }
756
- }
757
- else {
758
- console.error(`\nPlease log in first to use this feature.`);
759
- }
760
- }
761
- else {
762
- console.log(`\nNo search results found.`);
763
- }
764
- }
765
- else {
470
+ var _a, _b;
471
+ const label = kind === 'ANIME' ? 'anime' : 'manga';
472
+ const searchResults = yield fetcher(query, { search, page: 1, perPage: count });
473
+ if (!searchResults) {
766
474
  console.error(`\nSomething went wrong.`);
475
+ return;
476
+ }
477
+ const results = (_b = (_a = searchResults === null || searchResults === void 0 ? void 0 : searchResults.data) === null || _a === void 0 ? void 0 : _a.Page) === null || _b === void 0 ? void 0 : _b.media;
478
+ if (!results || results.length === 0) {
479
+ console.log(`\nNo search results found.`);
480
+ return;
767
481
  }
482
+ const { selectedMedia } = yield inquirer.prompt([
483
+ {
484
+ type: 'list',
485
+ name: 'selectedMedia',
486
+ message: `Select ${label} to add to your list:`,
487
+ choices: results.map((res, idx) => ({
488
+ name: `[${idx + 1}] ${getTitle(res === null || res === void 0 ? void 0 : res.title)}`,
489
+ value: res === null || res === void 0 ? void 0 : res.id,
490
+ })),
491
+ pageSize: 10,
492
+ },
493
+ ]);
494
+ const selectedListType = yield AniList.promptListType(kind);
495
+ yield AniList.saveEntryToList(addMutation, selectedMedia, selectedListType);
496
+ });
497
+ }
498
+ static searchAnime(search, count) {
499
+ return __awaiter(this, void 0, void 0, function* () {
500
+ return AniList.searchAndAddToList(animeSearchQuery, addAnimeToListMutation, search, count, 'ANIME');
768
501
  });
769
502
  }
770
503
  static searchManga(search, count) {
771
504
  return __awaiter(this, void 0, void 0, function* () {
772
- var _a, _b, _c;
773
- const mangaSearchResult = yield fetcher(mangaSearchQuery, {
774
- search,
775
- page: 1,
776
- perPage: count,
777
- });
778
- if (mangaSearchResult) {
779
- const results = (_b = (_a = mangaSearchResult === null || mangaSearchResult === void 0 ? void 0 : mangaSearchResult.data) === null || _a === void 0 ? void 0 : _a.Page) === null || _b === void 0 ? void 0 : _b.media;
780
- // List of manga search results
781
- const { selectedMangaId } = yield inquirer.prompt([
782
- {
783
- type: 'select',
784
- name: 'selectedMangaId',
785
- message: 'Select manga to add to your list:',
786
- choices: (results !== null && results !== void 0 ? results : []).map((res, idx) => ({
787
- name: `[${idx + 1}] ${getTitle(res === null || res === void 0 ? void 0 : res.title)}`,
788
- value: res === null || res === void 0 ? void 0 : res.id,
789
- })),
790
- pageSize: 10,
791
- },
792
- ]);
793
- // Options to save to the list
794
- const { selectedListType } = yield inquirer.prompt([
795
- {
796
- type: 'list',
797
- name: 'selectedListType',
798
- message: 'Select the list where you want to save this manga:',
799
- choices: [
800
- { name: 'Planning', value: 'PLANNING' },
801
- { name: 'Reading', value: 'CURRENT' },
802
- { name: 'Completed', value: 'COMPLETED' },
803
- { name: 'Paused', value: 'PAUSED' },
804
- { name: 'Dropped', value: 'DROPPED' },
805
- ],
806
- },
807
- ]);
808
- // If logged in save to the list
809
- if (yield Auth.isLoggedIn()) {
810
- const response = yield fetcher(addMangaToListMutation, {
811
- mediaId: selectedMangaId,
812
- status: selectedListType,
813
- });
814
- if (response) {
815
- const saved = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.SaveMediaListEntry;
816
- console.log(`\nEntry ${saved === null || saved === void 0 ? void 0 : saved.id}. Saved as ${saved === null || saved === void 0 ? void 0 : saved.status}.`);
817
- }
818
- }
819
- else {
820
- console.error(`\nPlease log in first to use this feature.`);
821
- }
822
- }
823
- else {
824
- console.error(`\nSomething went wrong.`);
825
- }
505
+ return AniList.searchAndAddToList(mangaSearchQuery, addMangaToListMutation, search, count, 'MANGA');
826
506
  });
827
507
  }
828
508
  }
829
509
  class MyAnimeList {
830
- static importAnime() {
510
+ /**
511
+ * Import a MyAnimeList export XML file, mapping each entry to its AniList
512
+ * equivalent by MAL id and saving it with progress.
513
+ */
514
+ static importMALList(options) {
831
515
  return __awaiter(this, void 0, void 0, function* () {
832
516
  var _a, _b, _c, _d, _e;
833
517
  try {
@@ -837,228 +521,161 @@ class MyAnimeList {
837
521
  }
838
522
  const filePath = join(getDownloadFolderPath(), filename);
839
523
  const fileContent = yield readFile(filePath, 'utf8');
840
- if (!(yield Validate.Import_AnimeXML(fileContent))) {
524
+ if (!(yield options.validator(fileContent))) {
841
525
  console.error(`\nInvalid XML file.`);
842
526
  return;
843
527
  }
844
528
  const parser = new XMLParser();
845
- if (fileContent) {
846
- const XMLObject = parser.parse(fileContent);
847
- const animeList = (_a = XMLObject === null || XMLObject === void 0 ? void 0 : XMLObject.myanimelist) === null || _a === void 0 ? void 0 : _a.anime;
848
- if ((animeList === null || animeList === void 0 ? void 0 : animeList.length) > 0) {
849
- let count = 0;
850
- const statusMap = {
851
- 'On-Hold': AniListMediaStatus.PAUSED,
852
- 'Dropped': AniListMediaStatus.DROPPED,
853
- 'Completed': AniListMediaStatus.COMPLETED,
854
- 'Watching': AniListMediaStatus.CURRENT,
855
- 'Plan to Watch': AniListMediaStatus.PLANNING,
856
- };
857
- for (const anime of animeList) {
858
- const malId = anime.series_animedb_id;
859
- const progress = anime.my_watched_episodes;
860
- const status = statusMap[anime.my_status];
861
- try {
862
- // Fetch AniList ID using MAL ID
863
- const anilistResponse = yield fetcher(malIdToAnilistAnimeId, { malId });
864
- const anilistId = (_c = (_b = anilistResponse === null || anilistResponse === void 0 ? void 0 : anilistResponse.data) === null || _b === void 0 ? void 0 : _b.Media) === null || _c === void 0 ? void 0 : _c.id;
865
- if (anilistId) {
866
- // Save anime entry with progress
867
- const saveResponse = yield fetcher(saveAnimeWithProgressMutation, {
868
- mediaId: anilistId,
869
- progress,
870
- status,
871
- hiddenFromStatusLists: false,
872
- private: false,
873
- });
874
- const entryId = (_e = (_d = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _d === void 0 ? void 0 : _d.SaveMediaListEntry) === null || _e === void 0 ? void 0 : _e.id;
875
- if (entryId) {
876
- count++;
877
- console.log(`[${count}] ${entryId} ✔`);
878
- }
879
- }
880
- else {
881
- console.error(`Could not retrieve AniList ID for MAL ID ${malId}`);
882
- }
529
+ const XMLObject = parser.parse(fileContent);
530
+ const entries = (_a = XMLObject === null || XMLObject === void 0 ? void 0 : XMLObject.myanimelist) === null || _a === void 0 ? void 0 : _a[options.listKey];
531
+ if (!entries || entries.length === 0) {
532
+ console.log(`\nNo ${options.listKey} list found in the file.`);
533
+ return;
534
+ }
535
+ let count = 0;
536
+ for (const entry of entries) {
537
+ const malId = entry[options.malIdField];
538
+ const progress = entry[options.progressField];
539
+ const status = options.statusMap[entry.my_status];
540
+ try {
541
+ // Fetch AniList ID using MAL ID
542
+ const anilistResponse = yield fetcher(options.malIdQuery, { malId });
543
+ const anilistId = (_c = (_b = anilistResponse === null || anilistResponse === void 0 ? void 0 : anilistResponse.data) === null || _b === void 0 ? void 0 : _b.Media) === null || _c === void 0 ? void 0 : _c.id;
544
+ if (anilistId) {
545
+ // Save entry with progress
546
+ const saveResponse = yield fetcher(options.saveMutation, {
547
+ mediaId: anilistId,
548
+ progress,
549
+ status,
550
+ hiddenFromStatusLists: false,
551
+ private: false,
552
+ });
553
+ const entryId = (_e = (_d = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _d === void 0 ? void 0 : _d.SaveMediaListEntry) === null || _e === void 0 ? void 0 : _e.id;
554
+ if (entryId) {
555
+ count++;
556
+ console.log(`[${count}] ${entryId} ✅`);
883
557
  }
884
- catch (error) {
885
- console.error(`Error processing MAL ID ${malId}: ${error.message}`);
558
+ else {
559
+ console.error(`Failed to save entry for ${malId}`);
886
560
  }
887
561
  }
888
- console.log(`\nTotal Entries Processed: ${count}`);
562
+ else {
563
+ console.error(`Could not retrieve AniList ID for MAL ID ${malId}`);
564
+ }
889
565
  }
890
- else {
891
- console.log(`\nNo anime list found in the file.`);
566
+ catch (error) {
567
+ console.error(`Error processing MAL ID ${malId}: ${error.message}`);
892
568
  }
893
569
  }
570
+ console.log(`\nTotal Entries Processed: ${count}`);
894
571
  }
895
572
  catch (error) {
896
573
  console.error(`\nError in MAL import process: ${error.message}`);
897
574
  }
898
575
  });
899
576
  }
900
- static importManga() {
577
+ static importAnime() {
901
578
  return __awaiter(this, void 0, void 0, function* () {
902
- var _a, _b, _c, _d, _e;
903
- try {
904
- const filename = yield selectFile('.xml');
905
- if (!filename) {
906
- return;
907
- }
908
- const filePath = join(getDownloadFolderPath(), filename);
909
- const fileContent = yield readFile(filePath, 'utf8');
910
- if (!(yield Validate.Import_MangaXML(fileContent))) {
911
- console.error(`\nInvalid XML file.`);
912
- return;
913
- }
914
- const parser = new XMLParser();
915
- if (fileContent) {
916
- const XMLObject = parser.parse(fileContent);
917
- const mangas = (_a = XMLObject === null || XMLObject === void 0 ? void 0 : XMLObject.myanimelist) === null || _a === void 0 ? void 0 : _a.manga;
918
- if ((mangas === null || mangas === void 0 ? void 0 : mangas.length) > 0) {
919
- let count = 0;
920
- const statusMap = {
921
- 'On-Hold': AniListMediaStatus.PAUSED,
922
- 'Dropped': AniListMediaStatus.DROPPED,
923
- 'Completed': AniListMediaStatus.COMPLETED,
924
- 'Reading': AniListMediaStatus.CURRENT,
925
- 'Plan to Read': AniListMediaStatus.PLANNING,
926
- };
927
- for (const manga of mangas) {
928
- const malId = manga.manga_mangadb_id;
929
- const progress = manga.my_read_chapters;
930
- const status = statusMap[manga.my_status];
931
- try {
932
- // Fetch AniList ID using MAL ID
933
- const anilistResponse = yield fetcher(malIdToAnilistMangaId, { malId });
934
- const anilistId = (_c = (_b = anilistResponse === null || anilistResponse === void 0 ? void 0 : anilistResponse.data) === null || _b === void 0 ? void 0 : _b.Media) === null || _c === void 0 ? void 0 : _c.id;
935
- if (anilistId) {
936
- // Save manga entry with progress
937
- const saveResponse = yield fetcher(saveMangaWithProgressMutation, {
938
- mediaId: anilistId,
939
- progress,
940
- status,
941
- hiddenFromStatusLists: false,
942
- private: false,
943
- });
944
- const entryId = (_e = (_d = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _d === void 0 ? void 0 : _d.SaveMediaListEntry) === null || _e === void 0 ? void 0 : _e.id;
945
- if (entryId) {
946
- count++;
947
- console.log(`[${count}] ${entryId} ✔`);
948
- }
949
- else {
950
- console.error(`Failed to save entry for ${malId}`);
951
- }
952
- }
953
- else {
954
- console.error(`Could not retrieve AniList ID for MAL ID ${malId}`);
955
- }
956
- }
957
- catch (error) {
958
- console.error(`Error processing MAL ID ${malId}: ${error.message}`);
959
- }
960
- }
961
- console.log(`\nTotal Entries Processed: ${count}`);
962
- }
963
- else {
964
- console.log(`\nNo manga list seems to be found.`);
965
- }
966
- }
967
- }
968
- catch (error) {
969
- console.error(`\nError from MAL import: ${error.message}`);
970
- }
579
+ return MyAnimeList.importMALList({
580
+ validator: Validate.Import_AnimeXML,
581
+ listKey: 'anime',
582
+ malIdField: 'series_animedb_id',
583
+ progressField: 'my_watched_episodes',
584
+ statusMap: {
585
+ 'On-Hold': AniListMediaStatus.PAUSED,
586
+ 'Dropped': AniListMediaStatus.DROPPED,
587
+ 'Completed': AniListMediaStatus.COMPLETED,
588
+ 'Watching': AniListMediaStatus.CURRENT,
589
+ 'Plan to Watch': AniListMediaStatus.PLANNING,
590
+ },
591
+ malIdQuery: malIdToAnilistAnimeId,
592
+ saveMutation: saveAnimeWithProgressMutation,
593
+ });
971
594
  });
972
595
  }
973
- static exportAnime() {
596
+ static importManga() {
974
597
  return __awaiter(this, void 0, void 0, function* () {
975
- var _a, _b, _c, _d, _e, _f, _g;
976
- try {
977
- if (yield Auth.isLoggedIn()) {
978
- const animeList = yield fetcher(currentUserAnimeList, {
979
- id: yield Auth.MyUserId(),
980
- });
981
- if (((_d = (_c = (_b = (_a = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0) {
982
- const lists = (_g = (_f = (_e = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _e === void 0 ? void 0 : _e.MediaListCollection) === null || _f === void 0 ? void 0 : _f.lists) !== null && _g !== void 0 ? _g : [];
983
- const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
984
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
985
- return ({
986
- id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
987
- malId: (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.idMal,
988
- title: (_d = (_c = entry === null || entry === void 0 ? void 0 : entry.media) === null || _c === void 0 ? void 0 : _c.title) !== null && _d !== void 0 ? _d : {},
989
- episodes: (_e = entry === null || entry === void 0 ? void 0 : entry.media) === null || _e === void 0 ? void 0 : _e.episodes,
990
- siteUrl: (_f = entry === null || entry === void 0 ? void 0 : entry.media) === null || _f === void 0 ? void 0 : _f.siteUrl,
991
- progress: (_g = entry.progress) !== null && _g !== void 0 ? _g : 0,
992
- status: (_h = entry === null || entry === void 0 ? void 0 : entry.status) !== null && _h !== void 0 ? _h : '',
993
- hiddenFromStatusLists: false,
994
- format: (_j = entry === null || entry === void 0 ? void 0 : entry.media) === null || _j === void 0 ? void 0 : _j.format,
995
- });
996
- }));
997
- yield saveJSONasXML(mediaWithProgress, 0);
998
- }
999
- else {
1000
- console.log(`\nHey, ${yield Auth.MyUserName()}. Your anime list seems to be empty.`);
1001
- }
1002
- }
1003
- }
1004
- catch (error) {
1005
- console.error(`\nError from MALexport. ${error.message}`);
1006
- }
598
+ return MyAnimeList.importMALList({
599
+ validator: Validate.Import_MangaXML,
600
+ listKey: 'manga',
601
+ malIdField: 'manga_mangadb_id',
602
+ progressField: 'my_read_chapters',
603
+ statusMap: {
604
+ 'On-Hold': AniListMediaStatus.PAUSED,
605
+ 'Dropped': AniListMediaStatus.DROPPED,
606
+ 'Completed': AniListMediaStatus.COMPLETED,
607
+ 'Reading': AniListMediaStatus.CURRENT,
608
+ 'Plan to Read': AniListMediaStatus.PLANNING,
609
+ },
610
+ malIdQuery: malIdToAnilistMangaId,
611
+ saveMutation: saveMangaWithProgressMutation,
612
+ });
1007
613
  });
1008
614
  }
1009
- static exportManga() {
615
+ /**
616
+ * Fetch the authenticated user's anime/manga list and export it as MAL XML.
617
+ */
618
+ static exportMALList(listQuery, mapEntry, dataType) {
1010
619
  return __awaiter(this, void 0, void 0, function* () {
1011
- var _a, _b, _c, _d, _e, _f, _g;
620
+ var _a, _b;
1012
621
  try {
1013
622
  if (!(yield Auth.isLoggedIn())) {
1014
623
  console.log(`\nPlease login to use this feature.`);
1015
624
  return;
1016
625
  }
1017
- const mangaList = yield fetcher(currentUserMangaList, {
626
+ const list = yield fetcher(listQuery, {
1018
627
  id: yield Auth.MyUserId(),
1019
628
  });
1020
- if (((_d = (_c = (_b = (_a = mangaList === null || mangaList === void 0 ? void 0 : mangaList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0) {
1021
- const lists = (_g = (_f = (_e = mangaList === null || mangaList === void 0 ? void 0 : mangaList.data) === null || _e === void 0 ? void 0 : _e.MediaListCollection) === null || _f === void 0 ? void 0 : _f.lists) !== null && _g !== void 0 ? _g : [];
1022
- const mediaWithProgress = lists.flatMap((list) => list.entries.map((entry) => {
1023
- var _a, _b, _c;
1024
- return ({
1025
- id: entry.media.id,
1026
- malId: entry.media.idMal,
1027
- title: (_a = entry.media.title) !== null && _a !== void 0 ? _a : {},
1028
- private: entry.private,
1029
- chapters: entry.media.chapters,
1030
- progress: (_b = entry.progress) !== null && _b !== void 0 ? _b : 0,
1031
- status: (_c = entry.status) !== null && _c !== void 0 ? _c : '',
1032
- hiddenFromStatusLists: entry.hiddenFromStatusLists,
1033
- });
1034
- }));
1035
- yield saveJSONasXML(mediaWithProgress, 1);
1036
- }
1037
- else {
629
+ const lists = (_b = (_a = list === null || list === void 0 ? void 0 : list.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists;
630
+ if (!lists || lists.length === 0) {
1038
631
  console.log(`\nHey, ${yield Auth.MyUserName()}. Your anime list seems to be empty.`);
632
+ return;
1039
633
  }
634
+ const mediaWithProgress = lists.flatMap((l) => l.entries.map(mapEntry));
635
+ yield saveJSONasXML(mediaWithProgress, dataType);
1040
636
  }
1041
637
  catch (error) {
1042
638
  console.error(`\nError from MALexport. ${error.message}`);
1043
639
  }
1044
640
  });
1045
641
  }
642
+ static exportAnime() {
643
+ return __awaiter(this, void 0, void 0, function* () {
644
+ return MyAnimeList.exportMALList(currentUserAnimeList, (entry) => {
645
+ var _a, _b, _c, _d, _e, _f;
646
+ return ({
647
+ id: (_a = entry === null || entry === void 0 ? void 0 : entry.media) === null || _a === void 0 ? void 0 : _a.id,
648
+ malId: (_b = entry === null || entry === void 0 ? void 0 : entry.media) === null || _b === void 0 ? void 0 : _b.idMal,
649
+ title: (_c = entry === null || entry === void 0 ? void 0 : entry.media) === null || _c === void 0 ? void 0 : _c.title,
650
+ episodes: (_d = entry === null || entry === void 0 ? void 0 : entry.media) === null || _d === void 0 ? void 0 : _d.episodes,
651
+ siteUrl: (_e = entry === null || entry === void 0 ? void 0 : entry.media) === null || _e === void 0 ? void 0 : _e.siteUrl,
652
+ progress: entry.progress,
653
+ status: entry === null || entry === void 0 ? void 0 : entry.status,
654
+ hiddenFromStatusLists: false,
655
+ format: (_f = entry === null || entry === void 0 ? void 0 : entry.media) === null || _f === void 0 ? void 0 : _f.format,
656
+ });
657
+ }, 0);
658
+ });
659
+ }
660
+ static exportManga() {
661
+ return __awaiter(this, void 0, void 0, function* () {
662
+ return MyAnimeList.exportMALList(currentUserMangaList, (entry) => ({
663
+ id: entry.media.id,
664
+ malId: entry.media.idMal,
665
+ title: entry.media.title,
666
+ private: entry.private,
667
+ chapters: entry.media.chapters,
668
+ progress: entry.progress,
669
+ status: entry.status,
670
+ hiddenFromStatusLists: entry.hiddenFromStatusLists,
671
+ }), 1);
672
+ });
673
+ }
1046
674
  }
1047
675
  class AniDB {
1048
676
  static importAnime() {
1049
677
  return __awaiter(this, void 0, void 0, function* () {
1050
678
  var _a, _b;
1051
- function getStatus(anidbStatus, episodesSeen) {
1052
- if (anidbStatus === 'complete') {
1053
- return AniListMediaStatus.COMPLETED;
1054
- }
1055
- else if (anidbStatus === 'incomplete' && Number(episodesSeen) > 0) {
1056
- return AniListMediaStatus.CURRENT;
1057
- }
1058
- else {
1059
- return AniListMediaStatus.PLANNING;
1060
- }
1061
- }
1062
679
  try {
1063
680
  const filename = yield selectFile('.json');
1064
681
  if (!filename) {
@@ -1088,7 +705,19 @@ class AniDB {
1088
705
  const ownEpisodes = anime.ownEpisodes;
1089
706
  const romanjiName = anime.romanjiName;
1090
707
  const englishName = anime.englishName;
1091
- const anilistId = yield anidbToanilistMapper(romanjiName, Number(released.split('.')[2]), englishName);
708
+ function getStatus(anidbStatus, episodesSeen) {
709
+ if (anidbStatus === 'complete') {
710
+ return AniListMediaStatus.COMPLETED;
711
+ }
712
+ else if (anidbStatus === 'incomplete' &&
713
+ Number(episodesSeen) > 0) {
714
+ return AniListMediaStatus.CURRENT;
715
+ }
716
+ else {
717
+ return AniListMediaStatus.PLANNING;
718
+ }
719
+ }
720
+ let anilistId = yield anidbToanilistMapper(romanjiName, Number(released.split('.')[2]), englishName);
1092
721
  if (anilistId) {
1093
722
  try {
1094
723
  const saveResponse = yield fetcher(saveAnimeWithProgressMutation, {
@@ -1101,7 +730,7 @@ class AniDB {
1101
730
  const entryId = (_b = (_a = saveResponse === null || saveResponse === void 0 ? void 0 : saveResponse.data) === null || _a === void 0 ? void 0 : _a.SaveMediaListEntry) === null || _b === void 0 ? void 0 : _b.id;
1102
731
  if (entryId) {
1103
732
  count++;
1104
- responsiveOutput(`[${count}]\t${entryId} ✔\t${anidbId}\t${anilistId}\t(${ownEpisodes}/${totalEpisodes})\t${status}–>${getStatus(status, ownEpisodes)}`);
733
+ responsiveOutput(`[${count}]\t${entryId} ✅\t${anidbId}\t${anilistId}\t(${ownEpisodes}/${totalEpisodes})\t${status}–>${getStatus(status, ownEpisodes)}`);
1105
734
  }
1106
735
  }
1107
736
  catch (error) {
@@ -1136,125 +765,4 @@ class AniDB {
1136
765
  });
1137
766
  }
1138
767
  }
1139
- class MoveTo {
1140
- static AnimeList() {
1141
- return __awaiter(this, void 0, void 0, function* () {
1142
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
1143
- try {
1144
- spinner.start(`Fetching your anime list...`);
1145
- const animeList = yield fetcher(currentUserAnimeList, {
1146
- id: yield Auth.MyUserId(),
1147
- });
1148
- if (((_d = (_c = (_b = (_a = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0) {
1149
- spinner.stop();
1150
- const list = (_g = (_f = (_e = animeList === null || animeList === void 0 ? void 0 : animeList.data) === null || _e === void 0 ? void 0 : _e.MediaListCollection) === null || _f === void 0 ? void 0 : _f.lists) !== null && _g !== void 0 ? _g : [];
1151
- const { selectedList } = yield inquirer.prompt([
1152
- {
1153
- type: 'list',
1154
- name: 'selectedList',
1155
- message: 'Select a list to move:',
1156
- choices: list.map((list) => list.name),
1157
- },
1158
- ]);
1159
- const selectedEntries = list.find((list) => list.name === selectedList);
1160
- if (!selectedEntries || selectedEntries.entries.length === 0) {
1161
- return console.log('\nNo anime entries found in the selected list.');
1162
- }
1163
- const { toMoveList } = yield inquirer.prompt([
1164
- {
1165
- type: 'list',
1166
- name: 'toMoveList',
1167
- message: 'Select a list to move to:',
1168
- choices: list.map((list) => list.name),
1169
- },
1170
- ]);
1171
- const entries = selectedEntries.entries;
1172
- for (const entry of entries) {
1173
- const mediaId = (_h = entry === null || entry === void 0 ? void 0 : entry.media) === null || _h === void 0 ? void 0 : _h.id;
1174
- const status = entry === null || entry === void 0 ? void 0 : entry.status;
1175
- // const progress = entry?.progress
1176
- // console.log(mediaId, status)
1177
- const response = yield fetcher(moveListMutation, {
1178
- mediaId,
1179
- status,
1180
- customList: toMoveList,
1181
- });
1182
- if (response === null || response === void 0 ? void 0 : response.data) {
1183
- const moved = (_j = response === null || response === void 0 ? void 0 : response.data) === null || _j === void 0 ? void 0 : _j.SaveMediaListEntry;
1184
- console.log(`✔ Entry ${moved === null || moved === void 0 ? void 0 : moved.id}. Moved from ${selectedList} to ${toMoveList}.`);
1185
- }
1186
- else {
1187
- console.error(`\nFailed to move the anime. ${((_l = (_k = response === null || response === void 0 ? void 0 : response.errors) === null || _k === void 0 ? void 0 : _k[0]) === null || _l === void 0 ? void 0 : _l.message) || 'Unknown error'}`);
1188
- }
1189
- }
1190
- }
1191
- else {
1192
- console.log(`\nHey, ${yield Auth.MyUserName()}. Your anime list seems to be empty.`);
1193
- }
1194
- }
1195
- catch (error) {
1196
- console.error(`\nError from AnimeList. ${error.message}`);
1197
- }
1198
- });
1199
- }
1200
- static MangaList() {
1201
- return __awaiter(this, void 0, void 0, function* () {
1202
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
1203
- try {
1204
- spinner.start(`Fetching your manga list...`);
1205
- const mangaList = yield fetcher(currentUserMangaList, {
1206
- id: yield Auth.MyUserId(),
1207
- });
1208
- if (((_d = (_c = (_b = (_a = mangaList === null || mangaList === void 0 ? void 0 : mangaList.data) === null || _a === void 0 ? void 0 : _a.MediaListCollection) === null || _b === void 0 ? void 0 : _b.lists) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0) {
1209
- spinner.stop();
1210
- const list = (_g = (_f = (_e = mangaList === null || mangaList === void 0 ? void 0 : mangaList.data) === null || _e === void 0 ? void 0 : _e.MediaListCollection) === null || _f === void 0 ? void 0 : _f.lists) !== null && _g !== void 0 ? _g : [];
1211
- const { selectedList } = yield inquirer.prompt([
1212
- {
1213
- type: 'list',
1214
- name: 'selectedList',
1215
- message: 'Select a list to move:',
1216
- choices: list.map((list) => list.name),
1217
- },
1218
- ]);
1219
- const selectedEntries = list.find((list) => list.name === selectedList);
1220
- if (!selectedEntries || selectedEntries.entries.length === 0) {
1221
- return console.log('\nNo manga entries found in the selected list.');
1222
- }
1223
- const { toMoveList } = yield inquirer.prompt([
1224
- {
1225
- type: 'list',
1226
- name: 'toMoveList',
1227
- message: 'Select a list to move to:',
1228
- choices: list.map((list) => list.name),
1229
- },
1230
- ]);
1231
- const entries = selectedEntries.entries;
1232
- for (const entry of entries) {
1233
- const mediaId = (_h = entry === null || entry === void 0 ? void 0 : entry.media) === null || _h === void 0 ? void 0 : _h.id;
1234
- const status = entry === null || entry === void 0 ? void 0 : entry.status;
1235
- // console.log(mediaId, status)
1236
- const response = yield fetcher(moveListMutation, {
1237
- mediaId,
1238
- status,
1239
- customList: toMoveList,
1240
- });
1241
- if (response === null || response === void 0 ? void 0 : response.data) {
1242
- const moved = (_j = response === null || response === void 0 ? void 0 : response.data) === null || _j === void 0 ? void 0 : _j.SaveMediaListEntry;
1243
- console.log(`✅ Entry ${moved === null || moved === void 0 ? void 0 : moved.id}. Moved from ${selectedList} to ${toMoveList}.`);
1244
- }
1245
- else {
1246
- console.error(`\nFailed to move the manga. ${((_l = (_k = response === null || response === void 0 ? void 0 : response.errors) === null || _k === void 0 ? void 0 : _k[0]) === null || _l === void 0 ? void 0 : _l.message) || 'Unknown error'}`);
1247
- }
1248
- }
1249
- }
1250
- else {
1251
- console.log(`\nHey, ${yield Auth.MyUserName()}. Your manga list seems to be empty.`);
1252
- }
1253
- }
1254
- catch (error) {
1255
- console.error(`\nError from MangaList. ${error.message}`);
1256
- }
1257
- });
1258
- }
1259
- }
1260
- export { AniDB, AniList, MoveTo, MyAnimeList };
768
+ export { AniDB, AniList, MyAnimeList };