public EpisodeRenamer(	EpisodeRenamerSettings episodeRenamerSettings = null, 
								EpisodeRenamerLogger episodeRenamerLogger = null)
        {
            EpisodeRenamerLogger = episodeRenamerLogger ?? new EpisodeRenamerLogger();
            EpisodeRenamerSettings = episodeRenamerSettings ?? new EpisodeRenamerSettings();

            MagicFields = new Dictionary<string, MagicFieldAction>
            {
                {"%r", (episode, settings) => { return episode.Series.Name; }},
                {"%s", (episode, settings) => { return episode.SeasonNumber.ToString(CultureInfo.InvariantCulture).PadLeft(settings.PadSeasonDigits, '0'); }},
                {"%e", (episode, settings) => { return episode.EPNum.ToString(CultureInfo.InvariantCulture).PadLeft(settings.PadEpisodeNumberDigits, '0'); }},
                {"%f", (episode, settings) => { return episode.EPInSeason.ToString(CultureInfo.InvariantCulture).PadLeft(settings.PadEpisodeNumberDigits, '0'); }},
                {"%p", (episode, settings) => { return episode.ProductionNum; }},
                {"%t", (episode, settings) => { return episode.Title; }},
                {"%d", (episode, settings) => { return BuildDate(episode.AirDate, settings.DateFormula); }}
            };
        }
        /// <summary>
        /// Generate a string based on a formula. %r for Series name, %s for Season Number, %e for the overall episode
        /// number, %f for the episode number in the season, %p for the production number, %t for the title, and %d for the 
        /// air date
        /// </summary>
        /// <returns></returns>
        public string Rename(FileInfo file, Episode episode, EpisodeRenamerSettings settings = null)
        {
            settings = settings ?? EpisodeRenamerSettings;
            var newFilename = CleanFilename(GetNewFilename(episode, settings));
            var newFilePath = string.Format("{0}\\{1}{2}", file.Directory, newFilename, file.Extension);

            if(!file.FullName.Equals(newFilePath, StringComparison.CurrentCultureIgnoreCase))
            {
                if (!settings.TestMode)
                {
                    var date = DateTime.Now;
                    File.Move(file.FullName, newFilePath);
                    EpisodeRenamerLogger.Log(date, file.FullName, newFilename);
                    return newFilePath;
                }

                return newFilePath;
            }

            return string.Empty;
        }
 protected string GetNewFilename(Episode episode, EpisodeRenamerSettings settings)
 {
     return MagicFields.Aggregate(settings.RenamingFormula,
         (current, magicField) => current.Replace(magicField.Key, magicField.Value.Invoke(episode, settings)));
 }
示例#4
0
        public static void Main(string[] args)
        {
            try
            {
                var parsedArgs = GetCommandLineArgs(args.ToList());

                var path = GetFromCommandLineOrAlternative(parsedArgs, PathVariableName, fieldName => AskForPath());
                var renameFormula = GetFromCommandLineOrAlternative(parsedArgs, FormulaVariableName, GetApplicationVariable);
                var dateFormula = GetFromCommandLineOrAlternative(parsedArgs, DateFormulaVariableName, GetApplicationVariable);
                var seasonDigits = GetFromCommandLineOrAlternative(parsedArgs, SeasonDigitsVariableName, GetApplicationVariable);
                var episodeDigits = GetFromCommandLineOrAlternative(parsedArgs, EpisodeDigitsVariableName, GetApplicationVariable);

                var eligibleFileTypes = GetFromCommandLineOrAlternative(parsedArgs, EligibleFileTypesVariableName, GetApplicationVariable).ConvertFromCSV();

                var episodeFileScanner = new EpisodeFileScanner(eligibleFileTypes);

                var settings = new EpisodeRenamerSettings(renameFormula, dateFormula)
                {
                    PadEpisodeNumberDigits = Convert.ToInt32(episodeDigits),
                    PadSeasonDigits = Convert.ToInt32(seasonDigits),
                    TestMode = true
                };

                var episodeRenamer = new EpisodeRenamer(settings);

                var renamer = new TVShowRenamer(Console.WriteLine, OnFoundMultipleSeries, episodeRenamer);

                var files = episodeFileScanner.Scan(path);

                renamer.Rename(files);
            }
            catch (Exception error)
            {
                Console.WriteLine("Critical Error! {0}".FormatReplace(error.Message));
            }
        }