Skip to content

Commit

Permalink
Implement selcuksports command
Browse files Browse the repository at this point in the history
Also move HttpClient to have a single shared static instance
  • Loading branch information
cankaratepe23 committed Aug 6, 2024
1 parent a1959b5 commit b11fa82
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 9 deletions.
3 changes: 0 additions & 3 deletions MedicBot/Commands/BaseCommands.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity.Extensions;
using MedicBot.Manager;
using MedicBot.Utils;
using MongoDB.Bson;
using MongoDB.Driver;
using Serilog;
Expand Down
16 changes: 16 additions & 0 deletions MedicBot/Commands/MiscCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Xml;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using HtmlAgilityPack;

namespace MedicBot.Commands;

public class MiscCommands : BaseCommandModule
{
[Command("selçuk")]
[Aliases("selcuk", "selcuksports")]
public async Task SelcukSport(CommandContext ctx)
{
await ctx.RespondAsync(await MiscManager.GetSelcukSportsUrlAsync());
}
}
16 changes: 16 additions & 0 deletions MedicBot/Controller/MiscController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MedicBot;

[ApiController]
public class MiscController : ControllerBase
{
[HttpGet("selcuksports")]
[Authorize(Policy = "CombinedPolicy")]
public async Task<IActionResult> SelcukSports()
{
var url = await MiscManager.GetSelcukSportsUrlAsync();
return Redirect(url);
}
}
3 changes: 1 addition & 2 deletions MedicBot/Manager/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public static async Task AddAsync(string audioName, ulong userId, string url)
{
Log.Information("Downloading: {DownloadUrl}", url);
Log.Information("Downloading file to {FilePath}", filePath);
using var client = new HttpClient();
await using var stream = await client.GetStreamAsync(url);
await using var stream = await Program.Client.GetStreamAsync(url);
await using var fileStream = File.OpenWrite(filePath);
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
Expand Down
3 changes: 1 addition & 2 deletions MedicBot/Manager/ImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public static async Task AddAsync(string imageName, ulong userId, string url)

{
Log.Information("Downloading file to {FilePath}", filePath);
using var client = new HttpClient();
await using var stream = await client.GetStreamAsync(url);
await using var stream = await Program.Client.GetStreamAsync(url);
await using var fileStream = File.OpenWrite(filePath);
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
Expand Down
3 changes: 1 addition & 2 deletions MedicBot/Manager/ImportExportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public static async Task<int> Import(string url)
var numberOfEntriesAdded = 0;
var jsonString = "";
{
using var client = new HttpClient();
await using var stream = await client.GetStreamAsync(url);
await using var stream = await Program.Client.GetStreamAsync(url);
using var streamReader = new StreamReader(stream);
jsonString = await streamReader.ReadToEndAsync();
}
Expand Down
19 changes: 19 additions & 0 deletions MedicBot/Manager/MiscManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HtmlAgilityPack;

namespace MedicBot;

public class MiscManager
{
public static async Task<string> GetSelcukSportsUrlAsync()
{
var selcukUrl = "https://selcuksportshd78.biz";
await using var stream = await Program.Client.GetStreamAsync(selcukUrl);
using var streamReader = new StreamReader(stream);
var responseString = await streamReader.ReadToEndAsync();
var doc = new HtmlDocument();
doc.LoadHtml(responseString);
var root = doc.DocumentNode ?? throw new FormatException("Could not find root node, probably error in HTML parsing.");
var selcukStreamUrl = root.SelectSingleNode("//div/div/a[1]").Attributes["href"].Value;
return selcukStreamUrl;
}
}
1 change: 1 addition & 0 deletions MedicBot/MedicBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="DSharpPlus.Lavalink" Version="4.4.6" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.6" />
<PackageReference Include="Fastenshtein" Version="1.0.0.8" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.62" />
<PackageReference Include="Magick.NET-Q8-x64" Version="13.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.17" />
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
Expand Down
2 changes: 2 additions & 0 deletions MedicBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace MedicBot;

internal static class Program
{
public static readonly HttpClient Client = new();
private static readonly CancellationTokenSource CancellationTokenSource = new();
private static DiscordClient? _client;

Expand Down Expand Up @@ -287,6 +288,7 @@ private static async Task ConfigureAsync()
commands.RegisterCommands<ImageCommands>();
commands.RegisterCommands<SettingsCommands>();
commands.RegisterCommands<ImportExportCommands>();
commands.RegisterCommands<MiscCommands>();
commands.RegisterConverter(new StringLowercaseConverter());

// Interactivity Init
Expand Down

0 comments on commit b11fa82

Please sign in to comment.