Example #1
0
        /// <summary>
        /// Process a file rename
        /// </summary>
        /// <param name="pathFrom">
        /// Path from.
        /// </param>
        /// <param name="renameTo">
        /// Rename too.
        /// </param>
        /// <returns>
        /// The remained path.
        /// </returns>
        public static string DoRename(string pathFrom, string renameTo)
        {
            renameTo = FileSystemCharChange.To(renameTo);

            if (MovieNaming.IsBluRay(pathFrom))
            {
                string folderPathFrom = MovieNaming.GetBluRayPath(pathFrom) + MovieNaming.GetBluRayName(pathFrom);
                string folderPathTo   = MovieNaming.GetBluRayPath(pathFrom) + renameTo;

                if (folderPathFrom != folderPathTo)
                {
                    try
                    {
                        Directory.Move(folderPathFrom, folderPathTo);
                        return(folderPathTo);
                    }
                    catch
                    {
                        return(string.Empty);
                    }
                }

                return(folderPathFrom);
            }

            if (MovieNaming.IsDVD(pathFrom))
            {
                string folderPathFrom = MovieNaming.GetDvdPath(pathFrom) + MovieNaming.GetDvdName(pathFrom);
                string folderPathTo   = MovieNaming.GetDvdPath(pathFrom) + renameTo;

                if (folderPathFrom != folderPathTo)
                {
                    try
                    {
                        Directory.Move(folderPathFrom, folderPathTo);
                        return(folderPathTo);
                    }
                    catch
                    {
                        return(string.Empty);
                    }
                }

                return(folderPathFrom);
            }

            string pathTo = Path.GetDirectoryName(pathFrom) + Path.DirectorySeparatorChar + renameTo +
                            Path.GetExtension(pathFrom);

            try
            {
                File.Move(pathFrom, pathTo);
            }
            catch
            {
                return(pathFrom);
            }

            return(pathTo);
        }
Example #2
0
        public static string TvSeries(Series series, string replace, string altFirstEpisode = null)
        {
            string firstEpisodeFullPath;
            string seriesName;

            if (altFirstEpisode != null)
            {
                if (altFirstEpisode == string.Empty)
                {
                    return(string.Empty);
                }

                if (MovieNaming.IsDVD(altFirstEpisode))
                {
                    firstEpisodeFullPath = MovieNaming.GetDvdPath(altFirstEpisode) + MovieNaming.GetDvdName(altFirstEpisode);
                }
                else if (MovieNaming.IsBluRay(altFirstEpisode))
                {
                    firstEpisodeFullPath = MovieNaming.GetBluRayPath(altFirstEpisode) + MovieNaming.GetBluRayName(altFirstEpisode);
                }
                else
                {
                    firstEpisodeFullPath = altFirstEpisode;
                }

                seriesName = Regex.Match(
                    Path.GetFileNameWithoutExtension(firstEpisodeFullPath),
                    "(?<seriesName>.*?)" + Settings.ConstSettings.DefaultRegex.Tv,
                    RegexOptions.IgnoreCase).Groups["seriesName"].Value.Trim();
            }
            else
            {
                firstEpisodeFullPath = series.GetFirstEpisode();

                seriesName = Tools.Restructure.FileSystemCharChange.To(series.SeriesName);
            }

            string firstEpisodePath;

            if (MovieNaming.IsDVD(firstEpisodeFullPath))
            {
                firstEpisodePath = MovieNaming.GetDvdPath(firstEpisodeFullPath);
            }
            else if (MovieNaming.IsBluRay(firstEpisodeFullPath))
            {
                firstEpisodePath = MovieNaming.GetBluRayPath(firstEpisodeFullPath);
            }
            else
            {
                firstEpisodePath = Path.GetDirectoryName(firstEpisodeFullPath);
            }

            replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesName, seriesName);
            replace = replace.Replace(Settings.Get.InOutCollection.TvFirstEpisodePathOfSeries, firstEpisodePath);

            return(replace);
        }
Example #3
0
        public static string TvEpisode(Episode episode, string replace, string fromFile, string altEpisode = null)
        {
            if (episode == null)
            {
                episode          = new Episode();
                episode.FilePath = new MediaModel {
                    PathAndFilename = @"c:\testshow\season 1\test show.s01e01.avi"
                };
            }

            string episodePath;
            string episodeFileName;

            if (altEpisode == null)
            {
                altEpisode = episode.CurrentFilenameAndPath;
            }

            if (altEpisode == string.Empty)
            {
                return(string.Empty);
            }

            if (MovieNaming.IsDVD(altEpisode))
            {
                episodePath     = MovieNaming.GetDvdPath(altEpisode);
                episodeFileName = MovieNaming.GetDvdName(altEpisode);
            }
            else if (MovieNaming.IsBluRay(altEpisode))
            {
                episodePath     = MovieNaming.GetBluRayPath(altEpisode);
                episodeFileName = MovieNaming.GetBluRayName(altEpisode);
            }
            else
            {
                episodePath     = Path.GetDirectoryName(altEpisode);
                episodeFileName = Path.GetFileNameWithoutExtension(altEpisode);
            }

            replace = replace.Replace(Settings.Get.InOutCollection.TvEpisodePath, episodePath);
            replace = replace.Replace(Settings.Get.InOutCollection.TvEpisodeFileName, episodeFileName);


            return(replace + Path.GetExtension(fromFile));
        }
Example #4
0
        /// <summary>
        /// Gets the season path.
        /// </summary>
        /// <returns>
        /// The season path.
        /// </returns>
        public string GetSeasonPath()
        {
            string path = string.Empty;

            foreach (Episode episode in this.Episodes)
            {
                if (!string.IsNullOrEmpty(episode.FilePath.PathAndFilename))
                {
                    if (File.Exists(episode.FilePath.PathAndFilename))
                    {
                        string[] segSplit = episode.FilePath.PathAndFilename.Split(new[] { '\\' });

                        if (MovieNaming.IsDVD(episode.FilePath.PathAndFilename))
                        {
                            path = string.Join(@"\", segSplit, 0, segSplit.Length - 3);
                        }
                        else if (MovieNaming.IsBluRay(episode.FilePath.PathAndFilename))
                        {
                            path = string.Join(@"\", segSplit, 0, segSplit.Length - 4);
                        }
                        else
                        {
                            path = string.Join(@"\", segSplit, 0, segSplit.Length - 1);
                        }

                        return(path);
                    }
                }
            }

            if (string.IsNullOrEmpty(path))
            {
                path = string.Format(
                    "{0}{1}Season {2}",
                    TvDBFactory.CurrentSeries.GetSeriesPath(),
                    Path.DirectorySeparatorChar,
                    this.SeasonNumber);
            }

            return(path);
        }
Example #5
0
        public static string TvEpisode(Episode episode, string replace, string altEpisode = null)
        {
            string episodePath;
            string episodeFileName;

            if (altEpisode == null)
            {
                altEpisode = episode.CurrentFilenameAndPath;
            }

            if (altEpisode == string.Empty)
            {
                return(string.Empty);
            }

            if (MovieNaming.IsDVD(altEpisode))
            {
                episodePath     = MovieNaming.GetDvdPath(altEpisode);
                episodeFileName = MovieNaming.GetDvdName(altEpisode);
            }
            else if (MovieNaming.IsBluRay(altEpisode))
            {
                episodePath     = MovieNaming.GetBluRayPath(altEpisode);
                episodeFileName = MovieNaming.GetBluRayName(altEpisode);
            }
            else
            {
                episodePath     = Path.GetDirectoryName(altEpisode);
                episodeFileName = Path.GetFileNameWithoutExtension(altEpisode);
            }

            replace = replace.Replace(Settings.Get.InOutCollection.TvEpisodePath, episodePath);
            replace = replace.Replace(Settings.Get.InOutCollection.TvEpisodeFileName, episodeFileName);


            return(replace);
        }
Example #6
0
        /// <summary>
        /// Gets the series path.
        /// </summary>
        /// <returns>The Series path</returns>
        public string GetSeriesPath()
        {
            foreach (var seasons in this.Seasons)
            {
                foreach (Episode episode in seasons.Value.Episodes)
                {
                    if (!string.IsNullOrEmpty(episode.FilePath.PathAndFilename))
                    {
                        if (File.Exists(episode.FilePath.PathAndFilename))
                        {
                            if (MovieNaming.IsDVD(episode.FilePath.PathAndFilename))
                            {
                                string[] segSplit = episode.FilePath.PathAndFilename.Split(new[] { '\\' });

                                return(string.Join(@"\", segSplit, 0, segSplit.Length - 2));
                            }

                            if (MovieNaming.IsBluRay(episode.FilePath.PathAndFilename))
                            {
                                string[] segSplit = episode.FilePath.PathAndFilename.Split(new[] { '\\' });

                                return(string.Join(@"\", segSplit, 0, segSplit.Length - 3));
                            }
                            else
                            {
                                string[] segSplit = episode.FilePath.PathAndFilename.Split(new[] { '\\' });
                                string   path     = string.Join(@"\", segSplit, 0, segSplit.Length - 2);

                                return(path);
                            }
                        }
                    }
                }
            }

            return(string.Empty);
        }
Example #7
0
        /// <summary>
        /// Saves the season.
        /// </summary>
        /// <param name="season">The season.</param>
        /// <param name="type">The SeasonIOType type.</param>
        public void SaveSeason(Season season, SeasonIOType type)
        {
            if (season.HasEpisodeWithPath())
            {
                string posterTemplate;
                string fanartTemplate;
                string bannerTemplate;

                string firstEpisodePath = season.GetFirstEpisode();

                if (MovieNaming.IsBluRay(firstEpisodePath))
                {
                    posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonPosterTemplate;
                    fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonFanartTemplate;
                    bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonBannerTemplate;
                }
                else if (MovieNaming.IsDVD(firstEpisodePath))
                {
                    posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonPosterTemplate;
                    fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonFanartTemplate;
                    bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonBannerTemplate;
                }
                else
                {
                    posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonPosterTemplate;
                    fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonFanartTemplate;
                    bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonBannerTemplate;
                }

                // Poster
                if (type == SeasonIOType.All || type == SeasonIOType.Poster)
                {
                    if (!string.IsNullOrEmpty(season.PosterUrl))
                    {
                        string posterPathFrom;

                        if (!string.IsNullOrEmpty(season.PosterPath) && File.Exists(season.PosterPath))
                        {
                            posterPathFrom = season.PosterPath;
                        }
                        else
                        {
                            posterPathFrom = this.TvPathImageGet(season.PosterUrl);
                        }

                        string posterPathTo = GeneratePath.TvSeason(season, posterTemplate);

                        this.CopyFile(posterPathFrom, posterPathTo + ".jpg");
                        season.ChangedPoster = false;
                    }
                }

                // Fanart
                if (type == SeasonIOType.All || type == SeasonIOType.Fanart)
                {
                    if (!string.IsNullOrEmpty(season.FanartUrl))
                    {
                        string fanartPathFrom;

                        if (!string.IsNullOrEmpty(season.PosterPath) && File.Exists(season.PosterPath))
                        {
                            fanartPathFrom = season.FanartPath;
                        }
                        else
                        {
                            fanartPathFrom = this.TvPathImageGet(season.FanartUrl);
                        }

                        string fanartPathTo = GeneratePath.TvSeason(season, fanartTemplate);

                        this.CopyFile(fanartPathFrom, fanartPathTo + ".jpg");
                        season.ChangedFanart = false;
                    }
                }

                // Banner
                if (type == SeasonIOType.All || type == SeasonIOType.Banner)
                {
                    if (!string.IsNullOrEmpty(season.BannerUrl))
                    {
                        string bannerPathFrom;

                        if (!string.IsNullOrEmpty(season.BannerPath) && File.Exists(season.BannerPath))
                        {
                            bannerPathFrom = season.BannerPath;
                        }
                        else
                        {
                            bannerPathFrom = this.TvPathImageGet(season.BannerUrl);
                        }

                        string bannerPathTo = GeneratePath.TvSeason(season, bannerTemplate);

                        this.CopyFile(bannerPathFrom, bannerPathTo + ".jpg");
                        season.ChangedBanner = false;
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Saves the episode.
        /// </summary>
        /// <param name="episode">The episode.</param>
        /// <param name="type">The EpisodeIOType type.</param>
        public void SaveEpisode(Episode episode, EpisodeIOType type)
        {
            if (episode.Secondary)
            {
                return;
            }

            if (string.IsNullOrEmpty(episode.FilePath.FileNameAndPath))
            {
                return;
            }

            string nfoTemplate;
            string screenshotTemplate;

            if (MovieNaming.IsDVD(episode.FilePath.FileNameAndPath))
            {
                nfoTemplate        = Get.InOutCollection.CurrentTvSaveSettings.DVDEpisodeNFOTemplate;
                screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDEpisodeScreenshotTemplate;
            }
            else if (MovieNaming.IsBluRay(episode.FilePath.FileNameAndPath))
            {
                nfoTemplate        = Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeNFOTemplate;
                screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeScreenshotTemplate;
            }
            else
            {
                nfoTemplate        = Get.InOutCollection.CurrentTvSaveSettings.EpisodeNFOTemplate;
                screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.EpisodeScreenshotTemplate;
            }

            // Nfo
            if (type == EpisodeIOType.All || type == EpisodeIOType.Nfo)
            {
                string nfoPathTo = GeneratePath.TvEpisode(episode, nfoTemplate);

                this.WriteNFO(this.GenerateSingleEpisodeOutput(episode, true), nfoPathTo);
                episode.ChangedText = false;
            }

            // Screenshot
            if (type == EpisodeIOType.Screenshot || type == EpisodeIOType.All)
            {
                if (!string.IsNullOrEmpty(episode.FilePath.FileNameAndPath))
                {
                    string screenshotPathFrom;

                    if (!string.IsNullOrEmpty(episode.EpisodeScreenshotPath) &&
                        File.Exists(episode.EpisodeScreenshotPath))
                    {
                        screenshotPathFrom = episode.EpisodeScreenshotPath;
                    }
                    else
                    {
                        screenshotPathFrom = this.TvPathImageGet(episode.EpisodeScreenshotUrl);
                    }

                    string screenshotPathTo = GeneratePath.TvEpisode(episode, screenshotTemplate);

                    this.CopyFile(screenshotPathFrom, screenshotPathTo + ".jpg");
                    episode.ChangedScreenshot = false;
                }
            }
        }
Example #9
0
        /// <summary>
        /// Renames the episode.
        /// </summary>
        /// <param name="episode">
        /// The episode.
        /// </param>
        /// <returns>
        /// The rename episode.
        /// </returns>
        public static string RenameEpisode(Episode episode)
        {
            string seriesName = "";

            string season1 = string.Empty;
            string season2 = string.Empty;

            string episode1 = string.Empty;
            string episode2 = string.Empty;

            string episodeName = string.Empty;

            bool           doRename;
            bool           dummy = false;
            List <Episode> episodesContaining = new List <Episode>();

            if (episode != null && episode.ProductionCode == "dummy")
            {
                dummy      = true;
                seriesName = "Star Trek: Deep Space Nine";
                episode    = new Episode
                {
                    EpisodeNumber = 5,
                    EpisodeName   = "Cardassians",
                    SeasonNumber  = 2
                };
                Episode ep2 = new Episode
                {
                    EpisodeNumber = 6,
                    EpisodeName   = "Cardassians2",
                    SeasonNumber  = 2
                };
                Episode ep3 = new Episode
                {
                    EpisodeNumber = 7,
                    EpisodeName   = "Cardassians3",
                    SeasonNumber  = 2
                };

                episodesContaining.Add(episode);
                episodesContaining.Add(ep2);
                episodesContaining.Add(ep3);
            }

            if (episode == null)
            {
                seriesName = "Star Trek: Deep Space Nine";

                season1  = "2";
                season2  = "02";
                episode1 = "5";
                episode2 = "05";

                episodeName = "Cardassians";
                doRename    = false;
            }
            else
            {
                episode1 = string.Empty;
                episode2 = string.Empty;

                if (seriesName == "")
                {
                    seriesName = episode.GetSeriesName();
                }
                int?seasonNumber = episode.SeasonNumber;

                if (episodesContaining.Count == 0)
                {
                    episodesContaining = GetEpisodesContainingFile(episode);
                }

                season1 = seasonNumber.ToString();
                season2 = string.Format("{0:00}", seasonNumber);

                doRename = false;

                if (episodesContaining.Count == 1)
                {
                    episode1    = episode.EpisodeNumber.ToString();
                    episode2    = string.Format("{0:00}", episode.EpisodeNumber);
                    doRename    = true;
                    episodeName = episode.EpisodeName;
                }
                else
                {
                    int    count         = 0;
                    string multiTemplate = Get.InOutCollection.EpisodeMultiTemplate;
                    multiTemplate = multiTemplate.Replace(EpisodeNumber1Template, "{0}");
                    multiTemplate = multiTemplate.Replace(EpisodeNumber2Template, "{0:00}");

                    foreach (Episode ep in episodesContaining)
                    {
                        if (ep.EpisodeNumber == episode.EpisodeNumber && count == 0)
                        {
                            doRename = true;
                        }

                        episode1 += string.Format(multiTemplate, ep.EpisodeNumber);
                        episode2 += string.Format(multiTemplate, ep.EpisodeNumber);

                        count++;
                    }
                    episodeName = episode.EpisodeName;
                    episodeName = episodeName.TrimEnd();
                }
            }

            string episodeTemplate = Get.InOutCollection.EpisodeNamingTemplate;

            episodeTemplate = episodeTemplate.Replace(SeriesNameTemplate, seriesName);

            episodeTemplate = episodeTemplate.Replace(SeasonNumber1Template, season1);
            episodeTemplate = episodeTemplate.Replace(SeasonNumber2Template, season2);

            if (episodesContaining.Count <= 1)
            {
                episodeTemplate = episodeTemplate.Replace(MultiEpisodeFileTemplate, EpisodeMultiTemplate);
                episodeTemplate = episodeTemplate.Replace(EpisodeNumber1Template, episode1);
                episodeTemplate = episodeTemplate.Replace(EpisodeNumber2Template, episode2);
            }
            else
            {
                if (EpisodeMultiTemplate.Contains(EpisodeNumber1Template))
                {
                    episodeTemplate = episodeTemplate.Replace(MultiEpisodeFileTemplate, episode1);
                }
                else
                {
                    episodeTemplate = episodeTemplate.Replace(MultiEpisodeFileTemplate, episode2);
                }
            }

            episodeTemplate = episodeTemplate.Replace(EpisodeNameTemplate, episodeName);

            if (episode != null)
            {
                if (doRename && !dummy)
                {
                    string newPath = DoRename(episode.FilePath.PathAndFilename, episodeTemplate);

                    if (!string.IsNullOrEmpty(newPath))
                    {
                        string pathAddition;

                        if (MovieNaming.IsBluRay(episode.FilePath.PathAndFilename))
                        {
                            pathAddition =
                                episode.FilePath.PathAndFilename.Replace(
                                    MovieNaming.GetBluRayPath(episode.FilePath.PathAndFilename) +
                                    MovieNaming.GetBluRayName(episode.FilePath.PathAndFilename),
                                    string.Empty);

                            episode.FilePath.PathAndFilename = newPath + pathAddition;
                            DatabaseIOFactory.SetDatabaseDirty();
                        }
                        else if (MovieNaming.IsDVD(episode.FilePath.PathAndFilename))
                        {
                            pathAddition =
                                episode.FilePath.PathAndFilename.Replace(
                                    MovieNaming.GetDvdPath(episode.FilePath.PathAndFilename) +
                                    MovieNaming.GetDvdName(episode.FilePath.PathAndFilename),
                                    string.Empty);

                            episode.FilePath.PathAndFilename = newPath + pathAddition;
                            DatabaseIOFactory.SetDatabaseDirty();
                        }
                        else
                        {
                            episode.FilePath.PathAndFilename = newPath;
                            DatabaseIOFactory.SetDatabaseDirty();
                        }
                    }
                }
            }

            return(episodeTemplate);
        }
Example #10
0
        /// <summary>
        /// Process a file rename
        /// </summary>
        /// <param name="pathFrom">
        /// Path from.
        /// </param>
        /// <param name="renameTo">
        /// Rename too.
        /// </param>
        /// <returns>
        /// The remained path.
        /// </returns>
        public static string DoRename(string pathFrom, string renameTo)
        {
            renameTo = FileSystemCharChange.To(renameTo, FileSystemCharChange.ConvertArea.Tv);

            var fileName = Path.GetFileNameWithoutExtension(pathFrom);
            var filePath = Path.GetDirectoryName(pathFrom);

            // Bluray))
            if (MovieNaming.IsBluRay(pathFrom))
            {
                string folderPathFrom = MovieNaming.GetBluRayPath(pathFrom) + MovieNaming.GetBluRayName(pathFrom);
                string folderPathTo   = MovieNaming.GetBluRayPath(pathFrom) + renameTo;

                if (folderPathFrom != folderPathTo)
                {
                    try
                    {
                        Directory.Move(folderPathFrom, folderPathTo);
                        MasterMediaDBFactory.ChangeTvFileName(folderPathFrom, folderPathTo);
                        return(folderPathTo);
                    }
                    catch
                    {
                        return(string.Empty);
                    }
                }

                return(folderPathFrom);
            }

            // DVD
            if (MovieNaming.IsDVD(pathFrom))
            {
                string folderPathFrom = MovieNaming.GetDvdPath(pathFrom) + MovieNaming.GetDvdName(pathFrom);
                string folderPathTo   = MovieNaming.GetDvdPath(pathFrom) + renameTo;

                if (folderPathFrom != folderPathTo)
                {
                    try
                    {
                        Directory.Move(folderPathFrom, folderPathTo);
                        MasterMediaDBFactory.ChangeTvFileName(folderPathFrom, folderPathTo);
                        return(folderPathTo);
                    }
                    catch
                    {
                        return(string.Empty);
                    }
                }

                return(folderPathFrom);
            }


            // File
            string pathTo = Path.GetDirectoryName(pathFrom) + Path.DirectorySeparatorChar + renameTo +
                            Path.GetExtension(pathFrom);

            foreach (var subExt in Get.InOutCollection.SubtitleExtentions)
            {
                var possibleFileName = filePath + Path.DirectorySeparatorChar + fileName + "." + subExt;

                if (File.Exists(possibleFileName))
                {
                    try
                    {
                        File.Move(possibleFileName, filePath + Path.DirectorySeparatorChar + renameTo + "." + subExt);
                    }
                    catch
                    {
                        Log.WriteToLog(LogSeverity.Error, 0, "Could not rename", possibleFileName + " -> " + filePath + Path.DirectorySeparatorChar + renameTo + subExt);
                    }
                }
            }

            try
            {
                File.Move(pathFrom, pathTo);
                MasterMediaDBFactory.ChangeTvFileName(pathFrom, pathTo);
            }
            catch
            {
                return(pathFrom);
            }

            return(pathTo);
        }
Example #11
0
        /// <summary>
        /// Renames the episode.
        /// </summary>
        /// <param name="episode">
        /// The episode.
        /// </param>
        /// <returns>
        /// The rename episode.
        /// </returns>
        public static string RenameEpisode(Episode episode)
        {
            string seriesName;

            string season1 = string.Empty;
            string season2 = string.Empty;

            string episode1 = string.Empty;
            string episode2 = string.Empty;

            string episodeName = string.Empty;

            bool doRename;

            if (episode == null)
            {
                seriesName = "Star Trek: Deep Space Nine";

                season1  = "S2";
                season2  = "S02";
                episode1 = "E5";
                episode2 = "E05";

                episodeName = "Cardassians";
                doRename    = false;
            }
            else
            {
                episode1 = string.Empty;
                episode2 = string.Empty;

                seriesName = episode.GetSeriesName();
                int?seasonNumber = episode.SeasonNumber;

                List <Episode> episodesContaining = GetEpisodesContainingFile(episode);

                season1 = "S" + seasonNumber;
                season2 = string.Format("S{0:00}", seasonNumber);

                doRename = false;

                if (episodesContaining.Count == 1)
                {
                    episode1    = "E" + episode.EpisodeNumber;
                    episode2    = "E" + string.Format("{0:00}", episode.EpisodeNumber);
                    doRename    = true;
                    episodeName = episode.EpisodeName;
                }
                else
                {
                    int count = 0;

                    foreach (Episode ep in episodesContaining)
                    {
                        if (ep.EpisodeNumber == episode.EpisodeNumber && count == 0)
                        {
                            doRename = true;
                        }

                        episode1 += "E" + ep.EpisodeNumber;
                        episode2 += "E" + string.Format("{0:00}", ep.EpisodeNumber);

                        episodeName += string.Format("{0} ", ep.EpisodeName);
                        count++;
                    }
                    episodeName = episodeName.TrimEnd();
                }
            }

            string episodeTemplate = Get.InOutCollection.EpisodeNamingTemplate;

            episodeTemplate = episodeTemplate.Replace(SeriesNameTemplate, seriesName);

            episodeTemplate = episodeTemplate.Replace(SeasonNumber1Template, season1);
            episodeTemplate = episodeTemplate.Replace(SeasonNumber2Template, season2);

            episodeTemplate = episodeTemplate.Replace(EpisodeNumber1Template, episode1);
            episodeTemplate = episodeTemplate.Replace(EpisodeNumber2Template, episode2);

            episodeTemplate = episodeTemplate.Replace(EpisodeNameTemplate, episodeName);

            if (episode != null)
            {
                if (doRename)
                {
                    string newPath = DoRename(episode.FilePath.FileNameAndPath, episodeTemplate);

                    if (!string.IsNullOrEmpty(newPath))
                    {
                        string pathAddition;

                        if (MovieNaming.IsBluRay(episode.FilePath.FileNameAndPath))
                        {
                            pathAddition =
                                episode.FilePath.FileNameAndPath.Replace(
                                    MovieNaming.GetBluRayPath(episode.FilePath.FileNameAndPath) +
                                    MovieNaming.GetBluRayName(episode.FilePath.FileNameAndPath),
                                    string.Empty);

                            episode.FilePath.FileNameAndPath = newPath + pathAddition;
                        }
                        else if (MovieNaming.IsDVD(episode.FilePath.FileNameAndPath))
                        {
                            pathAddition =
                                episode.FilePath.FileNameAndPath.Replace(
                                    MovieNaming.GetDvdPath(episode.FilePath.FileNameAndPath) +
                                    MovieNaming.GetDvdName(episode.FilePath.FileNameAndPath),
                                    string.Empty);

                            episode.FilePath.FileNameAndPath = newPath + pathAddition;
                        }
                        else
                        {
                            episode.FilePath.FileNameAndPath = newPath;
                        }
                    }
                }
            }

            return(episodeTemplate);
        }
Example #12
0
        /// <summary>
        /// Gets the episode details.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>The episode details.</returns>
        public static EpisodeDetails GetEpisodeDetails(string filePath)
        {
            string logCategory = "ImportTvFactory > GetEpisodeDetails";

            var episodeDetails = new EpisodeDetails
            {
                FilePath = filePath
            };

            string series;

            if (MovieNaming.IsDVD(filePath))
            {
                series = MovieNaming.GetDvdName(filePath);
            }
            else if (MovieNaming.IsBluRay(filePath))
            {
                series = MovieNaming.GetBluRayName(filePath);
            }
            else
            {
                series = Path.GetFileNameWithoutExtension(filePath);
            }

            InternalApps.Logs.Log.WriteToLog(
                LogSeverity.Debug, 0, string.Format("Getting episode details for: {0}", series), logCategory);

            // Check if dir structure is <root>/<series>/<season>/<episodes>
            string dir         = Directory.GetParent(filePath).Name;
            string seriesGuess = string.Empty;

            if (dir.StartsWith("season", true, System.Globalization.CultureInfo.CurrentCulture))
            {
                // Looks like it. Qualified series guess
                seriesGuess = Directory.GetParent(filePath).Parent.Name;
            }

            InternalApps.Logs.Log.WriteToLog(
                LogSeverity.Debug,
                0,
                string.Format("Qualified series name guess, based on folder: {0}", seriesGuess),
                logCategory);

            var regex = Regex.Match(
                series,
                "(?<seriesName>.*?)" + DefaultRegex.Tv,
                RegexOptions.IgnoreCase);

            string rawSeriesName;

            if (regex.Success)
            {
                rawSeriesName = regex.Groups["seriesName"].Value.Trim();
                InternalApps.Logs.Log.WriteToLog(
                    LogSeverity.Debug, 0, string.Format("Series name from file: {0}", rawSeriesName), logCategory);

                var result = (from r in ScanSeriesPicks where r.SearchString == rawSeriesName select r)
                             .SingleOrDefault();

                string rawSeriesGuess = rawSeriesName.Replace(new string[] { ".", "_", "-" }, " ").Trim();

                if (seriesGuess != string.Empty)
                {
                    if (rawSeriesName == string.Empty)
                    {
                        // Anything's better than nothing
                        rawSeriesName = seriesGuess;
                    }
                    else if (rawSeriesName.StartsWith(seriesGuess))
                    {
                        // Regex might've picked up some random crap between series name and s01e01
                        rawSeriesName = seriesGuess;
                    }
                    else if (rawSeriesName.Length > 0 && (rawSeriesName.Substring(0, 1) == seriesGuess.Substring(0, 1) ||
                                                          rawSeriesGuess.ToLower().Replace("and", "&") == seriesGuess.ToLower().Replace("and", "&")))
                    {
                        // Regex might've picked up an acronym for the series
                        // TGaaG = Two Guys and a Girl
                        if (result == null)
                        {
                            rawSeriesName = seriesGuess;
                        }
                    }
                }
                else
                {
                    rawSeriesName = rawSeriesGuess;
                }

                InternalApps.Logs.Log.WriteToLog(
                    LogSeverity.Debug, 0, string.Format("Best series guess: {0}", rawSeriesName), logCategory);

                episodeDetails.SeriesName = result != null ? result.SeriesName : rawSeriesName;

                episodeDetails.SeriesName = FileSystemCharChange.From(episodeDetails.SeriesName, FileSystemCharChange.ConvertArea.Tv);
            }

            var seasonMatch = Regex.Match(series, DefaultRegex.TvSeason, RegexOptions.IgnoreCase);

            if (seasonMatch.Success)
            {
                episodeDetails.SeasonNumber = seasonMatch.Groups[1].Value.GetNumber();
            }

            InternalApps.Logs.Log.WriteToLog(
                LogSeverity.Debug,
                0,
                string.Format("Extracted season number: {0}", episodeDetails.SeasonNumber),
                logCategory);

            var episodeMatch = Regex.Matches(series, DefaultRegex.TvEpisode, RegexOptions.IgnoreCase);

            if (episodeMatch.Count > 0)
            {
                episodeDetails.TvMatchSuccess = true;
                if (episodeMatch.Count > 1)
                {
                    bool first = true;
                    foreach (Match match in episodeMatch)
                    {
                        if (first)
                        {
                            episodeDetails.EpisodeNumber = match.Value.GetNumber();
                            first = false;
                        }
                        else
                        {
                            episodeDetails.SecondaryNumbers.Add(match.Value.GetNumber());
                        }
                    }

                    InternalApps.Logs.Log.WriteToLog(
                        LogSeverity.Debug,
                        0,
                        string.Format(
                            "Extracted episode numbers ({0}): {1}",
                            episodeDetails.SecondaryNumbers.Count,
                            string.Join(", ", episodeDetails.SecondaryNumbers)),
                        logCategory);
                }
                else
                {
                    var episodeMatch2 = Regex.Match(series, DefaultRegex.TvEpisode, RegexOptions.IgnoreCase);
                    episodeDetails.EpisodeNumber = episodeMatch2.Groups[1].Value.GetNumber();

                    InternalApps.Logs.Log.WriteToLog(
                        LogSeverity.Debug,
                        0,
                        string.Format("Extracted episode number: {0}", episodeDetails.EpisodeNumber),
                        logCategory);
                }
            }

            episodeDetails.SeriesName = episodeDetails.SeriesName.Replace(".", string.Empty).Trim();
            if (episodeDetails.SeriesName.EndsWith("-"))
            {
                episodeDetails.SeriesName = episodeDetails.SeriesName.TrimEnd('-').Trim();
            }

            var check =
                (from s in ScanSeriesPicks where s.SearchString == episodeDetails.SeriesName select s).SingleOrDefault();

            if (check != null)
            {
                episodeDetails.SeriesName = check.SeriesName;
            }

            InternalApps.Logs.Log.WriteToLog(
                LogSeverity.Debug, 0, string.Format("Final series name: {0}", episodeDetails.SeriesName), logCategory);

            return(episodeDetails);
        }
Example #13
0
        /// <summary>
        /// Gets the episode details.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>The episode details.</returns>
        public static EpisodeDetails GetEpisodeDetails(string filePath)
        {
            var episodeDetails = new EpisodeDetails
            {
                FilePath = filePath
            };

            string series;

            if (MovieNaming.IsDVD(filePath))
            {
                series = MovieNaming.GetDvdName(filePath);
            }
            else if (MovieNaming.IsBluRay(filePath))
            {
                series = MovieNaming.GetBluRayName(filePath);
            }
            else
            {
                series = Path.GetFileNameWithoutExtension(filePath);
            }

            var regex = Regex.Match(
                series,
                "(?<seriesName>.*?)" + DefaultRegex.Tv,
                RegexOptions.IgnoreCase);

            if (regex.Success)
            {
                var rawSeriesName = regex.Groups["seriesName"].Value.Trim();

                var result = (from r in ScanSeriesPicks where r.SearchString == rawSeriesName select r)
                             .SingleOrDefault();

                episodeDetails.SeriesName = result != null ? result.SeriesName : rawSeriesName;

                episodeDetails.SeriesName = Tools.Restructure.FileSystemCharChange.From(episodeDetails.SeriesName);
            }

            var fileMatch = Regex.Match(series, DefaultRegex.Tv, RegexOptions.IgnoreCase);

            if (fileMatch.Success)
            {
                episodeDetails.TvMatchSuccess = true;

                var ep = fileMatch.Groups[2].Value;
                if (string.IsNullOrEmpty(ep))
                {
                    ep = fileMatch.Groups[3].Value;
                }

                var episodeNumberString = ep;
                var seriesNumberString  = fileMatch.Groups[1].Value;

                episodeDetails.SeasonNumber  = seriesNumberString.GetNumber();
                episodeDetails.EpisodeNumber = episodeNumberString.GetNumbers()[0];

                var episodeNumbers = episodeNumberString.GetNumbers();

                if (episodeNumbers.Count > 1)
                {
                    for (var i = 1; i < episodeNumbers.Count; i++)
                    {
                        episodeDetails.SecondaryNumbers.Add(episodeNumbers[i]);
                    }
                }
            }

            episodeDetails.SeriesName = episodeDetails.SeriesName.Replace(".", string.Empty).Trim();
            if (episodeDetails.SeriesName.EndsWith("-"))
            {
                episodeDetails.SeriesName = episodeDetails.SeriesName.TrimEnd('-').Trim();
            }

            var check =
                (from s in ScanSeriesPicks where s.SearchString == episodeDetails.SeriesName select s).SingleOrDefault();

            if (check != null)
            {
                episodeDetails.SeriesName = check.SeriesName;
            }

            return(episodeDetails);
        }
Example #14
0
        public static string TvSeason(Season season, string replace, string fromFile, string altFirstEpisode = null)
        {
            string seriesName           = "test show";
            string firstEpisodeFullPath = @"c:\test show\season 1\test show.s01e01.avi";
            bool   settings             = false;


            if (season == null)
            {
                season = new Season();
                season.Episodes.Add(new Episode {
                    FilePath = new MediaModel {
                        PathAndFilename = @"c:\test show\season 1\test show.s01e01.avi"
                    }
                });
                settings = true;
            }

            string firstEpisodeOfSeasonPath;
            string firstEpisodeOfSeason;

            if (altFirstEpisode != null)
            {
                if (altFirstEpisode == string.Empty)
                {
                    return(string.Empty);
                }

                if (MovieNaming.IsDVD(altFirstEpisode))
                {
                    firstEpisodeFullPath     = MovieNaming.GetDvdPath(altFirstEpisode) + MovieNaming.GetDvdName(altFirstEpisode);
                    firstEpisodeOfSeasonPath = MovieNaming.GetDvdPath(altFirstEpisode);
                    firstEpisodeOfSeason     = MovieNaming.GetDvdName(altFirstEpisode);
                }
                else if (MovieNaming.IsBluRay(altFirstEpisode))
                {
                    firstEpisodeFullPath     = MovieNaming.GetBluRayPath(altFirstEpisode) + MovieNaming.GetBluRayName(altFirstEpisode);
                    firstEpisodeOfSeasonPath = MovieNaming.GetBluRayPath(altFirstEpisode);
                    firstEpisodeOfSeason     = MovieNaming.GetBluRayName(altFirstEpisode);
                }
                else
                {
                    firstEpisodeFullPath     = altFirstEpisode;
                    firstEpisodeOfSeasonPath = Path.GetDirectoryName(altFirstEpisode);
                    firstEpisodeOfSeason     = Path.GetFileNameWithoutExtension(altFirstEpisode);
                }

                seriesName = Regex.Match(Path.GetFileNameWithoutExtension(firstEpisodeFullPath), "(?<seriesName>.*?)" + Settings.ConstSettings.DefaultRegex.Tv, RegexOptions.IgnoreCase).Groups["seriesName"].Value.Trim();
            }
            else
            {
                if (!settings)
                {
                    firstEpisodeFullPath = season.GetFirstEpisode();
                    seriesName           = Restructure.FileSystemCharChange.To(season.GetSeries().SeriesName, FileSystemCharChange.ConvertArea.Tv);
                }

                if (MovieNaming.IsDVD(firstEpisodeFullPath))
                {
                    firstEpisodeOfSeasonPath = MovieNaming.GetDvdPath(firstEpisodeFullPath);
                    firstEpisodeOfSeason     = MovieNaming.GetDvdName(firstEpisodeFullPath);
                }
                else if (MovieNaming.IsBluRay(firstEpisodeFullPath))
                {
                    firstEpisodeOfSeasonPath = MovieNaming.GetBluRayPath(firstEpisodeFullPath);
                    firstEpisodeOfSeason     = MovieNaming.GetBluRayName(firstEpisodeFullPath);
                }
                else
                {
                    firstEpisodeOfSeasonPath = Path.GetDirectoryName(firstEpisodeFullPath);
                    firstEpisodeOfSeason     = Path.GetFileNameWithoutExtension(firstEpisodeFullPath);
                }
            }

            var firstEpisodePath = Path.GetDirectoryName(firstEpisodeFullPath);

            replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesName, seriesName);
            replace = replace.Replace(Settings.Get.InOutCollection.TvFirstEpisodePathOfSeries, firstEpisodePath);
            replace = replace.Replace(Settings.Get.InOutCollection.TvFirstEpisodeOfSeasonPath, firstEpisodeOfSeasonPath);
            replace = replace.Replace(Settings.Get.InOutCollection.TvFirstEpisodeOfSeason, firstEpisodeOfSeason);
            replace = replace.Replace(Settings.Get.InOutCollection.TvSeasonNumber, season.SeasonNumber.ToString());
            replace = replace.Replace(Settings.Get.InOutCollection.TvSeasonNumber2, string.Format("{0:d2}", season.SeasonNumber));

            if (settings)
            {
                replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesPath, @"c:\testshow\season 1\");
            }
            else
            {
                replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesPath, season.GetSeries().GetSeriesPath());
            }

            return(replace + Path.GetExtension(fromFile));;
        }
Example #15
0
        public static string TvSeries(Series series, string replace, string fromFile, string altFirstEpisode = null)
        {
            if (series == null)
            {
                series            = new Series();
                series.SeriesName = "Test series";
                series.Seasons.Add(1, new Season());
                series.Seasons[1].Episodes.Add(new Episode {
                    FilePath = new MediaModel {
                        PathAndFilename = @"c:\testshow\season 1\test show.s01e01.avi"
                    }
                });
            }

            string firstEpisodeFullPath;
            string seriesName;

            if (altFirstEpisode != null)
            {
                if (altFirstEpisode == string.Empty)
                {
                    return(string.Empty);
                }

                if (MovieNaming.IsDVD(altFirstEpisode))
                {
                    firstEpisodeFullPath = MovieNaming.GetDvdPath(altFirstEpisode) + MovieNaming.GetDvdName(altFirstEpisode);
                }
                else if (MovieNaming.IsBluRay(altFirstEpisode))
                {
                    firstEpisodeFullPath = MovieNaming.GetBluRayPath(altFirstEpisode) + MovieNaming.GetBluRayName(altFirstEpisode);
                }
                else
                {
                    firstEpisodeFullPath = altFirstEpisode;
                }

                seriesName = Regex.Match(
                    Path.GetFileNameWithoutExtension(firstEpisodeFullPath),
                    "(?<seriesName>.*?)" + Settings.ConstSettings.DefaultRegex.Tv,
                    RegexOptions.IgnoreCase).Groups["seriesName"].Value.Trim();
            }
            else
            {
                firstEpisodeFullPath = series.GetFirstEpisode();

                seriesName = Tools.Restructure.FileSystemCharChange.To(series.SeriesName, FileSystemCharChange.ConvertArea.Tv);
            }

            string firstEpisodePath;

            if (MovieNaming.IsDVD(firstEpisodeFullPath))
            {
                firstEpisodePath = MovieNaming.GetDvdPath(firstEpisodeFullPath);
            }
            else if (MovieNaming.IsBluRay(firstEpisodeFullPath))
            {
                firstEpisodePath = MovieNaming.GetBluRayPath(firstEpisodeFullPath);
            }
            else
            {
                firstEpisodePath = Path.GetDirectoryName(firstEpisodeFullPath);
            }

            replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesName, seriesName);
            replace = replace.Replace(Settings.Get.InOutCollection.TvFirstEpisodePathOfSeries, firstEpisodePath);
            replace = replace.Replace(Settings.Get.InOutCollection.TvSeriesPath, series.GetSeriesPath());

            return(replace + Path.GetExtension(fromFile));
        }
Example #16
0
        /// <summary>
        /// Saves the series.
        /// </summary>
        /// <param name="series">The series.</param>
        /// <param name="type">The SeriesIOType type.</param>
        public void SaveSeries(Series series, SeriesIOType type)
        {
            string path = series.GetSeriesPath();

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            string nfoTemplate;
            string posterTemplate;
            string fanartTemplate;
            string bannerTemplate;

            string firstEpisodePath = series.GetFirstEpisode();

            TvRenamerFactory.RenameSeries(series);

            if (MovieNaming.IsBluRay(firstEpisodePath))
            {
                nfoTemplate    = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesNfoTemplate;
                posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesPosterTemplate;
                fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesFanartTemplate;
                bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesBannerTemplate;
            }
            else if (MovieNaming.IsDVD(firstEpisodePath))
            {
                nfoTemplate    = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesNfoTemplate;
                posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesPosterTemplate;
                fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesFanartTemplate;
                bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesBannerTemplate;
            }
            else
            {
                nfoTemplate    = Get.InOutCollection.CurrentTvSaveSettings.SeriesNfoTemplate;
                posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesPosterTemplate;
                fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesFanartTemplate;
                bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesBannerTemplate;
            }

            if (type == SeriesIOType.All || type == SeriesIOType.Nfo)
            {
                // Nfo
                string nfoPathTo = GeneratePath.TvSeries(series, nfoTemplate);

                this.WriteNFO(this.GenerateSeriesOutput(series), nfoPathTo);
                series.ChangedText = false;
            }

            // Poster
            if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Poster)
            {
                if (!string.IsNullOrEmpty(series.PosterUrl))
                {
                    string posterPathFrom;

                    if (!string.IsNullOrEmpty(series.PosterPath) && File.Exists(series.PosterPath))
                    {
                        posterPathFrom = series.PosterPath;
                    }
                    else
                    {
                        posterPathFrom = this.TvPathImageGet(series.PosterUrl);
                    }

                    string posterPathTo = GeneratePath.TvSeries(series, posterTemplate);

                    this.CopyFile(posterPathFrom, posterPathTo + ".jpg");
                    series.ChangedPoster = false;
                }
            }

            // Fanart
            if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Fanart)
            {
                if (!string.IsNullOrEmpty(series.FanartUrl))
                {
                    string fanartPathFrom;

                    if (!string.IsNullOrEmpty(series.FanartPath) && File.Exists(series.FanartPath))
                    {
                        fanartPathFrom = series.FanartPath;
                    }
                    else
                    {
                        fanartPathFrom = this.TvPathImageGet(series.FanartUrl);
                    }

                    string fanartPathTo = GeneratePath.TvSeries(series, fanartTemplate);

                    this.CopyFile(fanartPathFrom, fanartPathTo + ".jpg");
                    series.ChangedFanart = false;
                }
            }

            // Banner
            if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Banner)
            {
                if (!string.IsNullOrEmpty(series.SeriesBannerUrl))
                {
                    string bannerPathFrom;

                    if (!string.IsNullOrEmpty(series.SeriesBannerPath) && File.Exists(series.SeriesBannerPath))
                    {
                        bannerPathFrom = series.SeriesBannerPath;
                    }
                    else
                    {
                        bannerPathFrom = this.TvPathImageGet(series.SeriesBannerUrl);
                    }

                    string bannerPathTo = GeneratePath.TvSeries(series, bannerTemplate);

                    this.CopyFile(bannerPathFrom, bannerPathTo + ".jpg");
                    series.ChangedBanner = false;
                }
            }
        }
Example #17
0
        /// <summary>
        /// Saves the movie.
        /// </summary>
        /// <param name="movieModel">
        /// The movie model.
        /// </param>
        public void SaveMovie(MovieModel movieModel)
        {
            string actualTrailerFileName    = "";
            string actualTrailerFileNameExt = "";
            string actualFilePath           = movieModel.AssociatedFiles.Media[0].FileModel.Path;
            string actualFileName           = movieModel.AssociatedFiles.Media[0].FileModel.FilenameWithOutExt;
            string currentTrailerUrl        = movieModel.CurrentTrailerUrl;

            MovieSaveSettings movieSaveSettings = Get.InOutCollection.CurrentMovieSaveSettings;

            string nfoPath = string.IsNullOrEmpty(movieSaveSettings.NfoPath)
                                 ? actualFilePath
                                 : movieSaveSettings.NfoPath;

            string posterPath = Downloader.ProcessDownload(
                movieModel.CurrentPosterImageUrl, DownloadType.Binary, Section.Movies);

            string fanartPathFrom = Downloader.ProcessDownload(
                movieModel.CurrentFanartImageUrl, DownloadType.Binary, Section.Movies);

            string trailerPathFrom = Downloader.ProcessDownload(
                movieModel.CurrentTrailerUrl, DownloadType.AppleBinary, Section.Movies);

            string nfoXml = GenerateOutput.GenerateMovieOutput(movieModel);

            string nfoTemplate;
            string posterTemplate;
            string fanartTemplate;
            string trailerTemplate;
            string setPosterTemplate;
            string setFanartTemplate;

            if (MovieNaming.IsDVD(movieModel.GetBaseFilePath))
            {
                actualFilePath = MovieNaming.GetDvdPath(movieModel.GetBaseFilePath);
                actualFileName = MovieNaming.GetDvdName(movieModel.GetBaseFilePath);

                nfoTemplate       = movieSaveSettings.DvdNfoNameTemplate;
                posterTemplate    = movieSaveSettings.DvdPosterNameTemplate;
                fanartTemplate    = movieSaveSettings.DvdFanartNameTemplate;
                trailerTemplate   = movieSaveSettings.DvdTrailerNameTemplate;
                setPosterTemplate = movieSaveSettings.DvdSetPosterNameTemplate;
                setFanartTemplate = movieSaveSettings.DvdSetFanartNameTemplate;
            }
            else if (MovieNaming.IsBluRay(movieModel.GetBaseFilePath))
            {
                actualFilePath = MovieNaming.GetBluRayPath(movieModel.GetBaseFilePath);
                actualFileName = MovieNaming.GetBluRayName(movieModel.GetBaseFilePath);

                nfoTemplate       = movieSaveSettings.BlurayNfoNameTemplate;
                posterTemplate    = movieSaveSettings.BlurayPosterNameTemplate;
                fanartTemplate    = movieSaveSettings.BlurayFanartNameTemplate;
                trailerTemplate   = movieSaveSettings.BlurayTrailerNameTemplate;
                setPosterTemplate = movieSaveSettings.BluraySetPosterNameTemplate;
                setFanartTemplate = movieSaveSettings.BluraySetFanartNameTemplate;
            }
            else
            {
                nfoTemplate       = movieSaveSettings.NormalNfoNameTemplate;
                posterTemplate    = movieSaveSettings.NormalPosterNameTemplate;
                fanartTemplate    = movieSaveSettings.NormalFanartNameTemplate;
                trailerTemplate   = movieSaveSettings.NormalTrailerNameTemplate;
                setPosterTemplate = movieSaveSettings.NormalSetPosterNameTemplate;
                setFanartTemplate = movieSaveSettings.NormalSetFanartNameTemplate;
            }

            if (!string.IsNullOrEmpty(currentTrailerUrl))
            {
                actualTrailerFileName    = currentTrailerUrl.Substring(currentTrailerUrl.LastIndexOf('/') + 1, currentTrailerUrl.LastIndexOf('.') - currentTrailerUrl.LastIndexOf('/') - 1);
                actualTrailerFileNameExt = currentTrailerUrl.Substring(currentTrailerUrl.LastIndexOf('.') + 1);
            }

            string nfoOutputName = nfoTemplate.Replace("<path>", actualFilePath).Replace("<filename>", actualFileName);

            string posterOutputName =
                posterTemplate.Replace("<path>", actualFilePath).Replace("<filename>", actualFileName).Replace(
                    "<ext>", "jpg");

            string fanartOutputName =
                fanartTemplate.Replace("<path>", actualFilePath).Replace("<filename>", actualFileName).Replace(
                    "<ext>", "jpg");

            string trailerOutputName =
                trailerTemplate.Replace("<path>", actualFilePath).Replace("<filename>", actualFileName).Replace(
                    "<trailername>", actualTrailerFileName).Replace("<ext>", actualTrailerFileNameExt);

            string setPosterOutputPath = setPosterTemplate.Replace("<path>", actualFilePath).Replace(
                "<filename>", actualFileName);

            string setFanartOutputPath = setFanartTemplate.Replace("<path>", actualFilePath).Replace(
                "<filename>", actualFileName);

            // Handle Set Images
            List <string> sets = MovieSetManager.GetSetsContainingMovie(movieModel);

            if (sets.Count > 0)
            {
                foreach (string setName in sets)
                {
                    MovieSetModel set = MovieSetManager.GetSet(setName);

                    MovieSetObjectModel setObjectModel =
                        (from s in set.Movies where s.MovieUniqueId == movieModel.MovieUniqueId select s).
                        SingleOrDefault();

                    string currentSetPosterPath = setPosterOutputPath.Replace("<setname>", setName).Replace(
                        "<ext>", ".jpg");

                    string currentSetFanartPath = setFanartOutputPath.Replace("<setname>", setName).Replace(
                        "<ext>", ".jpg");

                    if (setObjectModel.Order == 1)
                    {
                        if (File.Exists(set.PosterUrl))
                        {
                            File.Copy(set.PosterUrl, currentSetPosterPath);
                        }

                        if (File.Exists(set.FanartUrl))
                        {
                            File.Copy(set.FanartUrl, currentSetFanartPath);
                        }
                    }
                    else
                    {
                        if (File.Exists(set.PosterUrl) && File.Exists(currentSetPosterPath))
                        {
                            File.Delete(currentSetPosterPath);
                        }

                        if (File.Exists(set.FanartUrl) && File.Exists(currentSetFanartPath))
                        {
                            File.Delete(currentSetFanartPath);
                        }
                    }
                }
            }

            if (movieSaveSettings.IoType == MovieIOType.All || movieSaveSettings.IoType == MovieIOType.Nfo)
            {
                try
                {
                    this.WriteNFO(nfoXml, nfoOutputName);
                    movieModel.ChangedText = false;
                    Log.WriteToLog(
                        LogSeverity.Info, 0, "NFO Saved To Disk for " + movieModel.Title, nfoPath + nfoOutputName);
                }
                catch (Exception ex)
                {
                    Log.WriteToLog(LogSeverity.Error, 0, "Saving NFO Failed for " + movieModel.Title, ex.Message);
                }
            }

            if (movieSaveSettings.IoType == MovieIOType.All || movieSaveSettings.IoType == MovieIOType.Poster ||
                movieSaveSettings.IoType == MovieIOType.Images)
            {
                try
                {
                    if (!string.IsNullOrEmpty(movieModel.CurrentPosterImageUrl))
                    {
                        this.CopyFile(posterPath, posterOutputName);
                        movieModel.ChangedPoster = false;
                        Log.WriteToLog(
                            LogSeverity.Info,
                            0,
                            "Poster Saved To Disk for " + movieModel.Title,
                            posterPath + " -> " + posterOutputName);
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteToLog(LogSeverity.Error, 0, "Saving Poster Failed for " + movieModel.Title, ex.Message);
                }
            }

            if (movieSaveSettings.IoType == MovieIOType.All || movieSaveSettings.IoType == MovieIOType.Fanart ||
                movieSaveSettings.IoType == MovieIOType.Images)
            {
                try
                {
                    if (!string.IsNullOrEmpty(movieModel.CurrentFanartImageUrl))
                    {
                        this.CopyFile(fanartPathFrom, fanartOutputName);
                        movieModel.ChangedFanart = false;
                        Log.WriteToLog(
                            LogSeverity.Info,
                            0,
                            "Fanart Saved To Disk for " + movieModel.Title,
                            fanartPathFrom + " -> " + fanartOutputName);
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteToLog(LogSeverity.Error, 0, "Saving Fanart Failed for " + movieModel.Title, ex.Message);
                }
            }

            if (movieSaveSettings.IoType == MovieIOType.All || movieSaveSettings.IoType == MovieIOType.Trailer)
            {
                try
                {
                    if (!string.IsNullOrEmpty(movieModel.CurrentTrailerUrl))
                    {
                        this.CopyFile(trailerPathFrom, trailerOutputName);
                        movieModel.ChangedTrailer = false;
                        Log.WriteToLog(
                            LogSeverity.Info,
                            0,
                            "Trailer Saved To Disk for " + movieModel.Title,
                            trailerPathFrom + " -> " + trailerOutputName);
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteToLog(LogSeverity.Error, 0, "Saving Trailer Failed for " + movieModel.Title, ex.Message);
                }
            }
        }
Example #18
0
        /// <summary>
        /// Converts the media path import database into a MovieModel DB.
        /// </summary>
        public static void ConvertMediaPathImportToDB()
        {
            cancelImport = false;

            var db = MediaPathDBFactory.GetMediaPathMoviesUnsorted();

            var count = 0;

            MovieDBFactory.ImportProgressMaximum = db.Count;

            ImportDatabase.Clear();
            ImportDuplicatesDatabase.Clear();

            var getFiles            = new string[1];
            var currentGetPathFiles = string.Empty;

            UI.Windows7UIFactory.StartProgressState(db.Count);

            foreach (var file in db)
            {
                if (cancelImport)
                {
                    break;
                }

                MovieDBFactory.ImportProgressCurrent = count;

                MovieDBFactory.ImportProgressStatus = string.Format("Processing: " + file.PathAndFileName.Replace("{", "{{").Replace("}", "}}"));

                if (file.Path != currentGetPathFiles)
                {
                    getFiles            = FileHelper.GetFilesRecursive(file.Path, "*.*").ToArray();
                    currentGetPathFiles = file.Path;
                }

                var videoSource = file.DefaultVideoSource;

                if (MovieNaming.IsBluRay(file.PathAndFileName))
                {
                    videoSource = "Bluray";
                }
                else if (MovieNaming.IsDVD(file.PathAndFileName))
                {
                    videoSource = "DVD";
                }
                else
                {
                    var detect = Tools.IO.DetectType.FindVideoSource(file.PathAndFileName);

                    if (!string.IsNullOrEmpty(detect))
                    {
                        videoSource = detect;
                    }
                }

                string title      = MovieNaming.GetMovieName(file.PathAndFileName, file.MediaPathType);
                var    movieModel = new MovieModel
                {
                    Title            = title,
                    Year             = MovieNaming.GetMovieYear(file.PathAndFileName),
                    ScraperGroup     = file.ScraperGroup,
                    VideoSource      = videoSource,
                    NfoPathOnDisk    = FindNFO(file.FilenameWithOutExt, FindFilePath(title, file), getFiles),
                    PosterPathOnDisk = FindPoster(file.FilenameWithOutExt, FindFilePath(title, file), getFiles),
                    FanartPathOnDisk = FindFanart(file.FilenameWithOutExt, FindFilePath(title, file), getFiles)
                };

                if (!string.IsNullOrEmpty(movieModel.NfoPathOnDisk))
                {
                    InOut.OutFactory.LoadMovie(movieModel);
                    movieModel.ChangedText = false;
                }

                var result = (from m in ImportDatabase where (m.Title.ToLower().Trim() == movieModel.Title.ToLower().Trim()) select m).ToList();

                if (result.Count == 0)
                {
                    if (!string.IsNullOrEmpty(movieModel.PosterPathOnDisk))
                    {
                        movieModel.GenerateSmallPoster(movieModel.PosterPathOnDisk);
                        movieModel.ChangedPoster = false;
                    }

                    if (!string.IsNullOrEmpty(movieModel.FanartPathOnDisk))
                    {
                        movieModel.GenerateSmallFanart(movieModel.FanartPathOnDisk);
                        movieModel.ChangedFanart = false;
                    }

                    movieModel.AssociatedFiles.AddToMediaCollection(file);

                    // Does the movie exist in our current DB?
                    var result2 = (from m in MovieDBFactory.MovieDatabase where (m.Title.ToLower().Trim() == movieModel.Title.ToLower().Trim()) select m).ToList();
                    if (result2.Count > 0)
                    {
                        if (movieModel.Year != null)
                        {
                            var r = (from m in result2 where m.Year == movieModel.Year select m).ToList();
                            if (r.Count > 0)
                            {
                                // We already have a movie with that name and year, mark as dupe
                                ImportDuplicatesDatabase.Add(movieModel);
                            }
                        }
                        else
                        {
                            // No year, so we can't ensure it's a dupe
                            ImportDuplicatesDatabase.Add(movieModel);
                        }
                    }

                    // Add it to the list anyway, since there's no implementation of any action on duplicates.
                    ImportDatabase.Add(movieModel);
                }
                else
                {
                    var r = (from m in result where m.Year == movieModel.Year select m).ToList();
                    if (Regex.IsMatch(file.PathAndFileName.ToLower(), @"(disc|disk|part|cd|vob|ifo|bup)", RegexOptions.IgnoreCase))
                    {
                        // Only associate with an existing movie if its not a dupe
                        result[0].AssociatedFiles.AddToMediaCollection(file);
                    }
                    else if (r.Count == 0)
                    {
                        // Same title, different year
                        ImportDatabase.Add(movieModel);
                    }
                    else
                    {
                        // Dont count a disc or part as a dupe or movies with different years
                        ImportDuplicatesDatabase.Add(movieModel);
                        // Add it to the list anyway, since there's no implementation of any action on duplicates.
                        ImportDatabase.Add(movieModel);
                    }
                }

                count++;
                UI.Windows7UIFactory.SetProgressValue(count);
            }

            UI.Windows7UIFactory.StopProgressState();
        }