2011-09-04 03:05:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using NLog;
|
2013-03-06 21:20:33 +00:00
|
|
|
|
using NzbDrone.Core.Organizer;
|
2013-02-19 06:01:03 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2011-09-04 03:05:44 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class MisnamedProvider
|
|
|
|
|
{
|
2013-02-22 00:47:09 +00:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-03-06 21:20:33 +00:00
|
|
|
|
private readonly IBuildFileNames _buildFileNames;
|
2011-09-04 03:05:44 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-03-06 21:20:33 +00:00
|
|
|
|
public MisnamedProvider(IEpisodeService episodeService, IBuildFileNames buildFileNames)
|
2011-09-04 03:05:44 +00:00
|
|
|
|
{
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_episodeService = episodeService;
|
2013-03-06 21:20:33 +00:00
|
|
|
|
_buildFileNames = buildFileNames;
|
2011-09-04 03:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual List<MisnamedEpisodeModel> MisnamedFiles(int pageNumber, int pageSize, out int totalItems)
|
|
|
|
|
{
|
|
|
|
|
var misnamedFiles = new List<MisnamedEpisodeModel>();
|
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
|
var episodesWithFiles = _episodeService.EpisodesWithFiles().GroupBy(e => e.EpisodeFileId).ToList();
|
2011-09-04 03:05:44 +00:00
|
|
|
|
totalItems = episodesWithFiles.Count();
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
|
|
|
|
var misnamedFilesSelect = episodesWithFiles.AsParallel().Where(
|
|
|
|
|
w =>
|
|
|
|
|
w.First().EpisodeFile.Path !=
|
2013-03-06 21:20:33 +00:00
|
|
|
|
_buildFileNames.GetNewFilename(w.Select(e => e).ToList(), w.First().Series,
|
2012-08-03 07:01:34 +00:00
|
|
|
|
w.First().EpisodeFile.Quality, w.First().EpisodeFile.Proper, w.First().EpisodeFile)).Skip(Math.Max(pageSize * (pageNumber - 1), 0)).Take(pageSize);
|
2011-09-04 03:05:44 +00:00
|
|
|
|
|
|
|
|
|
//Process the episodes
|
|
|
|
|
misnamedFilesSelect.AsParallel().ForAll(f =>
|
|
|
|
|
{
|
|
|
|
|
var episodes = f.Select(e => e).ToList();
|
|
|
|
|
var firstEpisode = episodes[0];
|
2013-03-06 21:20:33 +00:00
|
|
|
|
var properName = _buildFileNames.GetNewFilename(episodes,
|
2012-12-27 03:51:04 +00:00
|
|
|
|
firstEpisode.Series,
|
2012-08-03 07:01:34 +00:00
|
|
|
|
firstEpisode.EpisodeFile.Quality, firstEpisode.EpisodeFile.Proper, firstEpisode.EpisodeFile);
|
2011-09-04 03:05:44 +00: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 07:45:58 +00:00
|
|
|
|
SeriesTitle = firstEpisode.Series.Title
|
2011-09-04 03:05:44 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
return misnamedFiles.OrderBy(e => e.SeriesTitle).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|