forked from RiisDev/ScriptBloxAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripts.cs
More file actions
355 lines (284 loc) · 17.1 KB
/
Scripts.cs
File metadata and controls
355 lines (284 loc) · 17.1 KB
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
using System;
using Newtonsoft.Json.Linq;
using ScriptBloxAPI.DataTypes;
using System.Collections.Generic;
using System.Linq;
using ScriptBloxAPI.Backend_Functions;
using System.Threading.Tasks;
// ReSharper disable UnusedMember.Global
#pragma warning disable IDE0270
namespace ScriptBloxAPI.Methods
{
public static class ScriptsMethods
{
public enum FilterType
{
Hot,
Verified,
Unverified,
Newest,
Oldest,
MostViewed,
LeastViewed,
Free,
Paid
}
#region Internal Functions
private static bool IsScriptDataInvalid(JToken scriptData)
{
return scriptData["game"] == null ||
scriptData["tags"] == null ||
scriptData["script"] == null ||
scriptData["views"] == null ||
scriptData["verified"] == null ||
scriptData["key"] == null ||
scriptData["isUniversal"] == null ||
scriptData["isPatched"] == null ||
scriptData["createdAt"] == null ||
scriptData["updatedAt"] == null ||
scriptData["likeCount"] == null ||
scriptData["dislikeCount"] == null ||
scriptData["slug"] == null ||
scriptData["id"] == null;
}
private static IEnumerable<string> GetSlugsFromResults(JToken json)
{
List<string> slugs = new();
try
{
json = JToken.Parse(json.ToString());
if (!json.HasValues) return slugs;
if (json["result"] == null) return slugs;
if (json["result"]["scripts"] == null) return slugs;
JArray scripts = json["result"]?["scripts"]?.ToObject<JArray>() ?? new JArray();
slugs.AddRange(scripts.Select(script => script.Value<string>("slug")));
}
catch (Exception ex)
{
throw new ScriptBloxException($"An error occurred in 'GetSlugsFromResults' please create an issue @ github: {ex}\n{ex.StackTrace}");
}
return slugs;
}
private static ScriptObject CreateScriptFromData(JToken scriptData)
{
GameObject game = new(scriptData["game"].Value<long>("gameId"),
scriptData["game"].Value<string>("name"),
scriptData["game"].Value<string>("imageUrl"));
return new ScriptObject(game,
scriptData.Value<string>("title"),
scriptData.Value<string>("_id"),
scriptData.Value<string>("slug"),
scriptData.Value<string>("script"),
scriptData.Value<string>("createdAt"),
scriptData.Value<string>("updatedAt"),
scriptData.Value<long>("views"),
scriptData.Value<long>("likeCount"),
scriptData.Value<long>("dislikeCount"),
scriptData.Value<bool>("isUniversal"),
scriptData.Value<bool>("isPatched"),
scriptData.Value<bool>("key"),
scriptData.Value<bool>("verified"),
new List<string>());
}
#endregion
#region Non Async
/// <summary>
/// Retrieves a script from ScriptBlox based on the provided ScriptBlox ID.
/// </summary>
/// <param name="bloxId">The ScriptBlox ID of the script to retrieve.</param>
/// <returns>The script retrieved from the API, or a default script if the retrieval fails or the data is invalid.</returns>
public static ScriptObject GetScriptFromScriptbloxId(string bloxId)
{
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetSafeString($"https://scriptblox.com/api/script/{bloxId}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["script"] == null)
throw new ScriptBloxException("Backend error occurred.");
JToken scriptData = jsonReturn["script"];
if (IsScriptDataInvalid(scriptData))
throw new ScriptBloxException("An error has occurred while parsing the json, please submit a bug report.");
return CreateScriptFromData(scriptData);
}
/// <summary>
/// Retrieves a list of scripts from the front page based on the provided page number.
/// </summary>
/// <param name="pageNumber">The page number of the front page scripts (default is 1).</param>
/// <returns>A list of Script objects representing the scripts from the front page.</returns>
public static List<ScriptObject> GetFrontPageScripts(int pageNumber = 1)
{
if (pageNumber < 1) pageNumber = 1;
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetSafeString($"https://scriptblox.com/api/script/fetch?page={pageNumber}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn);
return slugsToCheck.Select(GetScriptFromScriptbloxId).ToList();
}
/// <summary>
/// Retrieves a list of scripts from the front page based on the provided page number.
/// </summary>
/// <param name="pageNumber">The page number of the front page scripts (default is 1).</param>
/// <returns>A list of Script objects representing the scripts from the front page.</returns>
public static List<ScriptObject> GetScriptsFromPageNumber(int pageNumber = 1) => GetFrontPageScripts(pageNumber);
/// <summary>
/// Retrieves a list of scripts from ScriptBlox based on the provided search query and maximum results.
/// </summary>
/// <param name="searchQuery">The search query to filter the scripts.</param>
/// <param name="maxResults">The maximum number of results to retrieve (default is 20).</param>
/// <returns>A list of Script objects representing the scripts matching the search query.</returns>
public static List<ScriptObject> GetScriptsFromQuery(string searchQuery, int maxResults = 20)
{
if (maxResults < 1) maxResults = 1;
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetSafeString($"https://scriptblox.com/api/script/search?q={searchQuery}&page=1&max={maxResults}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn);
return slugsToCheck.Select(GetScriptFromScriptbloxId).ToList();
}
/// <summary>
/// Retrieves a list of scripts from ScriptBlox based on the provided username.
/// </summary>
/// <param name="username">The username of the user whose scripts to retrieve.</param>
/// <returns>A list of Script objects representing the scripts owned by the user.</returns>
public static List<ScriptObject> GetScriptsFromUser(string username)
{
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetSafeString($"https://scriptblox.com/api/user/scripts/{username}?page=1"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn.ToString());
return slugsToCheck.Select(GetScriptFromScriptbloxId).ToList();
}
/// <summary>
/// Retrieves a list of ScriptObjects based on the specified filter type asynchronously.
/// </summary>
/// <param name="filterType">The type of filter to apply.</param>
/// <returns>A list of ScriptObjects that match the specified filter type.</returns>
public static List<ScriptObject> GetScriptsWithFilter(FilterType filterType)
{
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetSafeString($@"https://scriptblox.com/api/script/fetch?page=1&filters[]={filterType.ToString().ToLower()}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn.ToString());
return slugsToCheck.Select(GetScriptFromScriptbloxId).ToList();
}
#endregion
#region Async
/// <summary>
/// Retrieves a script from ScriptBlox based on the provided ScriptBlox ID.
/// </summary>
/// <param name="bloxId">The ScriptBlox ID of the script to retrieve.</param>
/// <returns>The script retrieved from the API, or a default script if the retrieval fails or the data is invalid.</returns>
public static async Task<ScriptObject> GetScriptFromScriptbloxIdAsync(string bloxId)
{
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetSafeStringAsync($"https://scriptblox.com/api/script/{bloxId}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["script"] == null)
throw new ScriptBloxException("Backend error occurred.");
JToken scriptData = jsonReturn["script"];
if (IsScriptDataInvalid(scriptData))
throw new ScriptBloxException("An error has occurred while parsing the json, please submit a bug report.");
return CreateScriptFromData(scriptData);
}
/// <summary>
/// Retrieves a list of scripts from the front page based on the provided page number.
/// </summary>
/// <param name="pageNumber">The page number of the front page scripts (default is 1).</param>
/// <returns>A list of Script objects representing the scripts from the front page.</returns>
public static async Task<List<ScriptObject>> GetFrontPageScriptsAsync(int pageNumber = 1)
{
if (pageNumber < 1) pageNumber = 1;
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetSafeStringAsync($"https://scriptblox.com/api/script/fetch?page={pageNumber}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn);
List<Task<ScriptObject>> scriptTasks = slugsToCheck.Select(GetScriptFromScriptbloxIdAsync).ToList();
ScriptObject[] scripts = await Task.WhenAll(scriptTasks);
return scripts.ToList();
}
public static async Task<List<ScriptObject>> GetScriptsFromPageNumberAsync(int pageNumber = 1) => await GetFrontPageScriptsAsync(pageNumber);
/// <summary>
/// Retrieves a list of scripts from ScriptBlox based on the provided search query and maximum results.
/// </summary>
/// <param name="searchQuery">The search query to filter the scripts.</param>
/// <param name="maxResults">The maximum number of results to retrieve (default is 20).</param>
/// <returns>A list of Script objects representing the scripts matching the search query.</returns>
public static async Task<List<ScriptObject>> GetScriptsFromQueryAsync(string searchQuery, int maxResults = 20)
{
if (maxResults < 1) maxResults = 1;
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetSafeStringAsync($"https://scriptblox.com/api/script/search?q={searchQuery}&page=1&max={maxResults}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn);
List<Task<ScriptObject>> scriptTasks = slugsToCheck.Select(GetScriptFromScriptbloxIdAsync).ToList();
ScriptObject[] scripts = await Task.WhenAll(scriptTasks);
return scripts.ToList();
}
/// <summary>
/// Retrieves a list of scripts from ScriptBlox based on the provided username.
/// </summary>
/// <param name="username">The username of the user whose scripts to retrieve.</param>
/// <returns>A list of Script objects representing the scripts owned by the user.</returns>
public static async Task<List<ScriptObject>> GetScriptsFromUserAsync(string username)
{
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetSafeStringAsync($"https://scriptblox.com/api/user/scripts/{username}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn.ToString());
List<Task<ScriptObject>> scriptTasks = slugsToCheck.Select(GetScriptFromScriptbloxIdAsync).ToList();
ScriptObject[] scripts = await Task.WhenAll(scriptTasks);
return scripts.ToList();
}
/// <summary>
/// Retrieves a list of ScriptObjects based on the specified filter type asynchronously.
/// </summary>
/// <param name="filterType">The type of filter to apply.</param>
/// <returns>A list of ScriptObjects that match the specified filter type.</returns>
public static async Task<List<ScriptObject>> GetScriptsWithFilterAsync(FilterType filterType)
{
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetSafeStringAsync($@"https://scriptblox.com/api/script/fetch?page=1&filters[]={filterType.ToString().ToLower()}"));
if (jsonReturn == null)
throw new ScriptBloxException("An error has occurred while fetching the json, please submit a bug report.");
if (jsonReturn["message"] != null)
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
if (jsonReturn["result"]?["scripts"] == null)
throw new ScriptBloxException("Backend error occurred.");
IEnumerable<string> slugsToCheck = GetSlugsFromResults(jsonReturn.ToString());
List<Task<ScriptObject>> scriptTasks = slugsToCheck.Select(GetScriptFromScriptbloxIdAsync).ToList();
ScriptObject[] scripts = await Task.WhenAll(scriptTasks);
return scripts.ToList();
}
#endregion
}
}