-
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.
Merge pull request #2 from dominikus1993/rewrite
Add Reddit Support
- Loading branch information
Showing
18 changed files
with
153 additions
and
59 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
4 changes: 3 additions & 1 deletion
4
src/DevNews.Infrastructure.Notifications/Discord/DiscordConfiguration.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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace DevNews.Infrastructure.Notifications.Discord | ||
{ | ||
public class DiscordConfiguration | ||
{ | ||
public string? WebHookUrl { get; init; } | ||
public string WebHookUrl { get; set; } = ""; | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/DevNews.Infrastructure.Parsers/DependencyInjection/Extensions.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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
using System; | ||
using DevNews.Core.Abstractions; | ||
using DevNews.Infrastructure.Parsers.Dotnetomaniak; | ||
using DevNews.Infrastructure.Parsers.HackerNews; | ||
using DevNews.Infrastructure.Parsers.Reddit; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DevNews.Infrastructure.Parsers.DependencyInjection | ||
{ | ||
public static class Extensions | ||
{ | ||
public static IServiceCollection AddParsers(this IServiceCollection services) | ||
public static IServiceCollection AddParsers(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.AddTransient<IArticlesParser, HackerNewsArticlesParser>(); | ||
services.AddTransient<IArticlesParser, DotnetomaniakArticlesParser>(); | ||
services.AddReddit(configuration); | ||
return services; | ||
} | ||
|
||
} | ||
} |
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
16 changes: 8 additions & 8 deletions
16
src/DevNews.Infrastructure.Parsers/HackerNews/HackerNewsArticleParser.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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
namespace DevNews.Infrastructure.Parsers.Reddit | ||
{ | ||
public class RedditConfiguration | ||
internal class RedditConfiguration | ||
{ | ||
public string[]? SubReddits { get; init; } | ||
public string[]? SubReddits { get; set; } | ||
} | ||
} |
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 @@ | ||
using System; | ||
using DevNews.Core.Abstractions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DevNews.Infrastructure.Parsers.Reddit | ||
{ | ||
internal static class Extensions | ||
{ | ||
internal static void AddReddit(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.AddSingleton(configuration.GetSection("Reddit").Get<RedditConfiguration>()); | ||
services.AddHttpClient<SubRedditParser>(client => | ||
{ | ||
client.BaseAddress = new Uri("https://www.reddit.com/"); | ||
}); | ||
services.AddTransient<IArticlesParser, RedditParser>(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,31 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace DevNews.Infrastructure.Parsers.Reddit | ||
{ | ||
public class Model | ||
internal class Data | ||
{ | ||
|
||
[JsonPropertyName("children")] | ||
public PostData[]? Posts { get; init; } | ||
} | ||
|
||
internal record PostData | ||
{ | ||
[JsonPropertyName("data")] | ||
public Post? Post { get; init; } | ||
} | ||
|
||
public class Post | ||
{ | ||
[JsonPropertyName("title")] | ||
public string? Title { get; init; } | ||
[JsonPropertyName("url")] | ||
public string? Url { get; init; } | ||
[JsonPropertyName("selftext")] | ||
public string? Content { get; set; } | ||
} | ||
internal class Subreddit | ||
{ | ||
[JsonPropertyName("data")] | ||
public Data? Data { get; init; } | ||
}; | ||
} |
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
34 changes: 24 additions & 10 deletions
34
src/DevNews.Infrastructure.Parsers/Reddit/SubRedditParser.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 |
---|---|---|
@@ -1,23 +1,37 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Threading.Tasks; | ||
using DevNews.Core.Model; | ||
using HtmlAgilityPack; | ||
using LanguageExt; | ||
using static LanguageExt.Prelude; | ||
|
||
namespace DevNews.Infrastructure.Parsers.Reddit | ||
{ | ||
public class SubRedditParser | ||
internal sealed class SubRedditParser | ||
{ | ||
private const string PostToDownload = "10"; | ||
private readonly HttpClient _client; | ||
|
||
public Task<Article[]> Parse(string name) | ||
public SubRedditParser(HttpClient client) | ||
{ | ||
var url = $"https://www.reddit.com/r/{name}"; | ||
var html = new HtmlWeb(); | ||
var document = await html.LoadFromWebAsync(url); | ||
|
||
var nodes = document.DocumentNode.SelectNodes("//*[@class=\"storylink\"]") | ||
.Select(static node => new Article(node.InnerText, node.GetAttributeValue("href", null))); | ||
|
||
_client = client; | ||
} | ||
|
||
public async Task<Option<Article[]>> Parse(string name) | ||
{ | ||
var url = $"r/{name}/top/.json?limit={PostToDownload}"; | ||
var result = await _client.GetFromJsonAsync<Subreddit>(url); | ||
if (result?.Data?.Posts is null) | ||
{ | ||
return None; | ||
} | ||
|
||
return result.Data.Posts | ||
.Where(x => x.Post is not null) | ||
.Select(x => new Article(x.Post.Title, x.Post.Content, x.Post.Url)) | ||
.ToArray(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
HtmlAgilityPack | ||
Microsoft.Extensions.DependencyInjection.Abstractions | ||
Microsoft.Extensions.DependencyInjection.Abstractions | ||
Microsoft.Extensions.Configuration.Abstractions | ||
Microsoft.Extensions.Configuration.Binder | ||
Microsoft.Extensions.Http |
Oops, something went wrong.