Skip to content

Commit 879f5fd

Browse files
committed
Updating data to make the 404 entry less problematic.
1 parent c4b9c86 commit 879f5fd

5 files changed

Lines changed: 2705 additions & 1 deletion

File tree

ExampleDataApis.Tests/ExampleDataApis.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@
1717
<PackageReference Include="ServiceStack.Kestrel" Version="6.*" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<None Remove="xkcd-metadata.jsonl" />
22+
<Content Include="xkcd-metadata.jsonl">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
25+
</ItemGroup>
26+
2027
</Project>

ExampleDataApis.Tests/UnitTest.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.IO;
5+
using System.Linq;
16
using NUnit.Framework;
27
using ServiceStack;
38
using ServiceStack.Testing;
49
using ExampleDataApis.ServiceInterface;
510
using ExampleDataApis.ServiceModel;
11+
using ServiceStack.Text;
612

713
namespace ExampleDataApis.Tests;
814

@@ -28,4 +34,64 @@ public void Can_call_MyServices()
2834

2935
Assert.That(response.Result, Is.EqualTo("Hello, World!"));
3036
}
37+
38+
[Test]
39+
public void Extract_Xkcd_Image_Dimensions()
40+
{
41+
var allLines = "xkcd-metadata.jsonl"
42+
.ReadAllText().Split("\n");
43+
using var jsonConfig = JsConfig.With(new Config
44+
{
45+
TextCase = TextCase.SnakeCase
46+
});
47+
48+
var comics = allLines
49+
.Where(x => !x.IsNullOrEmpty())
50+
.Select(JsonSerializer.DeserializeFromString<XkcdComic>)
51+
.ToList();
52+
53+
var results = new List<XkcdComicDimensions>();
54+
foreach (var comic in comics)
55+
{
56+
try
57+
{
58+
var image = comic.ImageUrl.GetBytesFromUrl();
59+
using var imageStream = new MemoryStream(image);
60+
var pngImage = Image.FromStream(imageStream);
61+
// Get the dimensions of the PNG image
62+
int width = pngImage.Width;
63+
int height = pngImage.Height;
64+
results.Add(new XkcdComicDimensions
65+
{
66+
Id = comic.Id,
67+
Width = width,
68+
Height = height
69+
});
70+
// Dispose the image to release resources
71+
pngImage.Dispose();
72+
}
73+
catch (Exception e)
74+
{
75+
Console.WriteLine(e);
76+
}
77+
finally
78+
{
79+
results.Add(new XkcdComicDimensions
80+
{
81+
Id = comic.Id,
82+
Width = 0,
83+
Height = 0
84+
});
85+
}
86+
}
87+
88+
File.WriteAllText("xkcd-dimensions.json", JsonSerializer.SerializeToString(results));
89+
}
90+
}
91+
92+
public class XkcdComicDimensions
93+
{
94+
public int Id { get; set; }
95+
public int Width { get; set; }
96+
public int Height { get; set; }
3197
}

0 commit comments

Comments
 (0)