Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2025-06-23 21:34:41 +07:00
parent c3ba2a8336
commit d7b46a0756
6 changed files with 119 additions and 22 deletions

View File

@@ -1,24 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
class EpisodeRenamer
{
// Паттерны для поиска номера эпизода (регулярные выражения + правила обработки)
private static readonly List<Pattern> patterns = new List<Pattern>
private class PatternConfig
{
new Pattern(new Regex(@"\[\d+\]"), 1, -1),
new Pattern(new Regex(@"[s]\d+[e]\d+", RegexOptions.IgnoreCase), 4, null),
new Pattern(new Regex(@"[s]\d+[.][e]\d+", RegexOptions.IgnoreCase), 5, null),
new Pattern(new Regex(@"\d+$"), 0, null)
};
public string Regex { get; set; }
public int? Start { get; set; }
public int? End { get; set; }
public bool IgnoreCase { get; set; }
}
// Поддерживаемые расширения файлов
private static readonly HashSet<string> extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private class Config
{
".mkv", ".avi", ".mp4"
};
public List<PatternConfig> Patterns { get; set; }
public List<string> Extensions { get; set; }
}
private static List<Pattern> patterns;
private static HashSet<string> extensions;
private class Pattern
{
@@ -36,6 +40,20 @@ class EpisodeRenamer
static void Main()
{
Console.Title = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;
try
{
LoadConfiguration();
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка загрузки конфигурации: {ex.Message}");
Console.WriteLine("Программа будет завершена.");
Console.ReadLine();
return;
}
Console.CancelKeyPress += (sender, e) =>
{
Console.WriteLine("\nВыход из программы.");
@@ -50,7 +68,7 @@ class EpisodeRenamer
try
{
Console.Write("\nВведите путь до папки с эпизодами: ");
string input = Console.ReadLine()?.Trim() ?? string.Empty;
string input = Console.ReadLine().Trim() ?? string.Empty;
string folder = string.IsNullOrEmpty(input)
? Directory.GetCurrentDirectory()
@@ -72,6 +90,34 @@ class EpisodeRenamer
}
}
private static void LoadConfiguration()
{
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
if (!File.Exists(configPath))
{
throw new FileNotFoundException("Конфигурационный файл config.json не найден");
}
string json = File.ReadAllText(configPath);
Config config = JsonConvert.DeserializeObject<Config>(json);
// Загружаем расширения файлов
extensions = new HashSet<string>(config.Extensions, StringComparer.OrdinalIgnoreCase);
// Загружаем паттерны
patterns = new List<Pattern>();
foreach (PatternConfig patternConfig in config.Patterns)
{
RegexOptions options = patternConfig.IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
Regex regex = new Regex(patternConfig.Regex, options);
patterns.Add(new Pattern(regex, patternConfig.Start, patternConfig.End));
}
Console.WriteLine("Конфигурация успешно загружена.");
Console.WriteLine($"Загружено {patterns.Count} паттернов и {extensions.Count} расширений.");
}
private static void ProcessFolder(string folder)
{
foreach (string filePath in Directory.GetFiles(folder))
@@ -93,7 +139,8 @@ class EpisodeRenamer
foreach (var pattern in patterns)
{
Match match = pattern.Regex.Match(nameWithoutExt);
if (!match.Success) continue;
if (!match.Success)
continue;
string found = match.Value;
int startIndex = AdjustIndex(pattern.Start, found.Length);
@@ -126,8 +173,10 @@ class EpisodeRenamer
private static int AdjustIndex(int? index, int length)
{
if (!index.HasValue) return length;
if (index.Value < 0) return length + index.Value;
if (!index.HasValue)
return length;
if (index.Value < 0)
return length + index.Value;
return index.Value;
}
}