BangumiRenamer/Src/Tools/ShowCompletionChecker.cs
2025-11-01 17:20:03 +08:00

95 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
Console.WriteLine($"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)
{
Console.WriteLine($"找不到对应的TV{t.title}");
continue;
}
foreach (var session in t.sessions)
{
var sessionInfo = client.GetTvSeasonAsync(info.Id, session.id).Result;
if (sessionInfo == null)
{
Console.WriteLine($"季度对不上,可能找错了:{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} 集");
}
}
}
}
Console.Write(output);
}
}