添加整理
This commit is contained in:
parent
3d1ebec4db
commit
c5fc9ac223
114
FileMover.cs
114
FileMover.cs
@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Learn.Models;
|
||||
using Learn.Parsers;
|
||||
|
||||
@ -6,16 +8,118 @@ namespace Learn;
|
||||
|
||||
public class FileMover(string targetPath)
|
||||
{
|
||||
private readonly HashSet<string> _visited = new ();
|
||||
private readonly HashSet<TreeNode> _visited = new ();
|
||||
|
||||
private string NumToString(int n)
|
||||
{
|
||||
if (n < 10) return "0" + n;
|
||||
return n.ToString();
|
||||
}
|
||||
|
||||
private bool TryGetPath(TreeNode node, out string path)
|
||||
{
|
||||
path = null;
|
||||
if (!node.TryGetValue(ItemFields.Key_Title, out var title, out _)) return false;
|
||||
if (!node.TryGetValue(ItemFields.Key_Season, out var season, out _)) return false;
|
||||
if (!node.TryGetValue(ItemFields.Key_Year, out var year, out _)) return false;
|
||||
|
||||
path = $"{title} ({year})/";
|
||||
|
||||
if (int.TryParse(season, out var seasonNum))
|
||||
{
|
||||
path += $"Season {seasonNum}";
|
||||
}
|
||||
else
|
||||
{
|
||||
path += season;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void MoveFile(TreeNode node)
|
||||
{
|
||||
var path = node.Info.MainInfo[ItemFields.MainKey_Path];
|
||||
|
||||
if (!node.TryGetValue(ItemFields.Key_Type, out var type, out _)) return;
|
||||
if (!node.TryGetValue(ItemFields.Key_Title, out var title, out _)) return;
|
||||
if (!node.TryGetValue(ItemFields.Key_Episode, out var episode, out _)) return;
|
||||
if (!node.TryGetValue(ItemFields.Key_Season, out var season, out _)) return;
|
||||
if (!node.TryGetValue(ItemFields.Key_Year, out var year, out _)) return;
|
||||
if (!TryGetPath(node, out var relativePath)) return;
|
||||
|
||||
var filenameParts = new List<string>();
|
||||
var metaParts = new List<string>();
|
||||
filenameParts.Add(title);
|
||||
filenameParts.Add($"({year})");
|
||||
|
||||
var seasonAndEpisodePart = "";
|
||||
if (int.TryParse(season, out var seasonNum))
|
||||
{
|
||||
seasonAndEpisodePart += $"S{NumToString(seasonNum)}";
|
||||
}
|
||||
var episodeNum = int.Parse(episode);
|
||||
seasonAndEpisodePart += $"E{NumToString(episodeNum)}";
|
||||
filenameParts.Add(seasonAndEpisodePart);
|
||||
|
||||
if (node.TryGetValue(ItemFields.Key_Group, out var group, out _))
|
||||
{
|
||||
metaParts.Add(group);
|
||||
}
|
||||
|
||||
if (type == ItemFields.ItemType.Subtitle.ToString())
|
||||
{
|
||||
if (node.TryGetValue(ItemFields.Key_SubtitleLanguage, out var subLang, out _))
|
||||
{
|
||||
metaParts.Add(subLang);
|
||||
}
|
||||
}
|
||||
|
||||
var filename = string.Join(" ", string.Join(" ", filenameParts), string.Join("", metaParts.Select(part => $"[{part}]")));
|
||||
filename += Path.GetExtension(path);
|
||||
var filePath = string.Join("/", targetPath, relativePath, filename);
|
||||
node.Info.Info["TargetPath"] = filePath;
|
||||
}
|
||||
|
||||
private void MoveExtra(TreeNode node)
|
||||
{
|
||||
if (!TryGetPath(node, out var relativePath)) return;
|
||||
|
||||
var path = node.Info.MainInfo[ItemFields.MainKey_Path];
|
||||
var name = Path.GetFileName(path);
|
||||
var target = string.Join("/", targetPath, relativePath, name);
|
||||
node.Info.Info["TargetPath"] = target;
|
||||
}
|
||||
|
||||
public void DoMove(TreeNode node)
|
||||
{
|
||||
if (!node.Info.IsFolder())
|
||||
{
|
||||
var path = ItemFields.MainKey_Path;
|
||||
if (_visited.Contains(path)) return;
|
||||
if (_visited.Contains(node)) return;
|
||||
|
||||
string type;
|
||||
if (!node.TryGetValue(ItemFields.Key_Type, out type, out _))
|
||||
{
|
||||
type = ItemFields.ItemType.Unknown.ToString();
|
||||
}
|
||||
|
||||
if (type == ItemFields.ItemType.Extra.ToString())
|
||||
{
|
||||
MoveExtra(node);
|
||||
_visited.Add(node);
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.Info.IsFolder())
|
||||
{
|
||||
foreach (var child in node.Children)
|
||||
{
|
||||
DoMove(child);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveFile(node);
|
||||
}
|
||||
|
||||
_visited.Add(node);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user