mirror of
https://github.com/RPCS3/discord-bot.git
synced 2024-12-03 00:16:39 +00:00
parent
ac0b578c51
commit
cf842e6e2b
@ -12,10 +12,15 @@ namespace CompatBot.Database.Providers
|
||||
internal static class ThumbnailProvider
|
||||
{
|
||||
private static readonly HttpClient HttpClient = HttpClientFactory.Create();
|
||||
private static readonly PsnClient.Client PsnClient = new PsnClient.Client();
|
||||
|
||||
public static async Task<string> GetThumbnailUrlAsync(this DiscordClient client, string productCode)
|
||||
{
|
||||
productCode = productCode.ToUpperInvariant();
|
||||
var tmdbInfo = await PsnClient.GetTitleMetaAsync(productCode, Config.Cts.Token).ConfigureAwait(false);
|
||||
if (tmdbInfo?.Icon.Url is string tmdbIconUrl)
|
||||
return tmdbIconUrl;
|
||||
|
||||
using (var db = new ThumbnailDb())
|
||||
{
|
||||
var thumb = await db.Thumbnail.FirstOrDefaultAsync(t => t.ProductCode == productCode.ToUpperInvariant()).ConfigureAwait(false);
|
||||
|
35
PsnClient/POCOs/TitleMeta.cs
Normal file
35
PsnClient/POCOs/TitleMeta.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace PsnClient.POCOs
|
||||
{
|
||||
[XmlRoot("title-info")]
|
||||
public class TitleMeta
|
||||
{
|
||||
[XmlAttribute("rev")]
|
||||
public int Rev { get; set; }
|
||||
[XmlElement("id")]
|
||||
public string Id { get; set; }
|
||||
[XmlElement("console")]
|
||||
public string Console { get; set; }
|
||||
[XmlElement("media-type")]
|
||||
public string MediaType { get; set; }
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
[XmlElement("parental-level")]
|
||||
public int ParentalLevel { get; set; }
|
||||
[XmlElement("icon")]
|
||||
public TitleIcon Icon { get; set; }
|
||||
[XmlElement("resolution")]
|
||||
public string Resolution { get; set; }
|
||||
[XmlElement("sound-format")]
|
||||
public string SoundFormat { get; set; }
|
||||
}
|
||||
|
||||
public class TitleIcon
|
||||
{
|
||||
[XmlAttribute("type")]
|
||||
public string Type { get; set; }
|
||||
[XmlText]
|
||||
public string Url { get; set; }
|
||||
}
|
||||
}
|
@ -270,6 +270,35 @@ namespace PsnClient
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TitleMeta> GetTitleMetaAsync(string productId, CancellationToken cancellationToken)
|
||||
{
|
||||
var id = productId + "_00";
|
||||
var hash = TmdbHasher.GetTitleHash(id);
|
||||
try
|
||||
{
|
||||
using (var message = new HttpRequestMessage(HttpMethod.Get, $"http://tmdb.np.dl.playstation.net/tmdb/{id}_{hash}/{id}.xml"))
|
||||
using (var response = await client.SendAsync(message, cancellationToken).ConfigureAwait(false))
|
||||
try
|
||||
{
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
return null;
|
||||
|
||||
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
|
||||
return await response.Content.ReadAsAsync<TitleMeta>(xmlFormatters, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ConsoleLogger.PrintError(e, response);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ApiConfig.Log.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> GetSessionCookies(string locale, CancellationToken cancellationToken)
|
||||
{
|
||||
var loc = locale.AsLocaleData();
|
||||
|
49
PsnClient/Utils/TmdbHasher.cs
Normal file
49
PsnClient/Utils/TmdbHasher.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace PsnClient.Utils
|
||||
{
|
||||
public static class TmdbHasher
|
||||
{
|
||||
private static readonly byte[] HmacKey = "F5DE66D2680E255B2DF79E74F890EBF349262F618BCAE2A9ACCDEE5156CE8DF2CDF2D48C71173CDC2594465B87405D197CF1AED3B7E9671EEB56CA6753C2E6B0".FromHexString();
|
||||
|
||||
public static string GetTitleHash(string productId)
|
||||
{
|
||||
using (var hmacSha1 = new HMACSHA1(HmacKey))
|
||||
return hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(productId)).ToHexString();
|
||||
}
|
||||
|
||||
public static byte[] FromHexString(this string hexString)
|
||||
{
|
||||
if (hexString == null)
|
||||
return null;
|
||||
|
||||
if (hexString.Length == 0)
|
||||
return new byte[0];
|
||||
|
||||
if (hexString.Length % 2 != 0)
|
||||
throw new ArgumentException("Invalid hex string format: odd number of octets", nameof(hexString));
|
||||
|
||||
var result = new byte[hexString.Length/2];
|
||||
for (int i = 0, j = 0; i < hexString.Length; i += 2, j++)
|
||||
result[j] = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ToHexString(this byte[] array)
|
||||
{
|
||||
if (array == null)
|
||||
return null;
|
||||
|
||||
if (array.Length == 0)
|
||||
return "";
|
||||
|
||||
var result = new StringBuilder(array.Length*2);
|
||||
foreach (var b in array)
|
||||
result.Append(b.ToString("X2"));
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user