95 lines
3.4 KiB
C#
95 lines
3.4 KiB
C#
namespace BangumiRenamer.Tools;
|
||
|
||
using Config;
|
||
using TMDbLib.Client;
|
||
using Data;
|
||
using NativeFileDialogSharp;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
|
||
public static class ShowCompletionChecker
|
||
{
|
||
static List<Show> FindShows(string checkPath)
|
||
{
|
||
var shows = new List<Show>();
|
||
var showPaths = Directory.GetDirectories(checkPath);
|
||
foreach (var showPath in showPaths)
|
||
{
|
||
var sessions = new List<Session>();
|
||
var sessionPaths = Directory.GetDirectories(showPath);
|
||
foreach (var sessionPath in sessionPaths)
|
||
{
|
||
var episodePaths = Directory.GetFiles(sessionPath);
|
||
HashSet<int> episodes = new HashSet<int>();
|
||
foreach (var episodePath in episodePaths)
|
||
{
|
||
var matches = Regex.Matches(episodePath, @".*S\d+E(\d+).*");
|
||
if (matches.Count == 0) continue;
|
||
var episode = int.Parse(matches[0].Groups[1].Value);
|
||
episodes.Add(episode);
|
||
}
|
||
var session = new Session
|
||
{
|
||
id = int.Parse(Path.GetFileName(sessionPath).Replace("Season ", "")),
|
||
episodes = episodes.ToList()
|
||
};
|
||
sessions.Add(session);
|
||
}
|
||
shows.Add(new Show
|
||
{
|
||
title = Path.GetFileName(showPath),
|
||
sessions = sessions
|
||
});
|
||
}
|
||
return shows;
|
||
}
|
||
|
||
|
||
public static void Run()
|
||
{
|
||
var dir = Dialog.FolderPicker();
|
||
if (!dir.IsOk) return;
|
||
var checkPath = dir.Path;
|
||
|
||
var shows = FindShows(checkPath);
|
||
Log.Info($"Total Shows: {shows.Count}");
|
||
|
||
var client = new TMDbClient(
|
||
apiKey: Config.Default.Get<TMDBConfig>().ApiKey ,
|
||
proxy: new WebProxy(Config.Default.Get<ProxyConfig>().HttpProxy));
|
||
|
||
var output = new StringBuilder();
|
||
foreach (var t in shows)
|
||
{
|
||
var match = Regex.Match(t.title, @"(.*) \((\d{4})\)");
|
||
var title = match.Groups[1].Value;
|
||
var year = int.Parse(match.Groups[2].Value);
|
||
var result = client.SearchTvShowAsync(title, firstAirDateYear: year).Result;
|
||
var info = result.Results.FirstOrDefault();
|
||
if (info == null)
|
||
{
|
||
Log.Error($"找不到对应的TV:{t.title}");
|
||
continue;
|
||
}
|
||
foreach (var session in t.sessions)
|
||
{
|
||
var sessionInfo = client.GetTvSeasonAsync(info.Id, session.id).Result;
|
||
if (sessionInfo == null)
|
||
{
|
||
Log.Error($"季度对不上,可能找错了:{t.title} -> {info.OriginalName}");
|
||
break;
|
||
}
|
||
foreach (var episode in sessionInfo.Episodes)
|
||
{
|
||
if (DateTime.Now.AddDays(-7) < episode.AirDate) continue;
|
||
if (!session.episodes.Contains(episode.EpisodeNumber))
|
||
{
|
||
output.AppendLine($"{title} 的第 {session.id} 季少了第 {episode.EpisodeNumber} 集");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Log.Info(output.ToString());
|
||
}
|
||
} |