mirror of
https://github.com/RPCS3/discord-bot.git
synced 2024-11-27 04:00:34 +00:00
Merge pull request #825 from clienthax/irdfix
Rework ird client to use static backup
This commit is contained in:
commit
71e1da4803
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
@ -12,6 +11,7 @@ using CompatApiClient;
|
||||
using CompatApiClient.Compression;
|
||||
using CompatApiClient.Formatters;
|
||||
using CompatApiClient.Utils;
|
||||
using HtmlAgilityPack;
|
||||
using IrdLibraryClient.IrdFormat;
|
||||
using IrdLibraryClient.POCOs;
|
||||
|
||||
@ -19,7 +19,7 @@ namespace IrdLibraryClient
|
||||
{
|
||||
public class IrdClient
|
||||
{
|
||||
public static readonly string BaseUrl = "http://jonnysp.bplaced.net";
|
||||
public static readonly string BaseUrl = "https://ps3.aldostools.org";
|
||||
|
||||
private readonly HttpClient client;
|
||||
private readonly JsonSerializerOptions jsonOptions;
|
||||
@ -37,53 +37,51 @@ namespace IrdLibraryClient
|
||||
}
|
||||
|
||||
public static string GetDownloadLink(string irdFilename) => $"{BaseUrl}/ird/{irdFilename}";
|
||||
public static string GetInfoLink(string irdFilename) => $"{BaseUrl}/info.php?file=ird/{irdFilename}";
|
||||
|
||||
public async Task<SearchResult?> SearchAsync(string query, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var requestUri = new Uri(BaseUrl + "/data.php")
|
||||
.SetQueryParameters(
|
||||
("draw", query.Length.ToString()),
|
||||
|
||||
("columns[0][data]", "id"),
|
||||
("columns[0][name]", ""),
|
||||
("columns[0][searchable]", "true"),
|
||||
("columns[0][orderable]", "true"),
|
||||
("columns[0][search][value]", ""),
|
||||
("columns[0][search][regex]", "false"),
|
||||
|
||||
("columns[1][data]", "title"),
|
||||
("columns[1][name]", ""),
|
||||
("columns[1][searchable]", "true"),
|
||||
("columns[1][orderable]", "true"),
|
||||
("columns[1][search][value]", ""),
|
||||
("columns[1][search][regex]", "false"),
|
||||
|
||||
("order[0][column]", "0"),
|
||||
("order[0][dir]", "asc"),
|
||||
|
||||
("start", "0"),
|
||||
("length", "10"),
|
||||
|
||||
("search[value]", query.Trim(100)),
|
||||
|
||||
("_", DateTime.UtcNow.Ticks.ToString())
|
||||
);
|
||||
var requestUri = new Uri(BaseUrl + "/ird.html");
|
||||
using var getMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
using var response = await client.SendAsync(getMessage, cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
|
||||
var result = await response.Content.ReadFromJsonAsync<SearchResult>(jsonOptions, cancellationToken).ConfigureAwait(false);
|
||||
if (result?.Data?.Count > 0)
|
||||
foreach (var item in result.Data)
|
||||
{
|
||||
item.Filename = GetIrdFilename(item.Filename);
|
||||
item.Title = GetTitle(item.Title);
|
||||
}
|
||||
return result;
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
HtmlDocument doc = new();
|
||||
doc.LoadHtml(result);
|
||||
|
||||
List<string[]> table = doc.DocumentNode
|
||||
.Descendants("tr")
|
||||
.Skip(1)
|
||||
.Where(tr => tr.Elements("td").Count() > 1 && tr.Elements("td").First().InnerText == query)
|
||||
.Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToArray())
|
||||
.ToList();
|
||||
|
||||
SearchResult searchResults = new();
|
||||
searchResults.Data = new();
|
||||
|
||||
foreach (var item in table)
|
||||
{
|
||||
var r = new SearchResultItem();
|
||||
r.Id = item[0];
|
||||
r.Title = item[1];
|
||||
r.GameVersion = item[2];
|
||||
r.UpdateVersion = item[3];
|
||||
r.Size = item[4];
|
||||
r.FileCount = item[5];
|
||||
r.FolderCount = item[6];
|
||||
r.MD5 = item[7];
|
||||
r.IrdName = item[8];
|
||||
|
||||
r.Filename = r.Id + "-" + r.IrdName + ".ird";
|
||||
searchResults?.Data?.Add(r);
|
||||
}
|
||||
|
||||
return searchResults;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -208,20 +206,6 @@ namespace IrdLibraryClient
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetIrdFilename(string? html)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
return null;
|
||||
|
||||
var matches = IrdFilename.Matches(html);
|
||||
if (matches.Count > 0)
|
||||
return matches[0].Groups["filename"].Value;
|
||||
|
||||
ApiConfig.Log.Warn("Couldn't parse IRD filename from " + html);
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
private static string? GetTitle(string? html)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
|
@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Crc32.NET" Version="1.2.0" />
|
||||
<PackageReference Include="DiscUtils.OpticalDisk" Version="0.16.8" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.38" />
|
||||
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.1.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -6,24 +6,20 @@ namespace IrdLibraryClient.POCOs
|
||||
public sealed class SearchResult
|
||||
{
|
||||
public List<SearchResultItem>? Data;
|
||||
public int Draw;
|
||||
|
||||
[JsonPropertyName("recordsFiltered")]
|
||||
public int RecordsFiltered;
|
||||
|
||||
[JsonPropertyName("recordsTotal")]
|
||||
public int RecordsTotal;
|
||||
}
|
||||
|
||||
public sealed class SearchResultItem
|
||||
{
|
||||
public string? Id; // product code
|
||||
public string? AppVersion;
|
||||
public string? Title;
|
||||
public string? GameVersion;
|
||||
public string? UpdateVersion;
|
||||
public string? Date;
|
||||
public string? Title; // <span class="text-success glyphicon glyphicon-ok-sign"></span> MLB® 15 The Show™
|
||||
public string? Filename; // <a class="btn btn-primary btn-xs" href="ird/BCUS00236-7ECC6C2A9C12DABB875342DFF80E9A97.ird">Download</a>\r\n <a class="btn btn-info btn-xs" href="info.php?file=ird/BCUS00236-7ECC6C2A9C12DABB875342DFF80E9A97.ird"><span class="glyphicon glyphicon-info-sign" ></span></a>
|
||||
public string? State; //always 1?
|
||||
public string? Size;
|
||||
public string? FileCount;
|
||||
public string? FolderCount;
|
||||
public string? MD5;
|
||||
public string? IrdName;
|
||||
|
||||
public string? Filename;
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ namespace CompatBot.Utils.ResultFormatters
|
||||
parts = new[] {"", item.Filename};
|
||||
result.AddField(
|
||||
$"[{parts[0]} v{item.GameVersion}] {item.Title?.Sanitize().Trim(EmbedPager.MaxFieldTitleLength)}",
|
||||
$"[⏬ `{parts[1].Sanitize().Trim(200)}`]({IrdClient.GetDownloadLink(item.Filename)}) [ℹ Info]({IrdClient.GetInfoLink(item.Filename)})"
|
||||
$"[⏬ `{parts[1].Sanitize().Trim(200)}`]({IrdClient.GetDownloadLink(item.Filename)})"
|
||||
);
|
||||
}
|
||||
return result;
|
||||
|
Loading…
Reference in New Issue
Block a user