2011-09-04 06:05:44 +03:00
|
|
|
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 !=
|
2011-09-04 10:45:58 +03:00
|
|
|
_mediaFileProvider.GetNewFilename(w.Select(e => e).ToList(), w.First().Series.Title,
|
2012-08-03 10:01:34 +03:00
|
|
|
w.First().EpisodeFile.Quality, w.First().EpisodeFile.Proper, w.First().EpisodeFile)).Skip(Math.Max(pageSize * (pageNumber - 1), 0)).Take(pageSize);
|
2011-09-04 06:05:44 +03:00
|
|
|
|
|
|
|
//Process the episodes
|
|
|
|
misnamedFilesSelect.AsParallel().ForAll(f =>
|
|
|
|
{
|
|
|
|
var episodes = f.Select(e => e).ToList();
|
|
|
|
var firstEpisode = episodes[0];
|
|
|
|
var properName = _mediaFileProvider.GetNewFilename(episodes,
|
2011-09-04 10:45:58 +03:00
|
|
|
firstEpisode.Series.Title,
|
2012-08-03 10:01:34 +03:00
|
|
|
firstEpisode.EpisodeFile.Quality, firstEpisode.EpisodeFile.Proper, firstEpisode.EpisodeFile);
|
2011-09-04 06:05:44 +03:00
|
|
|
|
|
|
|
var currentName = Path.GetFileNameWithoutExtension(firstEpisode.EpisodeFile.Path);
|
|
|
|
|
|
|
|
if (properName != currentName)
|
|
|
|
{
|
|
|
|
misnamedFiles.Add(new MisnamedEpisodeModel
|
|
|
|
{
|
|
|
|
CurrentName = currentName,
|
|
|
|
EpisodeFileId = firstEpisode.EpisodeFileId,
|
|
|
|
ProperName = properName,
|
|
|
|
SeriesId = firstEpisode.SeriesId,
|
2011-09-04 10:45:58 +03:00
|
|
|
SeriesTitle = firstEpisode.Series.Title
|
2011-09-04 06:05:44 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
return misnamedFiles.OrderBy(e => e.SeriesTitle).ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|