Commit 4ebc9dbbc6

4ebc9dbbc6ed1c1ae10b063c44c767e315b6b643

parent: 47abfdfd5b

Unsigned

Wraient <wraient.911@gmail.com> · 2026-07-22 19:36 UTC

Fix: dedupe Show All when AniList custom lists re-export entries

Skip MediaListCollection custom lists when parsing, dedupe by media ID
as a safety net, and request isCustomList in AniList list queries so
status entries are not shown twice in Show All / CURRENT.

Layout: unified · split

internal/anilist.go +70
@@ -517,6 +517,8 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
517517 query ($userId: Int, $type: MediaType) {
518518 MediaListCollection(userId: $userId, type: $type) {
519519 lists {
520 name
521 isCustomList
520522 entries {
521523 id
522524 media {
@@ -575,6 +577,8 @@ func GetUserDataPreview(token string, userID int) (map[string]interface{}, error
575577 query ($userId: Int, $type: MediaType) {
576578 MediaListCollection(userId: $userId, type: $type) {
577579 lists {
580 name
581 isCustomList
578582 entries {
579583 id
580584 media {
@@ -663,11 +667,16 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m
663667 if !ok {
664668 return results
665669 }
670 seenMediaIDs := make(map[int]struct{})
666671 for _, list := range lists {
667672 listMap, ok := list.(map[string]interface{})
668673 if !ok {
669674 continue
670675 }
676 // Custom lists re-export status-list entries and would create search hits twice.
677 if isAniListCustomList(listMap) {
678 continue
679 }
671680 entries, ok := listMap["entries"].([]interface{})
672681 if !ok {
673682 continue
@@ -692,8 +701,21 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m
692701 if value, ok := media["duration"].(float64); ok {
693702 duration = int(value)
694703 }
704 mediaID := 0
705 switch id := media["id"].(type) {
706 case float64:
707 mediaID = int(id)
708 case int:
709 mediaID = id
710 }
695711
696712 if strings.Contains(strings.ToLower(romajiTitle), strings.ToLower(searchTitle)) || strings.Contains(strings.ToLower(englishTitle), strings.ToLower(searchTitle)) {
713 if mediaID != 0 {
714 if _, seen := seenMediaIDs[mediaID]; seen {
715 continue
716 }
717 seenMediaIDs[mediaID] = struct{}{}
718 }
697719 result := map[string]interface{}{
698720 "id": media["id"],
699721 "progress": entryMap["progress"],
@@ -986,6 +1008,39 @@ func makePostRequestAttempt(url string, requestBody []byte, headers map[string]s
9861008 return nil, fmt.Errorf("AniList request failed after retries")
9871009}
9881010
1011// isAniListCustomList reports whether a MediaListCollection list is a user custom
1012// list. Custom lists re-export entries that already appear under status lists, so
1013// including them double-counts the same media in Show All / CURRENT.
1014func isAniListCustomList(listData map[string]interface{}) bool {
1015 if listData == nil {
1016 return false
1017 }
1018 if isCustom, ok := listData["isCustomList"].(bool); ok {
1019 return isCustom
1020 }
1021 return false
1022}
1023
1024// dedupeEntriesByMediaID keeps the first occurrence of each non-zero Media.ID.
1025func dedupeEntriesByMediaID(entries []Entry) []Entry {
1026 if len(entries) < 2 {
1027 return entries
1028 }
1029 seen := make(map[int]struct{}, len(entries))
1030 out := make([]Entry, 0, len(entries))
1031 for _, entry := range entries {
1032 id := entry.Media.ID
1033 if id != 0 {
1034 if _, exists := seen[id]; exists {
1035 continue
1036 }
1037 seen[id] = struct{}{}
1038 }
1039 out = append(out, entry)
1040 }
1041 return out
1042}
1043
9891044func ParseAnimeList(input map[string]interface{}) AnimeList {
9901045 var animeList AnimeList
9911046
@@ -1053,11 +1108,17 @@ func ParseAnimeList(input map[string]interface{}) AnimeList {
10531108 return animeList
10541109 }
10551110
1111 // Prefer status lists; custom lists re-export the same MediaList entries.
1112 seenMediaIDs := make(map[int]struct{})
1113
10561114 for _, list := range mediaList {
10571115 listData, ok := list.(map[string]interface{})
10581116 if !ok {
10591117 continue
10601118 }
1119 if isAniListCustomList(listData) {
1120 continue
1121 }
10611122 entries, ok := listData["entries"].([]interface{})
10621123 if !ok {
10631124 continue
@@ -1103,6 +1164,15 @@ func ParseAnimeList(input map[string]interface{}) AnimeList {
11031164 animeEntry.CoverImage = safeString(coverImage["large"])
11041165 }
11051166
1167 // Defense in depth: never insert the same media twice even if AniList
1168 // returns the entry under multiple non-custom lists.
1169 if animeEntry.Media.ID != 0 {
1170 if _, exists := seenMediaIDs[animeEntry.Media.ID]; exists {
1171 continue
1172 }
1173 seenMediaIDs[animeEntry.Media.ID] = struct{}{}
1174 }
1175
11061176 // Append entries based on their status
11071177 switch animeEntry.Status {
11081178 case "CURRENT":
internal/curd.go +9 −8
@@ -1553,7 +1553,8 @@ func CheckAndDownloadFiles(storagePath string, filesToCheck []string) error {
15531553func getEntriesByCategory(list AnimeList, category string) []Entry {
15541554 switch category {
15551555 case "ALL":
1556 // Combine all categories into one slice
1556 // Combine all categories into one slice. Dedupe by Media.ID so Show All
1557 // never surfaces the same anime twice (e.g. stale cache / dual-track edge cases).
15571558 allEntries := make([]Entry, 0)
15581559 allEntries = append(allEntries, list.Watching...)
15591560 allEntries = append(allEntries, list.Completed...)
@@ -1561,22 +1562,22 @@ func getEntriesByCategory(list AnimeList, category string) []Entry {
15611562 allEntries = append(allEntries, list.Dropped...)
15621563 allEntries = append(allEntries, list.Planning...)
15631564 allEntries = append(allEntries, list.Rewatching...)
1564 return allEntries
1565 return dedupeEntriesByMediaID(allEntries)
15651566 case "CURRENT":
15661567 currentEntries := make([]Entry, 0, len(list.Watching)+len(list.Rewatching))
15671568 currentEntries = append(currentEntries, list.Watching...)
15681569 currentEntries = append(currentEntries, list.Rewatching...)
1569 return currentEntries
1570 return dedupeEntriesByMediaID(currentEntries)
15701571 case "COMPLETED":
1571 return list.Completed
1572 return dedupeEntriesByMediaID(list.Completed)
15721573 case "PAUSED":
1573 return list.Paused
1574 return dedupeEntriesByMediaID(list.Paused)
15741575 case "DROPPED":
1575 return list.Dropped
1576 return dedupeEntriesByMediaID(list.Dropped)
15761577 case "PLANNING":
1577 return list.Planning
1578 return dedupeEntriesByMediaID(list.Planning)
15781579 case "REWATCHING": // Added for completeness, though "ALL" covers it.
1579 return list.Rewatching
1580 return dedupeEntriesByMediaID(list.Rewatching)
15801581 default:
15811582 return []Entry{}
15821583 }
internal/logic_regression_test.go +141
@@ -288,6 +288,147 @@ func TestParseAnimeListMalformedInputReturnsEmptyList(t *testing.T) {
288288 }
289289}
290290
291func TestParseAnimeListSkipsCustomListsThatReExportEntries(t *testing.T) {
292 // AniList returns the same MediaList entry under the status list and again
293 // under each custom list that includes it.
294 input := map[string]interface{}{
295 "data": map[string]interface{}{
296 "MediaListCollection": map[string]interface{}{
297 "lists": []interface{}{
298 map[string]interface{}{
299 "name": "Watching",
300 "isCustomList": false,
301 "entries": []interface{}{
302 map[string]interface{}{
303 "id": float64(1001),
304 "status": "CURRENT",
305 "progress": float64(3),
306 "repeat": float64(0),
307 "updatedAt": float64(1_700_000_000),
308 "media": map[string]interface{}{
309 "id": float64(42),
310 "idMal": float64(420),
311 "episodes": float64(12),
312 "duration": float64(24),
313 "format": "TV",
314 "title": map[string]interface{}{
315 "english": "Example Show",
316 "romaji": "Example Show",
317 "native": "例",
318 },
319 "status": "RELEASING",
320 },
321 },
322 },
323 },
324 map[string]interface{}{
325 "name": "Favorites",
326 "isCustomList": true,
327 "entries": []interface{}{
328 map[string]interface{}{
329 "id": float64(1001),
330 "status": "CURRENT",
331 "progress": float64(3),
332 "repeat": float64(0),
333 "updatedAt": float64(1_700_000_000),
334 "media": map[string]interface{}{
335 "id": float64(42),
336 "idMal": float64(420),
337 "episodes": float64(12),
338 "duration": float64(24),
339 "format": "TV",
340 "title": map[string]interface{}{
341 "english": "Example Show",
342 "romaji": "Example Show",
343 "native": "例",
344 },
345 "status": "RELEASING",
346 },
347 },
348 },
349 },
350 },
351 },
352 },
353 }
354
355 list := ParseAnimeList(input)
356 if len(list.Watching) != 1 {
357 t.Fatalf("expected custom-list re-export to be skipped, watching=%d", len(list.Watching))
358 }
359 all := getEntriesByCategory(list, "ALL")
360 if len(all) != 1 || all[0].Media.ID != 42 {
361 t.Fatalf("Show All should list media 42 once, got %#v", all)
362 }
363}
364
365func TestParseAnimeListDedupesSameMediaWithoutIsCustomListFlag(t *testing.T) {
366 // Older/cached payloads may omit isCustomList; still dedupe by media ID.
367 input := map[string]interface{}{
368 "data": map[string]interface{}{
369 "MediaListCollection": map[string]interface{}{
370 "lists": []interface{}{
371 map[string]interface{}{
372 "name": "Watching",
373 "entries": []interface{}{
374 map[string]interface{}{
375 "id": float64(1),
376 "status": "CURRENT",
377 "progress": float64(1),
378 "updatedAt": float64(1_700_000_000),
379 "media": map[string]interface{}{
380 "id": float64(7),
381 "title": map[string]interface{}{"english": "Seven", "romaji": "Seven"},
382 },
383 },
384 },
385 },
386 map[string]interface{}{
387 "name": "Seasonal",
388 "entries": []interface{}{
389 map[string]interface{}{
390 "id": float64(1),
391 "status": "CURRENT",
392 "progress": float64(1),
393 "updatedAt": float64(1_700_000_000),
394 "media": map[string]interface{}{
395 "id": float64(7),
396 "title": map[string]interface{}{"english": "Seven", "romaji": "Seven"},
397 },
398 },
399 },
400 },
401 },
402 },
403 },
404 }
405
406 list := ParseAnimeList(input)
407 if len(list.Watching) != 1 {
408 t.Fatalf("expected media-ID dedupe without isCustomList, watching=%d", len(list.Watching))
409 }
410}
411
412func TestGetEntriesByCategoryDedupesAcrossBuckets(t *testing.T) {
413 list := AnimeList{
414 Watching: []Entry{
415 {Media: Media{ID: 1, Title: AnimeTitle{English: "A"}}, Status: "CURRENT"},
416 {Media: Media{ID: 1, Title: AnimeTitle{English: "A (dup)"}}, Status: "CURRENT"},
417 },
418 Completed: []Entry{
419 {Media: Media{ID: 2, Title: AnimeTitle{English: "B"}}, Status: "COMPLETED"},
420 },
421 }
422 all := getEntriesByCategory(list, "ALL")
423 if len(all) != 2 {
424 t.Fatalf("expected 2 unique media IDs in ALL, got %d (%#v)", len(all), all)
425 }
426 current := getEntriesByCategory(list, "CURRENT")
427 if len(current) != 1 || current[0].Media.ID != 1 {
428 t.Fatalf("expected CURRENT to dedupe watching, got %#v", current)
429 }
430}
431
291432func TestMediaDisplayTitleFallsBackToID(t *testing.T) {
292433 title := mediaDisplayTitle(Media{ID: 42}, &CurdConfig{AnimeNameLanguage: "english"})
293434 if title != "42" {