1- using Newtonsoft . Json . Linq ;
1+ using System ;
2+ using Newtonsoft . Json . Linq ;
23using ScriptBloxAPI . DataTypes ;
34using System . Collections . Generic ;
5+ using System . Diagnostics ;
46using System . Linq ;
57using ScriptBloxAPI . Backend_Functions ;
68using System . Threading . Tasks ;
9+ // ReSharper disable UnusedMember.Global
10+ #pragma warning disable IDE0270
711
812namespace ScriptBloxAPI . Methods
913{
1014 public class ScriptsMethods
1115 {
16+
17+ public enum FilterType
18+ {
19+ Hot ,
20+ Verified ,
21+ Unverified ,
22+ Newest ,
23+ Oldest ,
24+ MostViewed ,
25+ LeastViewed ,
26+ Free ,
27+ Paid
28+ }
29+
1230 #region Non Async
1331
1432 /// <summary>
@@ -111,6 +129,27 @@ public static List<ScriptObject> GetScriptsFromUser(string username)
111129 return slugsToCheck . Select ( GetScriptFromScriptbloxId ) . ToList ( ) ;
112130 }
113131
132+ /// <summary>
133+ /// Retrieves a list of ScriptObjects based on the specified filter type asynchronously.
134+ /// </summary>
135+ /// <param name="filterType">The type of filter to apply.</param>
136+ /// <returns>A list of ScriptObjects that match the specified filter type.</returns>
137+ public static List < ScriptObject > GetScriptsWithFilter ( FilterType filterType )
138+ {
139+ JToken jsonReturn = JToken . Parse ( MiscFunctions . HttpClient . GetStringAsync ( $@ "https://scriptblox.com/api/script/fetch?page=1&filters[]={ filterType . ToString ( ) . ToLower ( ) } ") . Result ) ;
140+
141+ if ( jsonReturn == null )
142+ throw new ScriptBloxException ( "An error has occurred while fetching the json, please submit a bug report." ) ;
143+ if ( jsonReturn [ "message" ] != null )
144+ throw new ScriptBloxException ( jsonReturn . Value < string > ( "message" ) ) ;
145+ if ( jsonReturn [ "result" ] ? [ "scripts" ] == null )
146+ throw new ScriptBloxException ( "Backend error occurred." ) ;
147+
148+ IEnumerable < string > slugsToCheck = GetSlugsFromResults ( jsonReturn . ToString ( ) ) ;
149+
150+ return slugsToCheck . Select ( GetScriptFromScriptbloxId ) . ToList ( ) ;
151+ }
152+
114153 #region Internal Functions
115154 private static bool IsScriptDataInvalid ( JToken scriptData )
116155 {
@@ -132,9 +171,12 @@ private static bool IsScriptDataInvalid(JToken scriptData)
132171
133172 private static IEnumerable < string > GetSlugsFromResults ( JToken json )
134173 {
135- List < string > slugs = new List < string > ( ) ;
174+ List < string > slugs = new ( ) ;
136175
137- JArray scripts = ( JArray ) json [ "result" ] [ "scripts" ] ;
176+ if ( json . Type == JTokenType . String )
177+ json = JToken . Parse ( json . ToString ( ) ) ;
178+
179+ JArray scripts = ( JArray ) json [ "result" ] ? [ "scripts" ] ;
138180
139181 if ( scripts == null ) return slugs ;
140182
@@ -145,7 +187,7 @@ private static IEnumerable<string> GetSlugsFromResults(JToken json)
145187
146188 private static ScriptObject CreateScriptFromData ( JToken scriptData )
147189 {
148- GameObject game = new GameObject ( scriptData [ "game" ] . Value < long > ( "gameId" ) ,
190+ GameObject game = new ( scriptData [ "game" ] . Value < long > ( "gameId" ) ,
149191 scriptData [ "game" ] . Value < string > ( "name" ) ,
150192 scriptData [ "game" ] . Value < string > ( "imageUrl" ) ) ;
151193
@@ -221,6 +263,9 @@ public static async Task<List<ScriptObject>> GetFrontPageScriptsAsync(int pageNu
221263 return scripts . ToList ( ) ;
222264 }
223265
266+
267+ public static async Task < List < ScriptObject > > GetScriptsFromPageNumberAsync ( int pageNumber = 1 ) => await GetFrontPageScriptsAsync ( pageNumber ) ;
268+
224269 /// <summary>
225270 /// Retrieves a list of scripts from ScriptBlox based on the provided search query and maximum results.
226271 /// </summary>
@@ -256,7 +301,32 @@ public static async Task<List<ScriptObject>> GetScriptsFromQueryAsync(string sea
256301 /// <returns>A list of Script objects representing the scripts owned by the user.</returns>
257302 public static async Task < List < ScriptObject > > GetScriptsFromUserAsync ( string username )
258303 {
259- JToken jsonReturn = JToken . Parse ( await MiscFunctions . HttpClient . GetStringAsync ( $ "https://scriptblox.com/api/user/scripts/{ username } ?page=1") ) ;
304+ JToken jsonReturn = JToken . Parse ( await MiscFunctions . HttpClient . GetStringAsync ( $ "https://scriptblox.com/api/user/scripts/{ username } ") ) ;
305+
306+ if ( jsonReturn == null )
307+ throw new ScriptBloxException ( "An error has occurred while fetching the json, please submit a bug report." ) ;
308+ if ( jsonReturn [ "message" ] != null )
309+ throw new ScriptBloxException ( jsonReturn . Value < string > ( "message" ) ) ;
310+ if ( jsonReturn [ "result" ] ? [ "scripts" ] == null )
311+ throw new ScriptBloxException ( "Backend error occurred." ) ;
312+
313+ IEnumerable < string > slugsToCheck = GetSlugsFromResults ( jsonReturn . ToString ( ) ) ;
314+
315+ List < Task < ScriptObject > > scriptTasks = slugsToCheck . Select ( GetScriptFromScriptbloxIdAsync ) . ToList ( ) ;
316+
317+ ScriptObject [ ] scripts = await Task . WhenAll ( scriptTasks ) ;
318+
319+ return scripts . ToList ( ) ;
320+ }
321+
322+ /// <summary>
323+ /// Retrieves a list of ScriptObjects based on the specified filter type asynchronously.
324+ /// </summary>
325+ /// <param name="filterType">The type of filter to apply.</param>
326+ /// <returns>A list of ScriptObjects that match the specified filter type.</returns>
327+ public static async Task < List < ScriptObject > > GetScriptsWithFilterAsync ( FilterType filterType )
328+ {
329+ JToken jsonReturn = JToken . Parse ( await MiscFunctions . HttpClient . GetStringAsync ( $@ "https://scriptblox.com/api/script/fetch?page=1&filters[]={ filterType . ToString ( ) . ToLower ( ) } ") ) ;
260330
261331 if ( jsonReturn == null )
262332 throw new ScriptBloxException ( "An error has occurred while fetching the json, please submit a bug report." ) ;
0 commit comments