-
Notifications
You must be signed in to change notification settings - Fork 10
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
154 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
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,72 @@ | ||
using Newtonsoft.Json.Linq; | ||
using SonequaBot.Shared.Commands.DTO; | ||
using SonequaBot.Shared.Commands.Interfaces.Responses; | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace SonequaBot.Shared.Commands | ||
{ | ||
public class CommandWikiRandom: CommandBase, IResponseImageCard | ||
{ | ||
private const string ENDPOINT = "https://it.wikipedia.org/api/rest_v1/page/random/summary"; | ||
|
||
protected override string ActivationCommand => "!wikirandom"; | ||
|
||
public ImageCardData GetImageCardEvent(CommandSource source) | ||
{ | ||
ImageCardData result = new ImageCardData() | ||
{ | ||
Title = "Rick Astley", | ||
Description = "Richard Paul Astley (Newton-le-Willows, 6 febbraio 1966) è un cantautore, musicista e conduttore radiofonico britannico.", | ||
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg" | ||
}; | ||
|
||
try | ||
{ | ||
string json = this.GetPage().Result; | ||
|
||
if (!string.IsNullOrEmpty(json)) | ||
{ | ||
JObject wikiPage = JObject.Parse(json); | ||
result.Title = wikiPage["displaytitle"]?.ToString() ?? string.Empty; | ||
result.Description = wikiPage["extract"]?.ToString() ?? string.Empty; | ||
result.ImageUrl = wikiPage["thumbnail"]?["source"]?.ToString() ?? string.Empty; | ||
result.Url = wikiPage["content_urls"]?["desktop"]?["page"]?.ToString() ?? string.Empty; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// external CommandHandler is catching exceptions | ||
throw; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private async Task<string> GetPage() | ||
{ | ||
string json = string.Empty; | ||
|
||
try | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
HttpResponseMessage response = await client.GetAsync(ENDPOINT); | ||
HttpContent responseContent = response.Content; | ||
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) | ||
{ | ||
json = await reader.ReadToEndAsync(); | ||
} | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
throw; | ||
} | ||
|
||
return json; | ||
} | ||
} | ||
} |
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,23 @@ | ||
using System; | ||
|
||
namespace SonequaBot.Shared.Commands.DTO | ||
{ | ||
public class ImageCardData | ||
{ | ||
public ImageCardData() | ||
{ | ||
this.Title = string.Empty; | ||
this.Description = string.Empty; | ||
this.ImageUrl = string.Empty; | ||
this.Url = string.Empty; | ||
} | ||
|
||
public string Title { get; set; } | ||
|
||
public string Description { get; set; } | ||
|
||
public string ImageUrl { get; set; } | ||
|
||
public string Url { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
SonequaBot.Shared/Commands/Interfaces/Responses/IResponseImageCard.cs
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,9 @@ | ||
using SonequaBot.Shared.Commands.DTO; | ||
|
||
namespace SonequaBot.Shared.Commands.Interfaces.Responses | ||
{ | ||
public interface IResponseImageCard | ||
{ | ||
ImageCardData GetImageCardEvent(CommandSource source); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
SonequaBot.Tests/WikiRandomCommand/WikiRandomCommandTest.cs
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,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using SonequaBot.Shared.Commands; | ||
using Xunit; | ||
|
||
namespace SonequaBot.Sentiment.Tests.WikiRandomCommand | ||
{ | ||
public class WikiRandomCommandTest | ||
{ | ||
[Fact] | ||
public void Command_Should_Run() | ||
{ | ||
var source = new CommandSource | ||
{ | ||
Channel = "Test channel", | ||
Message = "!wikirandom", | ||
User = "Defkon1" | ||
}; | ||
|
||
var commandProvider = new CommandWikiRandom(); | ||
Assert.NotNull(commandProvider); | ||
|
||
var cardData = commandProvider.GetImageCardEvent(source); | ||
Assert.NotNull(cardData); | ||
|
||
Assert.NotEqual(string.Empty, cardData.Title); | ||
Assert.NotEqual(string.Empty, cardData.Description); | ||
//// Assert.NotEqual(string.Empty, cardData.ImageUrl); // image can be empty if no thumbnail is provided from Wikipedia | ||
Assert.NotEqual(string.Empty, cardData.Url); | ||
} | ||
} | ||
} |