-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
261 lines (199 loc) · 7.15 KB
/
MainWindow.xaml.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
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using AniListNet;
using AniListNet.Objects;
using AniListNet.Parameters;
using FuzzySharp;
using Process = System.Diagnostics.Process;
namespace AnilistListConverter;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private const string AuthUrl = "https://anilist.co/api/v2/oauth/authorize?client_id=21239&response_type=token";
AniClient aniClient = new AniClient();
public MainWindow()
{
InitializeComponent();
this.MouseDown += delegate (object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) DragMove(); };
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
private void ApiButton_OnClick(object sender, RoutedEventArgs e)
{
Process.Start(new ProcessStartInfo
{
FileName = AuthUrl,
UseShellExecute = true
});
// Enable the ApiKey TextBox
ApiKey.IsEnabled = true;
Confirm.Content = "Click to check Api Token";
Confirm.IsEnabled = true;
}
private void ToggleList_OnClick(object sender, RoutedEventArgs e)
{
bool toggle = ToggleList.IsChecked.GetValueOrDefault();
if (toggle)
{
ToggleList.Content = "Move Anime to Manga";
}
else
{
ToggleList.Content = "Move Manga to Anime";
}
}
private async void ConfirmSettings_OnClick(object sender, RoutedEventArgs e)
{
if (!ToggleList.IsEnabled)
{
//Check the Api Key.
var result = await aniClient.TryAuthenticateAsync(ApiKey.Text);
if (!aniClient.IsAuthenticated)
{
Confirm.Width = 500;
Confirm.Content = "Token rejected, please try again with a new one.";
Confirm.Background = new SolidColorBrush(Colors.Coral);
}
else
{
ApiButton.IsEnabled = false;
ApiKey.IsEnabled = false;
ToggleList.IsEnabled = true;
Confirm.IsEnabled = true;
Confirm.Content = "Click to move Entries.";
}
}
else
{
//Move the Entries.
bool toggle = ToggleList.IsChecked.Value;
MoveLists(toggle);
}
}
private async void MoveLists(bool direction)
{
MediaType originalType;
MediaType newType;
Random random = new Random();
if (!direction)
{
originalType = MediaType.Manga;
newType = MediaType.Anime;
}
else
{
originalType = MediaType.Anime;
newType = MediaType.Manga;
}
Confirm.IsEnabled = false;
ToggleList.IsEnabled = false;
var user = await aniClient.GetAuthenticatedUserAsync();
var entryFilter = new MediaEntryFilter
{
Type = originalType,
};
int page = 0;
var pagination = new AniPaginationOptions(page, 25);
var mediaEntryCollection = await aniClient.GetUserEntryCollectionAsync(user.Id, originalType, pagination);
if (mediaEntryCollection.Lists.Length <= 0)
{
Confirm.Width = 500;
Confirm.Content = "No Entries found.";
Confirm.Background = new SolidColorBrush(Colors.Coral);
return;
}
List<MediaEntry> unfilteredEntries = new List<MediaEntry>();
do
{
foreach (var list in mediaEntryCollection.Lists)
{
unfilteredEntries.AddRange(list.Entries);
}
if (mediaEntryCollection.HasNextChunk)
{
page++;
pagination = new AniPaginationOptions(page, 25);
mediaEntryCollection = await aniClient.GetUserEntryCollectionAsync(user.Id, originalType, pagination);
}
} while (mediaEntryCollection.HasNextChunk);
if (unfilteredEntries.Count <= 0)
{
Confirm.Width = 500;
Confirm.Content = "No Entries found.";
Confirm.Background = new SolidColorBrush(Colors.Coral);
return;
}
List<MediaEntry> entries = new List<MediaEntry>();
// API Optimizisations, check beforehand, instead of spamming the API
foreach (var entry in unfilteredEntries)
{
if (entry.Media.Type != originalType)
{
continue;
}
if (entry.Status != MediaEntryStatus.Planning)
{
continue;
}
entries.Add(entry);
}
Progress.Visibility= Visibility.Visible;
Confirm.Background = new SolidColorBrush(Colors.ForestGreen);
Double progressPerEntry = 100.0 / entries.Count ;
Double currentProgress = 0;
int currentEntry = 0;
List<string?> notFound = new List<string?>();
foreach (var data in entries)
{
currentProgress = currentProgress + progressPerEntry;
Progress.Value = currentProgress;
Confirm.Content = $"Moving {entries.Count - currentEntry} Entries.";
currentEntry++;
int delayTime = random.Next(6000,7000);
await Task.Delay(delayTime);
int newID = 0;
var filter = new SearchMediaFilter
{
Query = data.Media.Title.NativeTitle,
Type = newType,
Sort = MediaSort.Popularity,
};
var results = await aniClient.SearchMediaAsync(filter, new AniPaginationOptions(1, 20));
if (results.TotalCount > 0)
{
foreach (var result in results.Data)
{
int score = Fuzz.Ratio(result.Title.NativeTitle, data.Media.Title.NativeTitle);
if (score >= 85)
{
newID = result.Id;
break;
}
}
}
else
{
await aniClient.DeleteMediaEntryAsync(data.Id);
continue;
}
await aniClient.DeleteMediaEntryAsync(data.Id);
if (newID == 0)
continue;
var mutation = new MediaEntryMutation
{
// Set properties of the mutation object as needed
Status = MediaEntryStatus.Planning,
Progress = 0
};
await aniClient.SaveMediaEntryAsync(newID,mutation);
}
Confirm.Content = $"Moved all Entries.";
}
}