1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-27 18:28:19 +00:00
Sonarr/NzbDrone.Core/Providers/MisnamedProvider.cs
Mark McDowall 0b586de226 Added misnamed provider, PLINQ speeds it up, but still to slow for use, paging helps, but isn't consistent.
A bunch of files changed removing System.Linq, thanks Resharper :(
2011-09-03 20:05:44 -07:00

70 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class MisnamedProvider
{
private readonly MediaFileProvider _mediaFileProvider;
private readonly EpisodeProvider _episodeProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public MisnamedProvider(MediaFileProvider mediaFileProvider, EpisodeProvider episodeProvider)
{
_mediaFileProvider = mediaFileProvider;
_episodeProvider = episodeProvider;
}
public virtual List<MisnamedEpisodeModel> MisnamedFiles(int pageNumber, int pageSize, out int totalItems)
{
var misnamedFiles = new List<MisnamedEpisodeModel>();
var episodesWithFiles = _episodeProvider.EpisodesWithFiles().GroupBy(e => e.EpisodeFileId).ToList();
totalItems = episodesWithFiles.Count();
var stopwatch = new Stopwatch();
stopwatch.Start();
var misnamedFilesSelect = episodesWithFiles.AsParallel().Where(
w =>
w.First().EpisodeFile.Path !=
_mediaFileProvider.GetNewFilename(w.Select(e => e).ToList(), w.First().SeriesTitle,
w.First().EpisodeFile.Quality)).Skip(Math.Max(pageSize * (pageNumber - 1), 0)).Take(pageSize);
//Process the episodes
misnamedFilesSelect.AsParallel().ForAll(f =>
{
var episodes = f.Select(e => e).ToList();
var firstEpisode = episodes[0];
var properName = _mediaFileProvider.GetNewFilename(episodes,
firstEpisode.SeriesTitle,
firstEpisode.EpisodeFile.Quality);
var currentName = Path.GetFileNameWithoutExtension(firstEpisode.EpisodeFile.Path);
if (properName != currentName)
{
misnamedFiles.Add(new MisnamedEpisodeModel
{
CurrentName = currentName,
EpisodeFileId = firstEpisode.EpisodeFileId,
ProperName = properName,
SeriesId = firstEpisode.SeriesId,
SeriesTitle = firstEpisode.SeriesTitle
});
}
});
stopwatch.Stop();
return misnamedFiles.OrderBy(e => e.SeriesTitle).ToList();
}
}
}