[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert movie matching #948

Merged
merged 8 commits into from
Feb 19, 2019
Merged
99 changes: 53 additions & 46 deletions Emby.Naming/Video/VideoListResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,64 +175,71 @@ private IEnumerable<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos)
return videos;
}

var list = new List<VideoInfo>();

var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path));
if (!string.IsNullOrEmpty(folderName))

if (!string.IsNullOrEmpty(folderName) && folderName.Length > 1)
JustAMan marked this conversation as resolved.
Show resolved Hide resolved
{
var videosMatchingFolder = new List<VideoInfo>();
foreach (VideoInfo video in videos)
if (videos.All(i => i.Files.Count == 1 && IsEligibleForMultiVersion(folderName, i.Files[0].Path)))
{
// Only interested in single files
if (video.Files.Count != 1)
// Enforce the multi-version limit
if (videos.Count <= 8 && HaveSameYear(videos))
{
continue;
}
var ordered = videos.OrderBy(i => i.Name).ToList();

if (string.Equals(folderName, video.Name, StringComparison.OrdinalIgnoreCase))
{
videosMatchingFolder.Add(video);
}
// Eg. My Movie == My Movie - Some Other Info, TODO doesn't seem like a robust test
else if (video.Name.StartsWith(folderName, StringComparison.OrdinalIgnoreCase) &&
video.Name.Substring(folderName.Length).TrimStart().StartsWith("-"))
{
videosMatchingFolder.Add(video);
list.Add(ordered[0]);

list[0].AlternateVersions = ordered.Skip(1).Select(i => i.Files[0]).ToList();
list[0].Name = folderName;
list[0].Extras.AddRange(ordered.Skip(1).SelectMany(i => i.Extras));

return list;
}
}
}

// It is assumed that any non-matching files are random samples, trailers, extras etc.
// So if there's at least one video file matching the folder name, skip the rest.
if (videosMatchingFolder.Count > 0)
{
var primary = videosMatchingFolder[0];
var remainingVideos = videosMatchingFolder.Skip(1);
var videoInfo = new VideoInfo
{
Name = folderName,
Year = primary.Year,
Files = primary.Files,
AlternateVersions = new List<VideoFileInfo>(),
Extras = primary.Extras
};
foreach (VideoInfo video in remainingVideos)
{
videoInfo.AlternateVersions.Add(video.Files.First());
videoInfo.Extras.AddRange(video.Extras);
}
return videos;
//foreach (var video in videos.OrderBy(i => i.Name))
//{
// var match = list
// .FirstOrDefault(i => string.Equals(i.Name, video.Name, StringComparison.OrdinalIgnoreCase));

// if (match != null && video.Files.Count == 1 && match.Files.Count == 1)
// {
// match.AlternateVersions.Add(video.Files[0]);
// match.Extras.AddRange(video.Extras);
// }
// else
// {
// list.Add(video);
// }
//}

//return list;
}

return new[] { videoInfo };
}
private bool HaveSameYear(List<VideoInfo> videos)
{
return videos.Select(i => i.Year ?? -1).Distinct().Count() < 2;
}

private bool IsEligibleForMultiVersion(string folderName, string testFilename)
{
testFilename = Path.GetFileNameWithoutExtension(testFilename);

if (string.Equals(folderName, testFilename, StringComparison.OrdinalIgnoreCase))
{
return true;
}

return videos.GroupBy(v => new { v.Name, v.Year }).Select(group => new VideoInfo
if (testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
{
// Because of the grouping, we can grab the information from the first movie and make it primary
// The remaining movie matches are 'alternate versions'
Name = group.First().Name,
Year = group.First().Year,
Files = group.First().Files,
AlternateVersions = group.Skip(1).Select(i => i.Files[0]).ToList(),
Extras = group.First().Extras.Concat(group.Skip(1).SelectMany(i => i.Extras)).ToList()
});
testFilename = testFilename.Substring(folderName.Length).Trim();
return testFilename.StartsWith("-", StringComparison.OrdinalIgnoreCase) || Regex.Replace(testFilename, @"\[([^]]*)\]", "").Trim() == string.Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return testFilename.StartsWith("-", StringComparison.OrdinalIgnoreCase) || Regex.Replace(testFilename, @"\[([^]]*)\]", "").Trim() == string.Empty;
return testFilename.StartsWith("-", StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this fail for the empty string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Indicates whether a specified string is null, empty, or consists only of white-space characters.”
https://docs.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.7.2

TLDR: No

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's misleading...

}

return false;
}

private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
Expand Down