public void CreateFiles(EntityBase entity, bool overwrite, UserConfiguration configuration)
        {
            Season season            = (Season)entity;
            string metadataDirectory = System.IO.Path.Combine(season.Path, "metadata");

            if (!Directory.Exists(metadataDirectory))
            {
                DirectoryInfo dir = Directory.CreateDirectory(metadataDirectory);
                dir.Attributes = FileAttributes.Hidden;
            }

            // Download banner images
            string bannerFilePath = string.Concat(season.Path, "\\banner.jpg");

            if (!File.Exists(bannerFilePath))
            {
                Library.Entities.Image image = this.GetBannerImage(season);
                if (image != null && !string.IsNullOrWhiteSpace(image.URL))
                {
                    EpisodeService.DownloadImage(image.URL, bannerFilePath);
                }
                else
                {
                    TraceManager.TraceFormat("Unable to find a suitable banner image for {0}.", season.Name);
                }
            }

            foreach (Episode episode in season.Episodes)
            {
                if (!string.IsNullOrEmpty(episode.Path))
                {
                    new EpisodeService().CreateFiles(episode, overwrite, configuration);
                }
            }
        }
 public void SetPosterImage(Movie movie, Image posterImage)
 {
     // Fetch the poster
     if (posterImage != null)
     {
         string posterFile = System.IO.Path.Combine(movie.Path, "folder.jpg");
         TraceManager.Trace("Downloading poster image ...", TraceTypes.OperationStarted);
         EpisodeService.DownloadImage(posterImage.URL, posterFile);
     }
     else
     {
         TraceManager.Trace(string.Format("Unable to find a poster file for {0}.", movie.Name), TraceTypes.Error);
     }
 }
        public void CreateFiles(EntityBase entity, bool overwrite, UserConfiguration configuration)
        {
            Episode       episode           = (Episode)entity;
            DirectoryInfo seasonDirectory   = new DirectoryInfo(episode.Parent.Path);
            DirectoryInfo metadataDirectory = new DirectoryInfo(System.IO.Path.Combine(seasonDirectory.FullName, "metadata"));

            if (seasonDirectory.Exists)
            {
                XmlDocument doc              = new XmlDocument();
                string      episodeName      = this.GetIdentifierAndName(episode, true, true);
                string      episodeImageName = string.Concat(episodeName, ".jpg");
                string      episodeXmlName   = string.Concat(episodeName, ".xml");
                string      episodeFilePath  = System.IO.Path.Combine(metadataDirectory.FullName, episodeXmlName);

                if (overwrite && File.Exists(episodeFilePath))
                {
                    File.Delete(episodeFilePath);
                }
                FileInfo episodeFile = new FileInfo(episodeFilePath);
                XmlNode  rootNode    = null;

                if (!episodeFile.Exists)
                {
                    Directory.CreateDirectory(episodeFile.DirectoryName);
                    rootNode = doc.AddNode("Item");
                }
                else
                {
                    doc.Load(episodeFilePath);
                    rootNode = doc.SelectSingleNode("//Item");
                }

                // Download the episode thumbnail image for the episode if it doesn't already exists.
                string imageFilePath = System.IO.Path.Combine(metadataDirectory.FullName, episodeImageName);
                if (!File.Exists(imageFilePath))
                {
                    Image thumbnail = this.GetThumbnailImage(episode);
                    if (thumbnail != null && !string.IsNullOrEmpty(thumbnail.URL))
                    {
                        TraceManager.TraceFormat("Downloading image {0} to {1}", thumbnail.URL, imageFilePath);
                        EpisodeService.DownloadImage(thumbnail.URL, imageFilePath);
                    }
                    else
                    {
                        TraceManager.Trace(string.Format("Unable to find thumbnail image for {0}", episode.Name), TraceTypes.Error);
                    }
                }

                // Rename the file if necessary
                string          currentEpisodeName = System.IO.Path.GetFileNameWithoutExtension(episode.Path);
                string          extension          = System.IO.Path.GetExtension(episode.Path);
                string          newPath            = string.Concat(System.IO.Path.Combine(seasonDirectory.FullName, episodeName), extension);
                MatchCollection matches            = Regex.Matches(currentEpisodeName, ShowService.SeasonEpisodeRegex, RegexOptions.IgnoreCase);

                if (currentEpisodeName != episodeName && matches.Count == 1)
                {
                    TraceManager.Trace(string.Format("Renaming {0} to {1}", episode.Path, newPath), TraceTypes.OperationStarted);
                    File.Move(episode.Path, newPath);
                }
                else if (matches.Count > 1)
                {
                    TraceManager.Trace(string.Format("Skipping renaming \"{0}\" to \"{1}\" because there were ({2}) season/episode numbers in the filename.", currentEpisodeName, episodeName, matches.Count.ToString()), TraceVerbosity.Minimal);
                }

                rootNode.AddNode("ID", episode.EpisodeNumber.ToString());
                rootNode.AddNode("EpisodeID", episode.ID.ToString());
                rootNode.AddNode("EpisodeName", episode.Name);
                rootNode.AddNode("EpisodeNumber", episode.EpisodeNumber.ToString());
                rootNode.AddNode("FirstAired", episode.Created.ToString("yyyy-MM-dd"));
                rootNode.AddNode("Overview", episode.Description);
                rootNode.AddNode("DVD_chapter", EpisodeService.WriteIntValue(episode.DVD_Chapter));
                rootNode.AddNode("DVD_discid", EpisodeService.WriteIntValue(episode.DVD_DiscID));
                rootNode.AddNode("DVD_episodenumber", EpisodeService.WriteDoubleValue(episode.DVD_EpisodeNumber));
                rootNode.AddNode("DVD_season", EpisodeService.WriteIntValue(episode.DVD_SeasonNumber));
                rootNode.AddNode("Director", episode.Director);
                rootNode.AddNode("GuestStars", string.Concat("|", string.Join("|", episode.GuestStars), "|"));
                rootNode.AddNode("IMDB_ID", episode.IMDB_ID);
                rootNode.AddNode("Language", episode.Language);
                rootNode.AddNode("ProductionCode", EpisodeService.WriteIntValue(episode.ProductionCode));
                rootNode.AddNode("Rating", EpisodeService.WriteDoubleValue(episode.Rating));
                rootNode.AddNode("Writer", episode.Writer);
                rootNode.AddNode("SeasonNumber", EpisodeService.WriteIntValue(episode.SeasonNumber));
                rootNode.AddNode("absolute_number");
                rootNode.AddNode("seasonid", EpisodeService.WriteIntValue(episode.SeasonID));
                rootNode.AddNode("seriesid", EpisodeService.WriteIntValue(episode.SeriesID));
                rootNode.AddNode("filename", string.Concat("/", episodeImageName));

                doc.Save(episodeFilePath);
            }
            else
            {
                throw new DirectoryNotFoundException(string.Format("Directory {0} was not found.", seasonDirectory.FullName));
            }
        }
        public void CreateFiles(EntityBase entity, bool overwrite, UserConfiguration configuration)
        {
            Show          show      = (Show)entity;
            DirectoryInfo directory = new DirectoryInfo(show.Path);

            if (directory.Exists)
            {
                string filePath = string.Concat(directory.FullName, "\\series.xml");
                if (File.Exists(filePath))
                {
                    if (overwrite)
                    {
                        File.Delete(filePath);
                    }
                    else
                    {
                        return;
                    }
                }

                XmlDocument doc      = new XmlDocument();
                XmlNode     rootNode = doc.AddNode("Series");
                rootNode.AddNode("id", show.ID.ToString());
                rootNode.AddNode("Actors", string.Concat("|", string.Join("|", show.Actors), "|"));
                rootNode.AddNode("ContentRating", show.ContentRating);
                rootNode.AddNode("FirstAired", show.Created.ToString("yyyy-MM-dd"));
                rootNode.AddNode("Genre", string.Concat("|", string.Join("|", show.Genres), "|"));
                rootNode.AddNode("IMDbId", show.IMDB_ID);
                rootNode.AddNode("IMDB_ID", show.IMDB_ID);
                rootNode.AddNode("Overview", show.Description);
                rootNode.AddNode("Network", show.Network);
                rootNode.AddNode("Rating", show.Rating);
                rootNode.AddNode("Runtime", show.Runtime.ToString());
                rootNode.AddNode("SeriesName", show.Name);
                rootNode.AddNode("Status", show.Status);

                // Save the file
                doc.Save(filePath);

                // Download banner images
                string bannerFilePath = System.IO.Path.Combine(show.Path, "banner.jpg");
                if (!File.Exists(bannerFilePath))
                {
                    Library.Entities.Image image = this.GetBannerImage(show);
                    if (image != null && !string.IsNullOrWhiteSpace(image.URL))
                    {
                        EpisodeService.DownloadImage(image.URL, bannerFilePath);
                    }
                    else
                    {
                        TraceManager.TraceFormat("Unable to find a suitable banner image for {0}.", show.Name);
                    }
                }

                // Download poster image
                string posterFile = System.IO.Path.Combine(show.Path, "folder.jpg");
                if (!File.Exists(posterFile))
                {
                    Image poster = show.Images.Where((i) => i.MappedType == ImageType.Poster).OrderByDescending((i) => i.Height).ToList().FirstOrDefault();
                    if (poster != null)
                    {
                        TraceManager.Trace("Downloading poster image ...", TraceTypes.OperationStarted);
                        EpisodeService.DownloadImage(poster.URL, posterFile);
                    }
                    else
                    {
                        TraceManager.Trace(string.Format("Unable to find a poster file for {0}.", show.Name), TraceTypes.Error);
                    }
                }

                foreach (Season season in show.Seasons)
                {
                    if (!string.IsNullOrEmpty(season.Path))
                    {
                        new SeasonService().CreateFiles(season, overwrite, configuration);
                    }
                }
            }
            else
            {
                throw new DirectoryNotFoundException(string.Format("Directory {0} was not found.", directory.FullName));
            }
        }