From 1e0aacf297e3f548e692efbe72de72054f8b1ca8 Mon Sep 17 00:00:00 2001 From: isjsg97 Date: Fri, 14 Aug 2020 18:50:51 +0200 Subject: [PATCH] Init --- RAE.Demo/Program.cs | 74 +++++++++++++++++++++ RAE.Demo/RAE.Demo.csproj | 12 ++++ RAE.NET.sln | 31 +++++++++ RAE/DRAE.cs | 140 +++++++++++++++++++++++++++++++++++++++ RAE/RAE.csproj | 11 +++ RAE/Word.cs | 20 ++++++ 6 files changed, 288 insertions(+) create mode 100644 RAE.Demo/Program.cs create mode 100644 RAE.Demo/RAE.Demo.csproj create mode 100644 RAE.NET.sln create mode 100644 RAE/DRAE.cs create mode 100644 RAE/RAE.csproj create mode 100644 RAE/Word.cs diff --git a/RAE.Demo/Program.cs b/RAE.Demo/Program.cs new file mode 100644 index 0000000..e8096d1 --- /dev/null +++ b/RAE.Demo/Program.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace RAE.Demo +{ + class Program + { + static DRAE drae; + + static Func[] functions = + { + GetKeysAsync, + SearchWordAsync, + WordOfTheDayAsync, + FetchRandomWorldAsync + }; + + static async Task Main() + { + drae = new DRAE(); + + for(int i = 0; i < functions.Length; i++) + { + await functions[i](); + Console.WriteLine(); + Console.WriteLine(new string('-', 20)); + Console.WriteLine(); + } + + Console.WriteLine("Press any key to exit..."); + Console.ReadLine(); + } + + static async Task GetKeysAsync() + { + var query = "hola"; + var keys = await drae.GetKeysAsync(query); + + Console.WriteLine($"GetKeys ({query}): {string.Join(", ", keys)}"); + } + + static async Task SearchWordAsync() + { + var query = "a"; + var words = await drae.SearchWordAsync(query, false); + + Console.WriteLine($"SearchWord ({query}): {string.Join(", ", words.Select(w => $"{w.Content} ({w.Id})"))}"); + } + + static async Task WordOfTheDayAsync() + { + var word = await drae.GetWordOfTheDayAsync(); + + Console.WriteLine($"Word of the day: {word} ({word.Id})"); + } + + static async Task GetRandomWorldAsync() + { + var word = await drae.GetRandomWordAsync(); + + Console.WriteLine($"A random word: {word}"); + } + + static async Task FetchRandomWorldAsync() + { + Word word = await drae.GetRandomWordAsync(); + string[] definitions = await drae.FetchWordByIdAsync(word.Id); + + Console.WriteLine($"Definitions of {word.Content}:"); + Array.ForEach(definitions, Console.WriteLine); + } + } +} diff --git a/RAE.Demo/RAE.Demo.csproj b/RAE.Demo/RAE.Demo.csproj new file mode 100644 index 0000000..8cbc543 --- /dev/null +++ b/RAE.Demo/RAE.Demo.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.0 + + + + + + + diff --git a/RAE.NET.sln b/RAE.NET.sln new file mode 100644 index 0000000..2e93e1b --- /dev/null +++ b/RAE.NET.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RAE", "RAE\RAE.csproj", "{F214F029-5904-44EB-8C99-B84821558369}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RAE.Demo", "RAE.Demo\RAE.Demo.csproj", "{66258F90-04F3-49B6-B8E0-98A82838D6B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F214F029-5904-44EB-8C99-B84821558369}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F214F029-5904-44EB-8C99-B84821558369}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F214F029-5904-44EB-8C99-B84821558369}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F214F029-5904-44EB-8C99-B84821558369}.Release|Any CPU.Build.0 = Release|Any CPU + {66258F90-04F3-49B6-B8E0-98A82838D6B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {66258F90-04F3-49B6-B8E0-98A82838D6B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {66258F90-04F3-49B6-B8E0-98A82838D6B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {66258F90-04F3-49B6-B8E0-98A82838D6B5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {17DC128C-3CC5-430A-B64E-0150FEBB72AD} + EndGlobalSection +EndGlobal diff --git a/RAE/DRAE.cs b/RAE/DRAE.cs new file mode 100644 index 0000000..c081c77 --- /dev/null +++ b/RAE/DRAE.cs @@ -0,0 +1,140 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Linq; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace RAE +{ + /* + Based on information obtained from: + Basado en la información obtenida de: + https://devhub.io/repos/mgp25-RAE-API + */ + public class DRAE + { + private const string URLBASE = "https://dle.rae.es/data"; + private const string TOKEN = "cDY4MkpnaFMzOmFHZlVkQ2lFNDM0"; + + private HttpClient _httpClient; + + public DRAE() + { + _httpClient = new HttpClient(); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", TOKEN); + } + + /// + /// Get definitions of a word. + /// Obtiene las definiciones de una palabra. + /// + /// + /// The id of the word to search. + /// El id de la palabra a buscar. + /// + public async Task FetchWordByIdAsync(string wordId) + { + var response = await _httpClient.GetStringAsync($"{URLBASE}/fetch?id={wordId}"); + + MatchCollection matches = Regex.Matches(response, "

.*?

"); + + string[] definitions = matches.Cast() + .Select(m => Regex.Replace(m.Value, "<.*?>", "")) + .ToArray(); + + return definitions; + } + + /// + /// Get keywords from another. + /// Obtiene palabras claves a partir de otra. + /// + /// + /// The base word. + /// La palabra base. + /// + public async Task> GetKeysAsync(string query) + { + string response = await _httpClient.GetStringAsync($"{URLBASE}/keys?q={query}&callback="); + string json = Regex.Match(response, @"\[.*?\]").Value; + + var keys = JsonConvert.DeserializeObject>(json); + + return keys; + } + + /// + /// Get a random word. + /// Obtiene una palabra aleatoria. + /// + public async Task GetRandomWordAsync() + { + string response = await _httpClient.GetStringAsync($"{URLBASE}/random"); + + string idFormat = "article id=\""; + Match idMatch = Regex.Match(response, $@"{idFormat}\w+"); + string id = Regex.Replace(idMatch.Value, idFormat, ""); + + string contentFormat = "class=\"f\".*?>"; + Match contentMatch = Regex.Match(response, $@"{contentFormat}\w[\,\s\w]*"); + string content = Regex.Replace(contentMatch.Value, contentFormat, ""); + + return new Word(id, content); + } + + /// + /// Get the word of the day. + /// Obtiene la palabra del día. + /// + public async Task GetWordOfTheDayAsync() + { + string response = await _httpClient.GetStringAsync($"{URLBASE}/wotd?callback="); + string json = response.Substring(1, response.Length - 2); + JObject jobject = JObject.Parse(json); + + string id = jobject.Value("id"); + string content = jobject.Value("header"); + + return new Word(id, content); + } + + /// + /// Search all entries of a word. + /// Busca todas las entradas de una palabra. + /// + /// + /// Word to search. + /// Palabra a buscar. + /// + /// + /// If true it will take secondary entries. + /// Si es verdadero cogerá entradas secundarias. + /// + public async Task> SearchWordAsync(string word, bool allGroups = true) + { + string response = await _httpClient.GetStringAsync($"{URLBASE}/search?w={word}"); + JToken jtoken = JToken.Parse(response); + + var words = new List(); + foreach (var w in jtoken.SelectToken("res")) + { + int group = w.Value("grp"); + + if (allGroups || group == 0) + { + string id = w.Value("id"); + string content = w.Value("header"); + + Match contentMatch = Regex.Match(content, @"[A-Za-z/-]+"); + + words.Add(new Word(id, contentMatch.Success ? contentMatch.Value : content)); + } + } + + return words; + } + } +} diff --git a/RAE/RAE.csproj b/RAE/RAE.csproj new file mode 100644 index 0000000..6e86919 --- /dev/null +++ b/RAE/RAE.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/RAE/Word.cs b/RAE/Word.cs new file mode 100644 index 0000000..5b79911 --- /dev/null +++ b/RAE/Word.cs @@ -0,0 +1,20 @@ +namespace RAE +{ + public class Word + { + public string Id { get; private set; } + + public string Content { get; private set; } + + public Word(string id, string content) + { + Id = id; + Content = content; + } + + public override string ToString() + { + return Content; + } + } +}