From 7f1ec8264db82a4127022258246d83295522a4d2 Mon Sep 17 00:00:00 2001 From: exsersewo Date: Tue, 26 Feb 2019 20:12:08 +0000 Subject: [PATCH] Add Meme Stuff, Add Documentation, Ver Bump & Test --- SysEx.Net.Tests/Program.cs | 18 +++-- SysEx.Net.Tests/SysEx.Net.Tests.csproj | 8 ++ SysEx.Net.Tests/packages.config | 5 ++ SysEx.Net/Client.cs | 100 ++++++++++++++++++++++++- SysEx.Net/Models/Animal.cs | 2 +- SysEx.Net/Models/MemeEndpoints.cs | 8 ++ SysEx.Net/Models/MemeResponse.cs | 14 ++++ SysEx.Net/Models/PickupLine.cs | 2 +- SysEx.Net/Models/Roast.cs | 2 +- SysEx.Net/Models/WeebGif.cs | 5 +- SysEx.Net/SysEx.Net.csproj | 5 +- SysEx.Net/WebRequest.cs | 50 ++++++++++++- 12 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 SysEx.Net.Tests/packages.config create mode 100644 SysEx.Net/Models/MemeEndpoints.cs create mode 100644 SysEx.Net/Models/MemeResponse.cs diff --git a/SysEx.Net.Tests/Program.cs b/SysEx.Net.Tests/Program.cs index 1b4fc37..945479b 100644 --- a/SysEx.Net.Tests/Program.cs +++ b/SysEx.Net.Tests/Program.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Drawing; +using System.IO; using System.Threading.Tasks; -using SysEx.Net; namespace SysEx.Net.Tests { @@ -17,9 +15,17 @@ namespace SysEx.Net.Tests { var client = new SysExClient(); - var resp = await client.GetWeebActionGifAsync(GifType.Slap); + var resp = await client.GetMemeImageAsync("22million", "https://cdn.discordapp.com/avatars/270047199184945152/e6a23a1c72703edb5e178411bba9190c.png"); - Console.WriteLine(resp); + if(resp is Stream) + { + Console.Write("Got Successfully"); + } + + var re = resp as MemoryStream; + + Image img = Image.FromStream(re); + img.Save(AppContext.BaseDirectory + "image.jpg"); Console.ReadLine(); } diff --git a/SysEx.Net.Tests/SysEx.Net.Tests.csproj b/SysEx.Net.Tests/SysEx.Net.Tests.csproj index 747d077..1c7c0b4 100644 --- a/SysEx.Net.Tests/SysEx.Net.Tests.csproj +++ b/SysEx.Net.Tests/SysEx.Net.Tests.csproj @@ -32,8 +32,15 @@ 4 + + ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + + + + ..\packages\System.Drawing.Common.4.6.0-preview.19073.11\lib\net461\System.Drawing.Common.dll + @@ -47,6 +54,7 @@ + diff --git a/SysEx.Net.Tests/packages.config b/SysEx.Net.Tests/packages.config new file mode 100644 index 0000000..2e3b2e2 --- /dev/null +++ b/SysEx.Net.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/SysEx.Net/Client.cs b/SysEx.Net/Client.cs index bfad523..f6f8821 100644 --- a/SysEx.Net/Client.cs +++ b/SysEx.Net/Client.cs @@ -1,8 +1,11 @@ -using System; -using System.Threading.Tasks; -using Newtonsoft.Json; -using System.Collections.Generic; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using SysEx.Net.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; namespace SysEx.Net { @@ -15,24 +18,45 @@ namespace SysEx.Net random = new Random(); } + /// + /// Gets a Llama image + /// public async Task GetLlamaAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/llama.json")); + /// + /// Gets a Seal image + /// public async Task GetSealAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/seal.json")); + /// + /// Gets a Duck image + /// public async Task GetDuckAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/duck.json")); + /// + /// Gets a Squirrel image + /// public async Task GetSquirrelAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/squirrel.json")); + /// + /// Gets a Lizard image + /// public async Task GetLizardAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/lizard.json")); + /// + /// Gets a morphed animal image + /// public async Task GetMorphAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/morphs.json")); + /// + /// Gets a Snake image + /// public async Task GetSnakeAsync() => await GetAnimalAsync(new Uri("https://api.systemexit.co.uk/v1/snake.json")); @@ -45,6 +69,9 @@ namespace SysEx.Net return animal; } + /// + /// Get a roast + /// public async Task GetRoastAsync() { var resp = await WebRequest.ReturnStringAsync(new Uri("https://api.systemexit.co.uk/v1/roasts.json")); @@ -53,6 +80,9 @@ namespace SysEx.Net return items[random.Next(0, items.Count)].Roast; } + /// + /// Gets a terrible, terrible Dad joke + /// public async Task GetDadJokeAsync() { var resp = await WebRequest.ReturnStringAsync(new Uri("https://api.systemexit.co.uk/v1/dadjokes.json")); @@ -61,6 +91,9 @@ namespace SysEx.Net return items[random.Next(0, items.Count)]; } + /// + /// Gets a terrible, terrible pickup line + /// public async Task GetPickupLineAsync() { var resp = await WebRequest.ReturnStringAsync(new Uri("https://api.systemexit.co.uk/v1/pickuplines.json")); @@ -69,18 +102,74 @@ namespace SysEx.Net return items[random.Next(0, items.Count)]; } + /// + /// Gets A Weeb Action Gif + /// + /// Gif Action Type + /// Url of image public async Task GetWeebActionGifAsync(GifType type) { var resp = await WebRequest.GetRedirectUriAsync(new Uri("https://api.systemexit.co.uk/actions/?action=" + type.ToString().ToLowerInvariant())); return resp.OriginalString; } + /// + /// Gets A Weeb Reaction Gif + /// + /// Url of image public async Task GetWeebReactionGifAsync() { var resp = await WebRequest.ReturnStringAsync(new Uri("https://api.systemexit.co.uk/reactions/")); return resp; } + /// + /// Gets a meme image based on given input + /// + /// Template to use + /// Source images to use with template + /// Either Object. MemeResponse on empty input or failure, or MemoryStream on success + public async Task GetMemeImageAsync(string template = null, params string[] images) + { + var endpoints = JsonConvert.DeserializeObject(await WebRequest.ReturnStringAsync(new Uri("https://api.skuldbot.uk/fun/meme/?endpoints"))); + if (template == null && (images.Length <= 0 || images == null)) + { + return endpoints; + } + if(endpoints.Endpoints.Exists(x=>x.Name.ToLowerInvariant() == template.ToLowerInvariant())) + { + var endpoint = endpoints.Endpoints.FirstOrDefault(z => z.Name.ToLowerInvariant() == template.ToLowerInvariant()); + string queryString = ""; + int x = 1; + foreach(var image in images) + { + if(image == images.Last()) + { + queryString += $"source{x}={image}"; + } + else + { + queryString += $"source{x}={image}&"; + } + x++; + } + + var resp = await WebRequest.GetStreamAsync(new Uri($"https://api.skuldbot.uk/fun/meme/{template}/?{queryString}")); + if(resp != null) + { + return resp; + } + } + return new MemeResponse + { + Successful = false, + Endpoints = endpoints.Endpoints + }; + } + + /// + /// Gets a NSFW image of a "Kitsune" + /// public async Task GetLewdKitsuneAsync() { var rawresp = await WebRequest.ReturnStringAsync(new Uri("https://kitsu.systemexit.co.uk/lewd")); @@ -89,6 +178,9 @@ namespace SysEx.Net if (img == null) return null; return img; } + /// + /// Gets a SFW* image of a "Kitsune" (*potentially) + /// public async Task GetKitsuneAsync() { var rawresp = await WebRequest.ReturnStringAsync(new Uri("https://kitsu.systemexit.co.uk/kitsune")); diff --git a/SysEx.Net/Models/Animal.cs b/SysEx.Net/Models/Animal.cs index 6dfa5ff..ec6519e 100644 --- a/SysEx.Net/Models/Animal.cs +++ b/SysEx.Net/Models/Animal.cs @@ -1,6 +1,6 @@ using Newtonsoft.Json; -namespace SysEx.Net +namespace SysEx.Net.Models { public class Animal { diff --git a/SysEx.Net/Models/MemeEndpoints.cs b/SysEx.Net/Models/MemeEndpoints.cs new file mode 100644 index 0000000..0b53f41 --- /dev/null +++ b/SysEx.Net/Models/MemeEndpoints.cs @@ -0,0 +1,8 @@ +namespace SysEx.Net.Models +{ + public struct MemeEndpoints + { + public string Name; + public int RequiredSources; + } +} diff --git a/SysEx.Net/Models/MemeResponse.cs b/SysEx.Net/Models/MemeResponse.cs new file mode 100644 index 0000000..81e69e7 --- /dev/null +++ b/SysEx.Net/Models/MemeResponse.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace SysEx.Net.Models +{ + public struct MemeResponse + { + public bool Successful; + public string Example; + + [JsonProperty(PropertyName = "availabletemplates")] + public List Endpoints; + } +} diff --git a/SysEx.Net/Models/PickupLine.cs b/SysEx.Net/Models/PickupLine.cs index bf8abf2..acf9a7b 100644 --- a/SysEx.Net/Models/PickupLine.cs +++ b/SysEx.Net/Models/PickupLine.cs @@ -1,4 +1,4 @@ -namespace SysEx.Net +namespace SysEx.Net.Models { public class Joke { diff --git a/SysEx.Net/Models/Roast.cs b/SysEx.Net/Models/Roast.cs index e9d024e..8c3b0be 100644 --- a/SysEx.Net/Models/Roast.cs +++ b/SysEx.Net/Models/Roast.cs @@ -1,4 +1,4 @@ -namespace SysEx.Net +namespace SysEx.Net.Models { public class Roasts { diff --git a/SysEx.Net/Models/WeebGif.cs b/SysEx.Net/Models/WeebGif.cs index 9871d43..38ae40c 100644 --- a/SysEx.Net/Models/WeebGif.cs +++ b/SysEx.Net/Models/WeebGif.cs @@ -1,7 +1,6 @@ using Newtonsoft.Json; -using System; -namespace SysEx.Net +namespace SysEx.Net.Models { public class WeebGif { @@ -9,7 +8,7 @@ namespace SysEx.Net public string URL { get; set; } [JsonProperty(PropertyName = "type")] - public GifType GifType { get; set; } + public GifType GifType { get; set; } } public enum GifType { diff --git a/SysEx.Net/SysEx.Net.csproj b/SysEx.Net/SysEx.Net.csproj index b478f71..117781d 100644 --- a/SysEx.Net/SysEx.Net.csproj +++ b/SysEx.Net/SysEx.Net.csproj @@ -15,11 +15,14 @@ en-GB Library https://github.com/exsersewo/SysEx.Net/blob/master/LICENSE - 1.0.5 + 1.0.6 true + DEBUG;TRACE + full + true diff --git a/SysEx.Net/WebRequest.cs b/SysEx.Net/WebRequest.cs index 9e6f961..903f13a 100644 --- a/SysEx.Net/WebRequest.cs +++ b/SysEx.Net/WebRequest.cs @@ -1,4 +1,5 @@ -using System; +using SysEx.Net.Models; +using System; using System.Net; using System.Threading.Tasks; using System.IO; @@ -59,5 +60,52 @@ namespace SysEx.Net return null; } } + public static async Task GetStreamAsync(Uri url) + { + try + { + var client = CreateWebRequest(url); + + var resp = (HttpWebResponse)(await client.GetResponseAsync()); + if (resp.StatusCode == HttpStatusCode.OK) + { + var reader = resp.GetResponseStream(); + long contLength = resp.ContentLength; + + var stream = resp.GetResponseStream(); + + byte[] outData; + using (var tempStream = new MemoryStream()) + { + byte[] buffer = new byte[128]; + while (true) + { + int read = stream.Read(buffer, 0, buffer.Length); + if (read <= 0) + { + outData = tempStream.ToArray(); + break; + } + tempStream.Write(buffer, 0, read); + } + } + + resp.Dispose(); + client.Abort(); + + return new MemoryStream(outData); + } + else + { + resp.Dispose(); + client.Abort(); + return null; + } + } + catch + { + return null; + } + } } }