-
Notifications
You must be signed in to change notification settings - Fork 38
/
Maintenance.cs
640 lines (560 loc) · 26.8 KB
/
Maintenance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
namespace TraktRater
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using global::TraktRater.Settings;
using global::TraktRater.TraktAPI.DataStructures;
using global::TraktRater.UI;
static class Maintenance
{
public static bool Cancel { get; set; }
public static void RemoveEpisodesFromWatchedHistory()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watched shows from trakt.tv");
var watchedShows = TraktAPI.TraktAPI.GetWatchedShows();
if (watchedShows != null)
{
int i = 0;
int count = watchedShows.Count();
UIUtils.UpdateStatus("Found {0} shows with {1} episodes watched ({2} plays) on trakt.tv", count, watchedShows.Sum(w => w.Seasons.Sum(we => we.Episodes.Count())), watchedShows.Sum(s => s.Plays));
// remove one show at a time
// there could be many underlying episodes per show
foreach (var watchedShow in watchedShows)
{
if (Cancel) return;
var syncData = new TraktShowSync
{
Shows = new List<TraktShow>
{
new TraktShow
{
Ids = new TraktShowId
{
Trakt = watchedShow.Show.Ids.Trakt
}
}
}
};
UIUtils.UpdateStatus("[{0}/{1}] Removing all episodes of {2} from trakt.tv watched history", ++i, count, watchedShow.Show.Title);
var syncResponse = TraktAPI.TraktAPI.RemoveShowsFromWatchedHistory(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("Failed to remove episodes of {0} from trakt.tv watched history", watchedShow.Show.Title), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watched shows from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveMoviesFromWatchedHistory()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watched movies from trakt.tv");
var watchedMovies = TraktAPI.TraktAPI.GetWatchedMovies();
if (watchedMovies != null)
{
UIUtils.UpdateStatus("Found {0} movies watched ({1} plays) on trakt.tv", watchedMovies.Count(), watchedMovies.Sum(s => s.Plays));
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)watchedMovies.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktMovieSync
{
Movies = watchedMovies.Select(w => w.Movie).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing movies from trakt.tv watched history", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveMoviesFromWatchedHistory(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove movies from trakt.tv watched history", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watched movies from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveEpisodesFromCollection()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting collected shows from trakt.tv");
var collectedShows = TraktAPI.TraktAPI.GetCollectedShows();
if (collectedShows != null)
{
int i = 0;
int count = collectedShows.Count();
UIUtils.UpdateStatus("Found {0} shows with {1} episodes collected on trakt.tv", count, collectedShows.Sum(c => c.Seasons.Sum(ce => ce.Episodes.Count())));
// remove one show at a time
// there could be many underlying episodes per show
foreach (var collectedShow in collectedShows)
{
if (Cancel) return;
var syncData = new TraktShowSync
{
Shows = new List<TraktShow>
{
new TraktShow
{
Ids = new TraktShowId
{
Trakt = collectedShow.Show.Ids.Trakt
}
}
}
};
UIUtils.UpdateStatus("[{0}/{1}] Removing all episodes of {2} from trakt.tv collection", ++i, count, collectedShow.Show.Title);
var syncResponse = TraktAPI.TraktAPI.RemoveShowsFromCollection(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("Failed to remove episodes of {0} from trakt.tv collection", collectedShow.Show.Title), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of collected shows from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveMoviesFromCollection()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting collected movies from trakt.tv");
var collectedMovies = TraktAPI.TraktAPI.GetCollectedMovies();
if (collectedMovies != null)
{
UIUtils.UpdateStatus("Found {0} movies collected on trakt.tv", collectedMovies.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)collectedMovies.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktMovieSync
{
Movies = collectedMovies.Select(c => c.Movie).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing movies from trakt.tv collection", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveMoviesFromCollection(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove movies from trakt.tv collection", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of collected movies from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveEpisodesFromRatings()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting rated episodes from trakt.tv");
var ratedEpisodes = TraktAPI.TraktAPI.GetRatedEpisodes();
if (ratedEpisodes != null)
{
UIUtils.UpdateStatus("Found {0} episodes rated on trakt.tv", ratedEpisodes.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)ratedEpisodes.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktEpisodeSync
{
Episodes = ratedEpisodes.Select(r => new TraktEpisode { Ids = r.Episode.Ids })
.Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing episodes from trakt.tv ratings", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveEpisodesFromRatings(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove episodes from trakt.tv ratings", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of rated episodes from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveShowsFromRatings()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting rated shows from trakt.tv");
var ratedShows = TraktAPI.TraktAPI.GetRatedShows();
if (ratedShows != null)
{
UIUtils.UpdateStatus("Found {0} shows rated on trakt.tv", ratedShows.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)ratedShows.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktShowSync
{
Shows = ratedShows.Select(r => r.Show).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing shows from trakt.tv ratings", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveShowsFromRatings(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove shows from trakt.tv ratings", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of rated shows from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveSeasonsFromRatings()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting rated seasons from trakt.tv");
var ratedSeasons = TraktAPI.TraktAPI.GetRatedSeasons();
if (ratedSeasons != null)
{
// group the seasons by tv show id
// remove season ratings by show i.e. one show at a time
// we get ratings for seasons ungrouped and sorted by date added
var seasonGroupings = ratedSeasons.GroupBy(r => r.Show.Ids.Trakt, r => r);
UIUtils.UpdateStatus("Found {0} seasons rated in {1} shows on trakt.tv", ratedSeasons.Count(), seasonGroupings.Count());
int i = 0;
int count = seasonGroupings.Count();
foreach (var seasonGroup in seasonGroupings)
{
if (Cancel) return;
// get the seasons rated for this show
var seasons = from rating in seasonGroup
select new TraktSeason
{
Number = rating.Season.Number
};
var syncData = new TraktSeasonSync
{
Shows = new List<TraktSeasonSync.TraktShowSeason>
{
new TraktSeasonSync.TraktShowSeason
{
Ids = new TraktShowId { Trakt = seasonGroup.Key },
Seasons = seasons.ToList()
}
}
};
UIUtils.UpdateStatus("[{0}/{1}] Removing season for {2} from trakt.tv ratings", ++i, count, seasonGroup.First().Show.Title);
var syncResponse = TraktAPI.TraktAPI.RemoveSeasonsFromRatings(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove {2} seasons from trakt.tv ratings", i, count, seasonGroup.First().Show.Title), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of rated seasons from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveMoviesFromRatings()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting rated movies from trakt.tv");
var ratedMovies = TraktAPI.TraktAPI.GetRatedMovies();
if (ratedMovies != null)
{
UIUtils.UpdateStatus("Found {0} movies rated on trakt.tv", ratedMovies.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)ratedMovies.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktMovieSync
{
Movies = ratedMovies.Select(r => r.Movie).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing movies from trakt.tv ratings", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveMoviesFromRatings(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove movies from trakt.tv ratings", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of rated movies from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveEpisodesFromWatchlist()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watchlisted episodes from trakt.tv");
var watchlistedEpisodes = TraktAPI.TraktAPI.GetWatchlistEpisodes();
if (watchlistedEpisodes != null)
{
UIUtils.UpdateStatus("Found {0} episodes watchlisted on trakt.tv", watchlistedEpisodes.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)watchlistedEpisodes.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktEpisodeSync
{
Episodes = watchlistedEpisodes.Select(r => new TraktEpisode { Ids = r.Episode.Ids })
.Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing episodes from trakt.tv watchlist", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveEpisodesFromWatchlist(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove episodes from trakt.tv watchlist", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watchlisted episodes from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveShowsFromWatchlist()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watchlisted shows from trakt.tv");
var watchlistedShows = TraktAPI.TraktAPI.GetWatchlistShows();
if (watchlistedShows != null)
{
UIUtils.UpdateStatus("Found {0} shows watchlisted on trakt.tv", watchlistedShows.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)watchlistedShows.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktShowSync
{
Shows = watchlistedShows.Select(r => r.Show).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing shows from trakt.tv watchlist", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveShowsFromWatchlist(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove shows from trakt.tv watchlist", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watchlisted shows from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveSeasonsFromWatchlist()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watchlisted seasons from trakt.tv");
var watchlistedSeasons = TraktAPI.TraktAPI.GetWatchlistSeasons();
if (watchlistedSeasons != null)
{
// group the seasons by tv show id
// remove watchlist seasons by show i.e. one show at a time
// we get watchlist for seasons ungrouped and sorted by date added
var seasonGroupings = watchlistedSeasons.GroupBy(r => r.Show.Ids.Trakt, r => r);
UIUtils.UpdateStatus("Found {0} seasons watchlisted in {1} shows on trakt.tv", watchlistedSeasons.Count(), seasonGroupings.Count());
int i = 0;
int count = seasonGroupings.Count();
foreach (var seasonGroup in seasonGroupings)
{
if (Cancel) return;
// get the seasons watchlisted for this show
var seasons = from rating in seasonGroup
select new TraktSeason
{
Number = rating.Season.Number
};
var syncData = new TraktSeasonSync
{
Shows = new List<TraktSeasonSync.TraktShowSeason>
{
new TraktSeasonSync.TraktShowSeason
{
Ids = new TraktShowId { Trakt = seasonGroup.Key },
Seasons = seasons.ToList()
}
}
};
UIUtils.UpdateStatus("[{0}/{1}] Removing seasons for {2} from trakt.tv watchlist", ++i, count, seasonGroup.First().Show.Title);
var syncResponse = TraktAPI.TraktAPI.RemoveSeasonsFromWatchlist(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove {2} seasons from trakt.tv watchlist", i, count, seasonGroup.First().Show.Title), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watchlisted seasons from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveMoviesFromWatchlist()
{
if (Cancel) return;
UIUtils.UpdateStatus("Getting watchlisted movies from trakt.tv");
var watchlistedMovies = TraktAPI.TraktAPI.GetWatchlistMovies();
if (watchlistedMovies != null)
{
UIUtils.UpdateStatus("Found {0} movies watchlisted on trakt.tv", watchlistedMovies.Count());
int pageSize = AppSettings.BatchSize;
int pages = (int)Math.Ceiling((double)watchlistedMovies.Count() / pageSize);
for (int i = 0; i < pages; i++)
{
if (Cancel) return;
var syncData = new TraktMovieSync
{
Movies = watchlistedMovies.Select(r => r.Movie).Skip(i * pageSize).Take(pageSize).ToList()
};
UIUtils.UpdateStatus("[{0}/{1}] Removing movies from trakt.tv watchlist", i + 1, pages);
var syncResponse = TraktAPI.TraktAPI.RemoveMoviesFromWatchlist(syncData);
if (syncResponse == null)
{
UIUtils.UpdateStatus(string.Format("[{0}/{1}] Failed to remove movies from trakt.tv watchlist", i + 1, pages), true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of watchlisted movies from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveEpisodePausedState()
{
if (Cancel) return;
// get paused info for episodes
UIUtils.UpdateStatus("Getting paused state for episodes from trakt.tv");
var pausedEpisodes = TraktAPI.TraktAPI.GetPausedEpisodes();
if (pausedEpisodes != null)
{
int i = 0;
int count = pausedEpisodes.Count(p => p.Type == "episode");
UIUtils.UpdateStatus($"Found {count} episodes with a paused state on trakt.tv");
// remove pause info for each episode
foreach (var item in pausedEpisodes.Where(p => p.Type == "episode"))
{
if (Cancel) return;
UIUtils.UpdateStatus($"[{++i}/{count}] Removing paused state for {item.Show.Title} - {item.Episode.Season}x{item.Episode.Number} - {item.Episode.Title} from trakt.tv. Paused At={item.PausedAt}, Progress={item.Progress}%");
bool syncResponse = TraktAPI.TraktAPI.RemovePausedState(item.Id);
if (syncResponse == false)
{
UIUtils.UpdateStatus($"Failed to remove paused state for {item.Show.Title} - {item.Episode.Season}x{item.Episode.Number} - {item.Episode.Title} from trakt.tv", true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of paused epsiodes from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveMoviePausedState()
{
if (Cancel) return;
// get paused info for episodes
UIUtils.UpdateStatus("Getting paused state for movies from trakt.tv");
var pausedMovies = TraktAPI.TraktAPI.GetPausedMovies();
if (pausedMovies != null)
{
int i = 0;
int count = pausedMovies.Count(p => p.Type == "movie");
UIUtils.UpdateStatus($"Found {count} movies with a paused state on trakt.tv");
// remove pause info for each episode
foreach (var item in pausedMovies.Where(p => p.Type == "movie"))
{
if (Cancel) return;
UIUtils.UpdateStatus($"[{++i}/{count}] Removing paused state for {item.Movie.Title} ({item.Movie.Year}) from trakt.tv. Paused At={item.PausedAt}, Progress={item.Progress}%");
bool syncResponse = TraktAPI.TraktAPI.RemovePausedState(item.Id);
if (syncResponse == false)
{
UIUtils.UpdateStatus($"Failed to remove paused state for {item.Movie.Title} ({item.Movie.Year}) from trakt.tv", true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current list of paused movies from trakt.tv", true);
Thread.Sleep(2000);
}
}
public static void RemoveCustomLists()
{
if (Cancel) return;
// get paused info for episodes
UIUtils.UpdateStatus("Getting custom lists from trakt.tv");
var customLists = TraktAPI.TraktAPI.GetCustomLists();
if (customLists != null)
{
int i = 0;
int count = customLists.Count();
UIUtils.UpdateStatus($"Found {count} custom lists on trakt.tv");
foreach (var item in customLists)
{
if (Cancel) return;
UIUtils.UpdateStatus($"[{++i}/{count}] Removing custom list '{item.Name}' from trakt.tv.");
bool syncResponse = TraktAPI.TraktAPI.DeleteCustomList(item.Ids.Trakt.ToString());
if (syncResponse == false)
{
UIUtils.UpdateStatus($"Failed to remove custom list '{item.Name}' from trakt.tv", true);
Thread.Sleep(2000);
continue;
}
}
}
else
{
UIUtils.UpdateStatus("Failed to get current custom lists from trakt.tv", true);
Thread.Sleep(2000);
}
}
}
}