public void RememberEpisode(FileInfo fullName, Episode episode)
        {
            CachedEpisodesByFileName[fullName.FullName] = episode;

            SeriesFolderMappingWriter.Write(episode.Series.SeriesId, fullName.DirectoryName);

            SeasonWriter.Write(episode);

            EpisodeWriter.Save(episode);
        }
        public Episode CreateEpisodeFromLine(string line)
        {
            var episodeMatch = Regex.Match(line.Trim(), EPGuidesEpisodeRegex);

            if (episodeMatch.Success)
            {
                var episode = new Episode
                {
                    EPNum = episodeMatch.Groups[1].Value == String.Empty ? 1 : Convert.ToInt32(episodeMatch.Groups[1].Value),
                    SeasonNumber = episodeMatch.Groups[2].ToString().Contains("P") ? 1 : Convert.ToInt32(episodeMatch.Groups[2].Value),
                    EPInSeason = Convert.ToInt32(episodeMatch.Groups[3].Value),
                    ProductionNum = episodeMatch.Groups[4].Value,
                    AirDate = GetAirDateOrUnAired(episodeMatch.Groups[5].ToString()),
                    Title = HttpUtility.HtmlDecode(episodeMatch.Groups[6].Value)
                };

                return episode;
            }

            return null;
        }
        public void Save(Episode episode)
        {
            using(var connection = Connection.Get(DB.Directory))
            {
                const string sql = @"INSERT INTO Episode
                                    ( SeasonNumber, EpisodeNumberInSeries, EpisodeNumberInSeason,
                                    ProductionNumber, Title, AirDate, SeriesId )
                                    SELECT @0, @1, @2, @3, @4, @5, @6
                                    WHERE NOT EXISTS(
                                        SELECT * FROM Episode
                                        WHERE
                                            SeasonNumber = @0
                                            AND SeriesId = @6
                                            AND EpisodeNumberInSeason = @2
                                    )";

                connection.ExecuteNonQuery(sql, episode.SeasonNumber, episode.EPNum, episode.EPInSeason,
                                                episode.ProductionNum, episode.Title, episode.AirDate,
                                                episode.Series.SeriesId);
            }
        }
        /// <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;
        }
 public void Write(Episode episode)
 {
     Write(episode.Series.SeriesId, episode.SeasonNumber);
 }
 protected string GetNewFilename(Episode episode, EpisodeRenamerSettings settings)
 {
     return MagicFields.Aggregate(settings.RenamingFormula,
         (current, magicField) => current.Replace(magicField.Key, magicField.Value.Invoke(episode, settings)));
 }