-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace RAE.Demo | ||
{ | ||
class Program | ||
{ | ||
static DRAE drae; | ||
|
||
static Func<Task>[] 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RAE\RAE.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Get definitions of a word.</para> | ||
/// <para>Obtiene las definiciones de una palabra.</para> | ||
/// </summary> | ||
/// <param name="wordId"> | ||
/// <para>The id of the word to search.</para> | ||
/// <para>El id de la palabra a buscar.</para> | ||
/// </param> | ||
public async Task<string[]> FetchWordByIdAsync(string wordId) | ||
{ | ||
var response = await _httpClient.GetStringAsync($"{URLBASE}/fetch?id={wordId}"); | ||
|
||
MatchCollection matches = Regex.Matches(response, "<p class=\"(?:j|m)\".*?>.*?</p>"); | ||
|
||
string[] definitions = matches.Cast<Match>() | ||
.Select(m => Regex.Replace(m.Value, "<.*?>", "")) | ||
.ToArray(); | ||
|
||
return definitions; | ||
} | ||
|
||
/// <summary> | ||
/// <para>Get keywords from another.</para> | ||
/// <para>Obtiene palabras claves a partir de otra.</para> | ||
/// </summary> | ||
/// <param name="query"> | ||
/// <para>The base word.</para> | ||
/// <para>La palabra base.</para> | ||
/// </param> | ||
public async Task<List<string>> GetKeysAsync(string query) | ||
{ | ||
string response = await _httpClient.GetStringAsync($"{URLBASE}/keys?q={query}&callback="); | ||
string json = Regex.Match(response, @"\[.*?\]").Value; | ||
|
||
var keys = JsonConvert.DeserializeObject<List<string>>(json); | ||
|
||
return keys; | ||
} | ||
|
||
/// <summary> | ||
/// <para>Get a random word.</para> | ||
/// <para>Obtiene una palabra aleatoria.</para> | ||
/// </summary> | ||
public async Task<Word> 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); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Get the word of the day.</para> | ||
/// <para>Obtiene la palabra del día.</para> | ||
/// </summary> | ||
public async Task<Word> 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<string>("id"); | ||
string content = jobject.Value<string>("header"); | ||
|
||
return new Word(id, content); | ||
} | ||
|
||
/// <summary> | ||
/// <para>Search all entries of a word.</para> | ||
/// <para>Busca todas las entradas de una palabra.</para> | ||
/// </summary> | ||
/// <param name="word"> | ||
/// <para>Word to search.</para> | ||
/// <para>Palabra a buscar.</para> | ||
/// </param> | ||
/// <param name="allGroups"> | ||
/// <para>If true it will take secondary entries.</para> | ||
/// <para>Si es verdadero cogerá entradas secundarias.</para> | ||
/// </param> | ||
public async Task<List<Word>> SearchWordAsync(string word, bool allGroups = true) | ||
{ | ||
string response = await _httpClient.GetStringAsync($"{URLBASE}/search?w={word}"); | ||
JToken jtoken = JToken.Parse(response); | ||
|
||
var words = new List<Word>(); | ||
foreach (var w in jtoken.SelectToken("res")) | ||
{ | ||
int group = w.Value<int>("grp"); | ||
|
||
if (allGroups || group == 0) | ||
{ | ||
string id = w.Value<string>("id"); | ||
string content = w.Value<string>("header"); | ||
|
||
Match contentMatch = Regex.Match(content, @"[A-Za-z/-]+"); | ||
|
||
words.Add(new Word(id, contentMatch.Success ? contentMatch.Value : content)); | ||
} | ||
} | ||
|
||
return words; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |