Commit e8fb17b059
Unsigned
Layout: unified · split
internal/tracking.go +158
| @@ -2,6 +2,7 @@ package internal | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bufio" | 4 | "bufio" |
| 5 | "encoding/json" | ||
| 5 | "fmt" | 6 | "fmt" |
| 6 | "os" | 7 | "os" |
| 7 | "path/filepath" | 8 | "path/filepath" |
| @@ -289,6 +290,94 @@ func hasAnyEntries(list AnimeList) bool { | |||
| 289 | return len(list.Watching)+len(list.Completed)+len(list.Paused)+len(list.Dropped)+len(list.Planning)+len(list.Rewatching) > 0 | 290 | return len(list.Watching)+len(list.Completed)+len(list.Paused)+len(list.Dropped)+len(list.Planning)+len(list.Rewatching) > 0 |
| 290 | } | 291 | } |
| 291 | 292 | ||
| 293 | func animeListEntryCount(list AnimeList) int { | ||
| 294 | return len(getEntriesByCategory(list, "ALL")) | ||
| 295 | } | ||
| 296 | |||
| 297 | func 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 | |||
| 307 | func 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 | |||
| 317 | type remoteSyncPreview struct { | ||
| 318 | Action string | ||
| 319 | AniListEntries int | ||
| 320 | MyAnimeListEntries int | ||
| 321 | Writes int | ||
| 322 | Deletes int | ||
| 323 | MissingMALIDs int | ||
| 324 | } | ||
| 325 | |||
| 326 | func 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 | |||
| 347 | func 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 | |||
| 292 | func currentFuzzyDate() FuzzyDate { | 381 | func currentFuzzyDate() FuzzyDate { |
| 293 | now := time.Now() | 382 | now := time.Now() |
| 294 | return FuzzyDate{ | 383 | return FuzzyDate{ |
| @@ -1268,6 +1357,30 @@ func ReplaceMyAnimeListWithAniList(config *CurdConfig) error { | |||
| 1268 | if err != nil { | 1357 | if err != nil { |
| 1269 | return err | 1358 | return err |
| 1270 | } | 1359 | } |
| 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)) | ||
| 1271 | if err := wipeMyAnimeListRemote(config); err != nil { | 1384 | if err := wipeMyAnimeListRemote(config); err != nil { |
| 1272 | return err | 1385 | return err |
| 1273 | } | 1386 | } |
| @@ -1283,6 +1396,30 @@ func ReplaceAniListWithMyAnimeList(config *CurdConfig) error { | |||
| 1283 | if err != nil { | 1396 | if err != nil { |
| 1284 | return err | 1397 | return err |
| 1285 | } | 1398 | } |
| 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)) | ||
| 1286 | if err := wipeAniListRemote(config); err != nil { | 1423 | if err := wipeAniListRemote(config); err != nil { |
| 1287 | return err | 1424 | return err |
| 1288 | } | 1425 | } |
| @@ -1308,6 +1445,27 @@ func MergeRemoteLists(config *CurdConfig) error { | |||
| 1308 | } | 1445 | } |
| 1309 | myAnimeListUser.AnimeList = myAnimeList | 1446 | myAnimeListUser.AnimeList = myAnimeList |
| 1310 | 1447 | ||
| 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)) | ||
| 1311 | _, err = syncDualRemoteTrackers(config, token, aniListUser, myAnimeListUser) | 1469 | _, err = syncDualRemoteTrackers(config, token, aniListUser, myAnimeListUser) |
| 1312 | return err | 1470 | return err |
| 1313 | } | 1471 | } |