Commit e8fb17b059

e8fb17b059eb56860b1acee8b1a216872d198baa

parent: 9811645041

Unsigned

Wraient <wraient.911@gmail.com> · 2026-05-23 00:29 UTC

Preview tracker sync changes

Layout: unified · split

internal/tracking.go +158
@@ -2,6 +2,7 @@ package internal
22
33import (
44 "bufio"
5 "encoding/json"
56 "fmt"
67 "os"
78 "path/filepath"
@@ -289,6 +290,94 @@ func hasAnyEntries(list AnimeList) bool {
289290 return len(list.Watching)+len(list.Completed)+len(list.Paused)+len(list.Dropped)+len(list.Planning)+len(list.Rewatching) > 0
290291}
291292
293func animeListEntryCount(list AnimeList) int {
294 return len(getEntriesByCategory(list, "ALL"))
295}
296
297func countEntriesMissingMyAnimeListID(list AnimeList) int {
298 count := 0
299 for _, entry := range getEntriesByCategory(list, "ALL") {
300 if entry.Media.MalID == 0 {
301 count++
302 }
303 }
304 return count
305}
306
307func countAniListDeletes(list AnimeList) int {
308 count := 0
309 for _, entry := range getEntriesByCategory(list, "ALL") {
310 if entry.ListID != 0 {
311 count++
312 }
313 }
314 return count
315}
316
317type remoteSyncPreview struct {
318 Action string
319 AniListEntries int
320 MyAnimeListEntries int
321 Writes int
322 Deletes int
323 MissingMALIDs int
324}
325
326func confirmRemoteSync(preview remoteSyncPreview) (bool, error) {
327 CurdOut(fmt.Sprintf("%s: AniList %d, MyAnimeList %d.", preview.Action, preview.AniListEntries, preview.MyAnimeListEntries))
328 if preview.Deletes > 0 {
329 CurdOut(fmt.Sprintf("This will write %d entries and delete %d entries.", preview.Writes, preview.Deletes))
330 } else {
331 CurdOut(fmt.Sprintf("This will write %d entries.", preview.Writes))
332 }
333 if preview.MissingMALIDs > 0 {
334 CurdOut(fmt.Sprintf("%d entries need a MyAnimeList ID lookup.", preview.MissingMALIDs))
335 }
336
337 selected, err := DynamicSelect([]SelectionOption{
338 {Key: "continue", Label: "Continue"},
339 {Key: "cancel", Label: "Cancel"},
340 })
341 if err != nil {
342 return false, err
343 }
344 return selected.Key == "continue", nil
345}
346
347func writeTrackingBackup(config *CurdConfig, action string, aniList, myAnimeList AnimeList) (string, error) {
348 if config == nil {
349 return "", fmt.Errorf("missing config")
350 }
351
352 backupDir := filepath.Join(os.ExpandEnv(config.StoragePath), "tracking-backups")
353 if err := os.MkdirAll(backupDir, 0755); err != nil {
354 return "", err
355 }
356
357 slug := strings.NewReplacer(" ", "-", "+", "-", ":", "", "/", "-").Replace(strings.ToLower(action))
358 backupPath := filepath.Join(backupDir, fmt.Sprintf("%s-%s.json", time.Now().Format("20060102-150405"), slug))
359 payload := struct {
360 Action string `json:"action"`
361 CreatedAt time.Time `json:"created_at"`
362 AniList AnimeList `json:"anilist"`
363 MyAnimeList AnimeList `json:"myanimelist"`
364 }{
365 Action: action,
366 CreatedAt: time.Now(),
367 AniList: aniList,
368 MyAnimeList: myAnimeList,
369 }
370
371 data, err := json.MarshalIndent(payload, "", " ")
372 if err != nil {
373 return "", err
374 }
375 if err := os.WriteFile(backupPath, data, 0644); err != nil {
376 return "", err
377 }
378 return backupPath, nil
379}
380
292381func currentFuzzyDate() FuzzyDate {
293382 now := time.Now()
294383 return FuzzyDate{
@@ -1268,6 +1357,30 @@ func ReplaceMyAnimeListWithAniList(config *CurdConfig) error {
12681357 if err != nil {
12691358 return err
12701359 }
1360 targetList, err := FetchLatestMyAnimeList(config, &User{})
1361 if err != nil {
1362 return err
1363 }
1364 ok, err := confirmRemoteSync(remoteSyncPreview{
1365 Action: "Replace MyAnimeList with AniList",
1366 AniListEntries: animeListEntryCount(sourceList),
1367 MyAnimeListEntries: animeListEntryCount(targetList),
1368 Writes: animeListEntryCount(sourceList),
1369 Deletes: animeListEntryCount(targetList),
1370 MissingMALIDs: countEntriesMissingMyAnimeListID(sourceList),
1371 })
1372 if err != nil {
1373 return err
1374 }
1375 if !ok {
1376 CurdOut("Tracker sync cancelled.")
1377 return nil
1378 }
1379 backupPath, err := writeTrackingBackup(config, "replace-myanimelist-with-anilist", sourceList, targetList)
1380 if err != nil {
1381 return err
1382 }
1383 CurdOut(fmt.Sprintf("Tracker backup saved: %s", backupPath))
12711384 if err := wipeMyAnimeListRemote(config); err != nil {
12721385 return err
12731386 }
@@ -1283,6 +1396,30 @@ func ReplaceAniListWithMyAnimeList(config *CurdConfig) error {
12831396 if err != nil {
12841397 return err
12851398 }
1399 targetList, err := fetchAniListAnimeListFromToken(token)
1400 if err != nil {
1401 return err
1402 }
1403 ok, err := confirmRemoteSync(remoteSyncPreview{
1404 Action: "Replace AniList with MyAnimeList",
1405 AniListEntries: animeListEntryCount(targetList),
1406 MyAnimeListEntries: animeListEntryCount(sourceList),
1407 Writes: animeListEntryCount(sourceList),
1408 Deletes: countAniListDeletes(targetList),
1409 MissingMALIDs: countEntriesMissingMyAnimeListID(sourceList),
1410 })
1411 if err != nil {
1412 return err
1413 }
1414 if !ok {
1415 CurdOut("Tracker sync cancelled.")
1416 return nil
1417 }
1418 backupPath, err := writeTrackingBackup(config, "replace-anilist-with-myanimelist", targetList, sourceList)
1419 if err != nil {
1420 return err
1421 }
1422 CurdOut(fmt.Sprintf("Tracker backup saved: %s", backupPath))
12861423 if err := wipeAniListRemote(config); err != nil {
12871424 return err
12881425 }
@@ -1308,6 +1445,27 @@ func MergeRemoteLists(config *CurdConfig) error {
13081445 }
13091446 myAnimeListUser.AnimeList = myAnimeList
13101447
1448 plan := buildDualRemoteSyncPlan(aniList, myAnimeList)
1449 ok, err := confirmRemoteSync(remoteSyncPreview{
1450 Action: "Merge AniList and MyAnimeList",
1451 AniListEntries: animeListEntryCount(aniList),
1452 MyAnimeListEntries: animeListEntryCount(myAnimeList),
1453 Writes: len(plan.AniListUpdates) + len(plan.MyAnimeListUpdates),
1454 Deletes: 0,
1455 MissingMALIDs: countEntriesMissingMyAnimeListID(plan.Merged),
1456 })
1457 if err != nil {
1458 return err
1459 }
1460 if !ok {
1461 CurdOut("Tracker sync cancelled.")
1462 return nil
1463 }
1464 backupPath, err := writeTrackingBackup(config, "merge-anilist-and-myanimelist", aniList, myAnimeList)
1465 if err != nil {
1466 return err
1467 }
1468 CurdOut(fmt.Sprintf("Tracker backup saved: %s", backupPath))
13111469 _, err = syncDualRemoteTrackers(config, token, aniListUser, myAnimeListUser)
13121470 return err
13131471}