Skip to content

Commit 361dcc2

Browse files
committed
Bug Fixes | Check Description
- 5.0.0 * Added an internal convert to WebP Method, 'ConvertWebPToBitmapInternal' * Changed internl C# Language to 'latest' * Updated 'NetCore.Platforms' -> 7.0.4 * Updated 'Fody' -> 6.8.0 * Fixed bug with 'GetSlugsFromResults' throwing a 'Cannot access child value on Newtonsoft.Json.Linq.JValue.' + Added UserAgent to HttpClient to help usage control + Created a new script Method 'GetScriptsWithFilter, GetScriptsWithFilterAsync' + Created new Enum for Filters 'ScriptsMethod.FilterType' to be used with the new methods.
1 parent 2b84111 commit 361dcc2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+282
-27
lines changed

.vs/ScriptBloxAPI/FileContentIndex/read.lock

Whitespace-only changes.

.vs/ScriptBloxAPI/v17/.suo

-43 KB
Binary file not shown.

Backend Functions/MiscFunctions.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
using System;
22
using System.Globalization;
33
using System.Net.Http;
4+
using System.Reflection;
45

56
namespace ScriptBloxAPI.Backend_Functions
67
{
78
internal class MiscFunctions
89
{
9-
internal static HttpClient HttpClient = new HttpClient();
10-
internal static DateTime ConvertStringToDateTime(string dateString, string timeFormat = "MM/dd/yyyy HH:mm:ss")
10+
11+
internal static HttpClient InternalClient;
12+
internal static HttpClient HttpClient
13+
{
14+
get
15+
{
16+
if (InternalClient != null) return InternalClient;
17+
18+
InternalClient = new HttpClient();
19+
InternalClient.DefaultRequestHeaders.UserAgent.ParseAdd(@$"iScriptBloxAPI/{Assembly.GetEntryAssembly()?.GetName().Version} (Windows 10/11; .NET Framework 4.8; C# <Latest>)");
20+
21+
return InternalClient;
22+
}
23+
}
24+
internal static DateTime ConvertStringToDateTime(string dateString, string timeFormat = @"MM/dd/yyyy HH:mm:ss")
1125
{
1226
try
1327
{

Methods/Commenting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static BoolStatus DeleteComment(string authorization, string commentId)
6969
/// </exception>
7070
public static List<CommentObject> GetCommentsFromScript(ScriptObject script)
7171
{
72-
List<CommentObject> comments = new List<CommentObject>();
72+
List<CommentObject> comments = new();
7373

7474
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetStringAsync($"https://scriptblox.com/api/comment/{script.Id}?page=1&max=999").Result);
7575

@@ -120,7 +120,7 @@ internal static HttpResponseMessage SendRequest(string url, string authorization
120120
/// </exception>
121121
public static async Task<List<CommentObject>> GetCommentsFromScriptAsync(ScriptObject script)
122122
{
123-
List<CommentObject> comments = new List<CommentObject>();
123+
List<CommentObject> comments = new();
124124

125125
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetStringAsync($"https://scriptblox.com/api/comment/{script.Id}?page=1&max=999"));
126126

Methods/Converter.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public static async Task<Bitmap> ConvertWebPToBitmap(string fileUrl)
1616
{
1717
byte[] imageBytes = await MiscFunctions.HttpClient.GetByteArrayAsync(fileUrl);
1818

19-
using StringContent imageJsonFormat = new StringContent(
19+
using StringContent imageJsonFormat = new(
2020
$"{{\"image\": \"{Convert.ToBase64String(imageBytes)}\"}}", Encoding.UTF8, "application/json");
2121

2222
HttpResponseMessage response =
2323
await MiscFunctions.HttpClient.PostAsync("https://scriptblox.com/api/user/image", imageJsonFormat);
2424

2525
Bitmap finalBitmap =
26-
new Bitmap(new MemoryStream(Convert.FromBase64String(response.Content.ReadAsStringAsync().Result)));
26+
new(new MemoryStream(Convert.FromBase64String(response.Content.ReadAsStringAsync().Result)));
2727

2828
return response.IsSuccessStatusCode
2929
? finalBitmap
@@ -35,5 +35,26 @@ public static async Task<Bitmap> ConvertWebPToBitmap(string fileUrl)
3535
throw new ScriptBloxException($"An error occurred while trying to handle conversion: {ex}");
3636
}
3737
}
38+
39+
public static async Task<Bitmap> ConvertWebPToBitmapInternal(string webpUrl)
40+
{
41+
try
42+
{
43+
using HttpResponseMessage response = await MiscFunctions.HttpClient.GetAsync(webpUrl);
44+
response.EnsureSuccessStatusCode();
45+
46+
using Stream contentStream = await response.Content.ReadAsStreamAsync();
47+
using MemoryStream webPStream = new();
48+
49+
await contentStream.CopyToAsync(webPStream);
50+
51+
using Bitmap bitmap = new(webPStream);
52+
return bitmap;
53+
}
54+
catch (Exception ex)
55+
{
56+
throw new ScriptBloxException($"An error occurred while trying to handle conversion: {ex}");
57+
}
58+
}
3859
}
3960
}

Methods/Notifications.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ScriptBloxAPI.Methods
1010
{
1111
public class Notifications
1212
{
13-
internal static Dictionary<string, NotificationType> ActionToType = new Dictionary<string, NotificationType>
13+
internal static Dictionary<string, NotificationType> ActionToType = new()
1414
{
1515
{ "followed", NotificationType.Followed },
1616
{ "commented", NotificationType.CommentAddedToScript },
@@ -41,7 +41,7 @@ public static List<NotificationObject> GetAllNotifications(string authorization)
4141

4242
if (jsonNotifications == null) throw new ScriptBloxException("Backend error occurred.");
4343

44-
List<NotificationObject> notifications = new List<NotificationObject>(jsonNotifications.Count);
44+
List<NotificationObject> notifications = new(jsonNotifications.Count);
4545

4646
notifications.AddRange(jsonNotifications.Select(ParseNotification));
4747

@@ -135,7 +135,7 @@ public static async Task<List<NotificationObject>> GetAllNotificationsAsync(stri
135135
if (jsonNotifications == null)
136136
throw new ScriptBloxException("Backend error occurred.");
137137

138-
List<NotificationObject> notifications = new List<NotificationObject>(jsonNotifications.Count);
138+
List<NotificationObject> notifications = new(jsonNotifications.Count);
139139

140140
notifications.AddRange((List<NotificationObject>)jsonNotifications.Select(ParseNotificationAsync));
141141

Methods/Scripts.cs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
using Newtonsoft.Json.Linq;
1+
using System;
2+
using Newtonsoft.Json.Linq;
23
using ScriptBloxAPI.DataTypes;
34
using System.Collections.Generic;
5+
using System.Diagnostics;
46
using System.Linq;
57
using ScriptBloxAPI.Backend_Functions;
68
using System.Threading.Tasks;
9+
// ReSharper disable UnusedMember.Global
10+
#pragma warning disable IDE0270
711

812
namespace 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.");

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("4.0.5.0")]
35-
[assembly: AssemblyFileVersion("4.0.5.0")]
34+
[assembly: AssemblyVersion("5.0.0.0")]
35+
[assembly: AssemblyFileVersion("5.0.0.0")]

ScriptBloxAPI.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
1515
<Deterministic>true</Deterministic>
16+
<LangVersion>latest</LangVersion>
1617
<NuGetPackageImportStamp>
1718
</NuGetPackageImportStamp>
1819
</PropertyGroup>
@@ -24,7 +25,7 @@
2425
<DefineConstants>DEBUG;TRACE</DefineConstants>
2526
<ErrorReport>prompt</ErrorReport>
2627
<WarningLevel>4</WarningLevel>
27-
<LangVersion>8</LangVersion>
28+
<LangVersion>latest</LangVersion>
2829
</PropertyGroup>
2930
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3031
<DebugType>pdbonly</DebugType>
@@ -33,7 +34,7 @@
3334
<DefineConstants>TRACE</DefineConstants>
3435
<ErrorReport>prompt</ErrorReport>
3536
<WarningLevel>4</WarningLevel>
36-
<LangVersion>8</LangVersion>
37+
<LangVersion>latest</LangVersion>
3738
</PropertyGroup>
3839
<ItemGroup>
3940
<Reference Include="Costura, Version=5.7.0.0, Culture=neutral, processorArchitecture=MSIL">
@@ -212,19 +213,19 @@
212213
<PropertyGroup>
213214
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
214215
</PropertyGroup>
216+
<Error Condition="!Exists('packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets'))" />
215217
<Error Condition="!Exists('packages\Costura.Fody.5.7.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.5.7.0\build\Costura.Fody.props'))" />
216218
<Error Condition="!Exists('packages\Costura.Fody.5.7.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.5.7.0\build\Costura.Fody.targets'))" />
217-
<Error Condition="!Exists('packages\Fody.6.7.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.7.0\build\Fody.targets'))" />
218-
<Error Condition="!Exists('packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets'))" />
219+
<Error Condition="!Exists('packages\Fody.6.8.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.8.0\build\Fody.targets'))" />
219220
</Target>
220-
<Import Project="packages\Costura.Fody.5.7.0\build\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.5.7.0\build\Costura.Fody.targets')" />
221221
<PropertyGroup>
222222
<PostBuildEvent>IF "$(ConfigurationName)" == "Release" (
223223
CD "$(ProjectDir)"
224224
nuget pack -Properties Configuration=Release
225225
)
226226
</PostBuildEvent>
227227
</PropertyGroup>
228-
<Import Project="packages\Fody.6.7.0\build\Fody.targets" Condition="Exists('packages\Fody.6.7.0\build\Fody.targets')" />
229228
<Import Project="packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" />
229+
<Import Project="packages\Costura.Fody.5.7.0\build\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.5.7.0\build\Costura.Fody.targets')" />
230+
<Import Project="packages\Fody.6.8.0\build\Fody.targets" Condition="Exists('packages\Fody.6.8.0\build\Fody.targets')" />
230231
</Project>

ScriptBloxAPI.nuspec

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
<projectUrl>https://api.irisapp.ca/Docs/ScriptBloxAPI/</projectUrl>
1313
<description>$description$</description>
1414
<releaseNotes>
15-
- 4.0.5
16-
- Added method to convert the given image format to a PNG format "ConvertWebPToBitmap(string fileUrl)"
15+
- 5.0.0
16+
* Added an internal convert to WebP Method, 'ConvertWebPToBitmapInternal'
17+
* Changed internl C# Language to 'latest'
18+
* Updated 'NetCore.Platforms' -> 7.0.4
19+
* Updated 'Fody' -> 6.8.0
20+
* Fixed bug with 'GetSlugsFromResults' throwing a 'Cannot access child value on Newtonsoft.Json.Linq.JValue.'
21+
22+
+ Added UserAgent to HttpClient to help usage control
23+
+ Created a new script Method 'GetScriptsWithFilter, GetScriptsWithFilterAsync'
24+
+ Created new Enum for Filters 'ScriptsMethod.FilterType' to be used with the new methods.
1725
</releaseNotes>
1826
<copyright>$copyright$</copyright>
1927
<tags>ScriptBlox ScriptBloxAPI</tags>

0 commit comments

Comments
 (0)