91 lines
2.3 KiB
C#
91 lines
2.3 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
using Godot;
|
||
using Learn.Config;
|
||
using Learn.Models;
|
||
using Learn.Utils;
|
||
using TMDbLib.Client;
|
||
using TMDbLib.Objects.Search;
|
||
|
||
namespace Learn.Parsers;
|
||
|
||
public class TMDBParser(Configs configs) : ItemParser
|
||
{
|
||
private TMDBParserConfig config => configs.Get<TMDBParserConfig>();
|
||
|
||
private readonly Dictionary<string, SearchTv> _cache = new();
|
||
|
||
private TMDbClient _client;
|
||
|
||
private async Task<SearchTv> QueryTMDB(string title)
|
||
{
|
||
if (string.IsNullOrEmpty(title)) return null;
|
||
|
||
if (_cache.TryGetValue(title, out var result))
|
||
{
|
||
return result;
|
||
}
|
||
|
||
var results = (await GetTMDbClient().SearchTvShowAsync(title, language: "zh-CN")).Results;
|
||
result = results.FirstOrDefault();
|
||
_cache[title] = result;
|
||
return result;
|
||
}
|
||
|
||
private TMDbClient GetTMDbClient()
|
||
{
|
||
if (_client != null) return _client;
|
||
|
||
var apiKey = config.ApiKey;
|
||
var proxy = config.HttpProxy;
|
||
|
||
if (string.IsNullOrEmpty(proxy))
|
||
{
|
||
_client = new TMDbClient(apiKey);
|
||
}
|
||
else
|
||
{
|
||
_client = new TMDbClient(apiKey , proxy: new WebProxy(proxy));
|
||
}
|
||
|
||
return _client;
|
||
}
|
||
|
||
|
||
|
||
public async Task Parse(TreeNode node)
|
||
{
|
||
// if (!node.TryGetValue(ItemFields.Key_RawTitle, out var rawTitle, out _))
|
||
// {
|
||
// if (!DoRawParse(node))
|
||
// {
|
||
// return false;
|
||
// }
|
||
// node.TryGetValue(ItemFields.Key_RawTitle, out rawTitle, out _);
|
||
// }
|
||
//
|
||
// if (string.IsNullOrEmpty(rawTitle))
|
||
// {
|
||
// return false;
|
||
// }
|
||
//
|
||
// var result = await QueryTMDB(rawTitle);
|
||
// if (result == null)
|
||
// {
|
||
// GD.PrintErr($"找不到对应的TV:{rawTitle}");
|
||
// return false;
|
||
// }
|
||
//
|
||
// if (result.FirstAirDate != null)
|
||
// {
|
||
// node.Info.SetYear(result.FirstAirDate.Value.Year);
|
||
// }
|
||
//
|
||
// node.Info.SetTitleIfNotExist(result.Name);
|
||
//
|
||
// return true;
|
||
}
|
||
} |