Commit 4ebc9dbbc6
Unsigned
Layout: unified · split
internal/anilist.go +70
| @@ -517,6 +517,8 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) { | |||
| 517 | query ($userId: Int, $type: MediaType) { | 517 | query ($userId: Int, $type: MediaType) { |
| 518 | MediaListCollection(userId: $userId, type: $type) { | 518 | MediaListCollection(userId: $userId, type: $type) { |
| 519 | lists { | 519 | lists { |
| 520 | name | ||
| 521 | isCustomList | ||
| 520 | entries { | 522 | entries { |
| 521 | id | 523 | id |
| 522 | media { | 524 | media { |
| @@ -575,6 +577,8 @@ func GetUserDataPreview(token string, userID int) (map[string]interface{}, error | |||
| 575 | query ($userId: Int, $type: MediaType) { | 577 | query ($userId: Int, $type: MediaType) { |
| 576 | MediaListCollection(userId: $userId, type: $type) { | 578 | MediaListCollection(userId: $userId, type: $type) { |
| 577 | lists { | 579 | lists { |
| 580 | name | ||
| 581 | isCustomList | ||
| 578 | entries { | 582 | entries { |
| 579 | id | 583 | id |
| 580 | media { | 584 | media { |
| @@ -663,11 +667,16 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m | |||
| 663 | if !ok { | 667 | if !ok { |
| 664 | return results | 668 | return results |
| 665 | } | 669 | } |
| 670 | seenMediaIDs := make(map[int]struct{}) | ||
| 666 | for _, list := range lists { | 671 | for _, list := range lists { |
| 667 | listMap, ok := list.(map[string]interface{}) | 672 | listMap, ok := list.(map[string]interface{}) |
| 668 | if !ok { | 673 | if !ok { |
| 669 | continue | 674 | continue |
| 670 | } | 675 | } |
| 676 | // Custom lists re-export status-list entries and would create search hits twice. | ||
| 677 | if isAniListCustomList(listMap) { | ||
| 678 | continue | ||
| 679 | } | ||
| 671 | entries, ok := listMap["entries"].([]interface{}) | 680 | entries, ok := listMap["entries"].([]interface{}) |
| 672 | if !ok { | 681 | if !ok { |
| 673 | continue | 682 | continue |
| @@ -692,8 +701,21 @@ func SearchAnimeByTitle(jsonData map[string]interface{}, searchTitle string) []m | |||
| 692 | if value, ok := media["duration"].(float64); ok { | 701 | if value, ok := media["duration"].(float64); ok { |
| 693 | duration = int(value) | 702 | duration = int(value) |
| 694 | } | 703 | } |
| 704 | mediaID := 0 | ||
| 705 | switch id := media["id"].(type) { | ||
| 706 | case float64: | ||
| 707 | mediaID = int(id) | ||
| 708 | case int: | ||
| 709 | mediaID = id | ||
| 710 | } | ||
| 695 | 711 | ||
| 696 | if strings.Contains(strings.ToLower(romajiTitle), strings.ToLower(searchTitle)) || strings.Contains(strings.ToLower(englishTitle), strings.ToLower(searchTitle)) { | 712 | 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 | } | ||
| 697 | result := map[string]interface{}{ | 719 | result := map[string]interface{}{ |
| 698 | "id": media["id"], | 720 | "id": media["id"], |
| 699 | "progress": entryMap["progress"], | 721 | "progress": entryMap["progress"], |
| @@ -986,6 +1008,39 @@ func makePostRequestAttempt(url string, requestBody []byte, headers map[string]s | |||
| 986 | return nil, fmt.Errorf("AniList request failed after retries") | 1008 | return nil, fmt.Errorf("AniList request failed after retries") |
| 987 | } | 1009 | } |
| 988 | 1010 | ||
| 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. | ||
| 1014 | func 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. | ||
| 1025 | func 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 | |||
| 989 | func ParseAnimeList(input map[string]interface{}) AnimeList { | 1044 | func ParseAnimeList(input map[string]interface{}) AnimeList { |
| 990 | var animeList AnimeList | 1045 | var animeList AnimeList |
| 991 | 1046 | ||
| @@ -1053,11 +1108,17 @@ func ParseAnimeList(input map[string]interface{}) AnimeList { | |||
| 1053 | return animeList | 1108 | return animeList |
| 1054 | } | 1109 | } |
| 1055 | 1110 | ||
| 1111 | // Prefer status lists; custom lists re-export the same MediaList entries. | ||
| 1112 | seenMediaIDs := make(map[int]struct{}) | ||
| 1113 | |||
| 1056 | for _, list := range mediaList { | 1114 | for _, list := range mediaList { |
| 1057 | listData, ok := list.(map[string]interface{}) | 1115 | listData, ok := list.(map[string]interface{}) |
| 1058 | if !ok { | 1116 | if !ok { |
| 1059 | continue | 1117 | continue |
| 1060 | } | 1118 | } |
| 1119 | if isAniListCustomList(listData) { | ||
| 1120 | continue | ||
| 1121 | } | ||
| 1061 | entries, ok := listData["entries"].([]interface{}) | 1122 | entries, ok := listData["entries"].([]interface{}) |
| 1062 | if !ok { | 1123 | if !ok { |
| 1063 | continue | 1124 | continue |
| @@ -1103,6 +1164,15 @@ func ParseAnimeList(input map[string]interface{}) AnimeList { | |||
| 1103 | animeEntry.CoverImage = safeString(coverImage["large"]) | 1164 | animeEntry.CoverImage = safeString(coverImage["large"]) |
| 1104 | } | 1165 | } |
| 1105 | 1166 | ||
| 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 | |||
| 1106 | // Append entries based on their status | 1176 | // Append entries based on their status |
| 1107 | switch animeEntry.Status { | 1177 | switch animeEntry.Status { |
| 1108 | case "CURRENT": | 1178 | case "CURRENT": |
internal/curd.go +9 −8
| @@ -1553,7 +1553,8 @@ func CheckAndDownloadFiles(storagePath string, filesToCheck []string) error { | |||
| 1553 | func getEntriesByCategory(list AnimeList, category string) []Entry { | 1553 | func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 1554 | switch category { | 1554 | switch category { |
| 1555 | case "ALL": | 1555 | 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). | ||
| 1557 | allEntries := make([]Entry, 0) | 1558 | allEntries := make([]Entry, 0) |
| 1558 | allEntries = append(allEntries, list.Watching...) | 1559 | allEntries = append(allEntries, list.Watching...) |
| 1559 | allEntries = append(allEntries, list.Completed...) | 1560 | allEntries = append(allEntries, list.Completed...) |
| @@ -1561,22 +1562,22 @@ func getEntriesByCategory(list AnimeList, category string) []Entry { | |||
| 1561 | allEntries = append(allEntries, list.Dropped...) | 1562 | allEntries = append(allEntries, list.Dropped...) |
| 1562 | allEntries = append(allEntries, list.Planning...) | 1563 | allEntries = append(allEntries, list.Planning...) |
| 1563 | allEntries = append(allEntries, list.Rewatching...) | 1564 | allEntries = append(allEntries, list.Rewatching...) |
| 1564 | return allEntries | 1565 | return dedupeEntriesByMediaID(allEntries) |
| 1565 | case "CURRENT": | 1566 | case "CURRENT": |
| 1566 | currentEntries := make([]Entry, 0, len(list.Watching)+len(list.Rewatching)) | 1567 | currentEntries := make([]Entry, 0, len(list.Watching)+len(list.Rewatching)) |
| 1567 | currentEntries = append(currentEntries, list.Watching...) | 1568 | currentEntries = append(currentEntries, list.Watching...) |
| 1568 | currentEntries = append(currentEntries, list.Rewatching...) | 1569 | currentEntries = append(currentEntries, list.Rewatching...) |
| 1569 | return currentEntries | 1570 | return dedupeEntriesByMediaID(currentEntries) |
| 1570 | case "COMPLETED": | 1571 | case "COMPLETED": |
| 1571 | return list.Completed | 1572 | return dedupeEntriesByMediaID(list.Completed) |
| 1572 | case "PAUSED": | 1573 | case "PAUSED": |
| 1573 | return list.Paused | 1574 | return dedupeEntriesByMediaID(list.Paused) |
| 1574 | case "DROPPED": | 1575 | case "DROPPED": |
| 1575 | return list.Dropped | 1576 | return dedupeEntriesByMediaID(list.Dropped) |
| 1576 | case "PLANNING": | 1577 | case "PLANNING": |
| 1577 | return list.Planning | 1578 | return dedupeEntriesByMediaID(list.Planning) |
| 1578 | case "REWATCHING": // Added for completeness, though "ALL" covers it. | 1579 | case "REWATCHING": // Added for completeness, though "ALL" covers it. |
| 1579 | return list.Rewatching | 1580 | return dedupeEntriesByMediaID(list.Rewatching) |
| 1580 | default: | 1581 | default: |
| 1581 | return []Entry{} | 1582 | return []Entry{} |
| 1582 | } | 1583 | } |
internal/logic_regression_test.go +141
| @@ -288,6 +288,147 @@ func TestParseAnimeListMalformedInputReturnsEmptyList(t *testing.T) { | |||
| 288 | } | 288 | } |
| 289 | } | 289 | } |
| 290 | 290 | ||
| 291 | func 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 | |||
| 365 | func 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 | |||
| 412 | func 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 | |||
| 291 | func TestMediaDisplayTitleFallsBackToID(t *testing.T) { | 432 | func TestMediaDisplayTitleFallsBackToID(t *testing.T) { |
| 292 | title := mediaDisplayTitle(Media{ID: 42}, &CurdConfig{AnimeNameLanguage: "english"}) | 433 | title := mediaDisplayTitle(Media{ID: 42}, &CurdConfig{AnimeNameLanguage: "english"}) |
| 293 | if title != "42" { | 434 | if title != "42" { |