-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
273 lines (253 loc) · 11.1 KB
/
Main.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
using AniListNet;
using AniListNet.Objects;
using AniListNet.Parameters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.Anilist
{
public class Anilist : IPlugin, ISettingProvider, IContextMenu
{
private readonly AniClient _client = new AniClient();
private PluginInitContext _context;
private Settings _settings;
public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_client.TryAuthenticateAsync(_settings.AnilistToken);
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
if (query.Search.Length > 2)
{
string searchQuery = query.Search;
switch (_settings.DefaultMediaType)
{
case MediaType.Anime:
var animeSearchResults = _client.SearchMediaAsync(new SearchMediaFilter
{
Query = searchQuery,
Type = MediaType.Anime,
Sort = _settings.DefaultMediaSort,
Format = new Dictionary<MediaFormat, bool>
{
{ MediaFormat.TV, true},
{ MediaFormat.Movie, true},
{ MediaFormat.TVShort, true},
{ MediaFormat.ONA, true},
}
});
foreach (var anime in animeSearchResults.Result.Data)
{
var result = new Result();
result.Title = anime.Title.EnglishTitle ?? anime.Title.RomajiTitle;
if (anime.Entry != null)
{
result.SubTitle = $"Format: {anime.Format} | Status: {anime.Status} \n" +
$"Watched: {anime.Entry?.Progress ?? 0} / {anime.Episodes}";
}
else
{
result.SubTitle = $"Format: {anime.Format} | Status: {anime.Status} \n" +
$"Episodes: {anime.Episodes?.ToString() ?? "?"}";
}
result.IcoPath = anime.Cover.ExtraLargeImageUrl.ToString();
result.Action = e =>
{
string url = $"https://anilist.co/anime/{anime.Id}";
_context.API.OpenUrl(url);
return true;
};
result.ContextData = anime;
results.Add(result);
}
break;
case MediaType.Manga:
var mangaSearchResults = _client.SearchMediaAsync(new SearchMediaFilter
{
Query = searchQuery,
Type = MediaType.Manga,
Sort = MediaSort.Popularity,
Format = new Dictionary<MediaFormat, bool>
{
{ MediaFormat.Manga, true},
{ MediaFormat.OneShot, true}
}
});
foreach (var manga in mangaSearchResults.Result.Data)
{
var result = new Result();
result.Title = manga.Title.EnglishTitle ?? manga.Title.RomajiTitle;
if (manga.Entry != null)
{
result.SubTitle = $"Format: {manga.Format} | Status: {manga.Status} \n" +
$"Chapters Read: {manga.Entry?.Progress ?? 0} / {manga.Chapters} | Volumes Read: {manga.Entry?.VolumeProgress ?? 0} / {manga.Volumes}";
}
else
{
result.SubTitle = $"Format: {manga.Format} | Status: {manga.Status} \n" +
$"Chapters: {manga.Chapters} | Volumes: {manga.Volumes}";
}
result.IcoPath = manga.Cover.ExtraLargeImageUrl.ToString();
result.Action = e =>
{
string url = $"https://anilist.co/anime/{manga.Id}";
_context.API.OpenUrl(url);
return true;
};
result.ContextData = manga;
results.Add(result);
}
break;
}
}
/*results.Add(new Result
{
Title = "Search",
Action = c =>
{
return true;
}
});*/
return results;
}
public Control CreateSettingPanel()
{
return new AnilistSetting(_context, _settings);
}
public List<Result> LoadContextMenus(Result selectedResult)
{
var results = new List<Result>();
var media = selectedResult.ContextData as Media;
Func<ActionContext, ValueTask<bool>> PlusOneActionCreator(bool updateVolume)
{
return async ValueTask<bool> (ActionContext c) =>
{
try
{
var mediaEntryMutation = new MediaEntryMutation();
switch (media.Type)
{
case MediaType.Anime:
mediaEntryMutation.Progress = media.Entry.Progress + 1;
if (mediaEntryMutation.Progress >= media.Episodes)
{
mediaEntryMutation.Status = MediaEntryStatus.Completed;
mediaEntryMutation.CompleteDate = DateTime.Now;
}
break;
case MediaType.Manga:
// TODO: Support volume progress
if (updateVolume)
{
mediaEntryMutation.VolumeProgress = media.Entry.VolumeProgress + 1;
}
else
{
mediaEntryMutation.Progress = media.Entry.Progress + 1;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
await _client.SaveMediaEntryAsync(media.Id, mediaEntryMutation);
}
catch (Exception e)
{
// ignored
}
return true;
};
}
// If already on list
if (media.Entry != null)
{
if (media.Type == MediaType.Anime)
{
// Add a +1 progress option
results.Add(new ()
{
Title = "+1 Watched",
IcoPath = "Assets\\AniListLogo.png",
AsyncAction = PlusOneActionCreator(false),
});
}
else
{
results.Add(new ()
{
Title = "+1 Chapter Read",
IcoPath = "Assets\\AniListLogo.png",
AsyncAction = PlusOneActionCreator(false)
});
results.Add(new ()
{
Title = "+1 Volume Read",
IcoPath = "Assets\\AniListLogo.png",
AsyncAction = PlusOneActionCreator(true)
});
}
results.Add(new Result
{
Title = "Remove from list",
IcoPath = "Assets\\AniListlogo.png",
AsyncAction = async c =>
{
try
{
var anilistEntry = await _client.SearchMediaAsync(new SearchMediaFilter
{
Query = selectedResult.Title,
Type = _settings.DefaultMediaType,
Sort = MediaSort.Popularity
});
await _client.DeleteMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id);
}
catch (Exception)
{
_context.API.ShowMsgError("Not Authenticated", "Please add your token in the plugin settings");
}
return true;
},
});
}
// If not on list, add an option to add to list
else
{
results.Add(new Result
{
Title = "Add to list",
IcoPath = "Assets\\AniListlogo.png",
AsyncAction = async c =>
{
try
{
var anilistEntry = await _client.SearchMediaAsync(new SearchMediaFilter
{
Query = selectedResult.Title,
Type = _settings.DefaultMediaType,
Sort = MediaSort.Popularity
});
await _client.SaveMediaEntryAsync(anilistEntry.Data.FirstOrDefault()!.Id, new MediaEntryMutation()
{
StartDate = DateTime.Today,
Progress = 0,
Status = MediaEntryStatus.Planning
});
}
catch (Exception)
{
_context.API.ShowMsgError("Not Authenticated", "Please add your token in the plugin settings");
}
return true;
},
});
}
return results;
}
}
}