Exemplo n.º 1
0
        public void UpdateMovieDatabase()
        {
            var moviesInDb = GetMovies();
            var moviesOnDisk = new HashSet<string>();
            var xmlFiles = Directory.GetFiles(@"M:\Movies", "*.xml", SearchOption.AllDirectories);

            // for movies on disk that aren't in the database, insert new records
            foreach (var xmlFile in xmlFiles)
            {
                var xelement = XElement.Load(xmlFile);
                var elements = xelement.Elements();
                var tMDbId = elements.SingleOrDefault(x => x.Name == "TMDbId").Value;
                moviesOnDisk.Add(tMDbId);

                if (moviesInDb.All(x => x.TMDbId != tMDbId))
                {
                    var iMDbId = elements.SingleOrDefault(x => x.Name == "IMDbId").Value;
                    var title = elements.SingleOrDefault(x => x.Name == "LocalTitle").Value;
                    var year = int.Parse(elements.SingleOrDefault(x => x.Name == "ProductionYear").Value);
                    var mPAARating = elements.SingleOrDefault(x => x.Name == "MPAARating").Value;
                    var overview = elements.SingleOrDefault(x => x.Name == "Overview").Value;
                    var language = elements.SingleOrDefault(x => x.Name == "Language").Value;
                    var runningTime = int.Parse(elements.SingleOrDefault(x => x.Name == "RunningTime").Value);
                    var fileSize = long.Parse(elements.SingleOrDefault(x => x.Name == "VideoFileSize").Value);
                    var quality = elements.SingleOrDefault(x => x.Name == "VideoQuality").Value;
                    var videoCodec = elements.SingleOrDefault(x => x.Name == "VideoCodec").Value;
                    var videoBitRate = long.Parse(elements.SingleOrDefault(x => x.Name == "VideoBitrate").Value);
                    var height = int.Parse(elements.SingleOrDefault(x => x.Name == "VideoHeight").Value);
                    var width = int.Parse(elements.SingleOrDefault(x => x.Name == "VideoWidth").Value);
                    var audioCodec = elements.SingleOrDefault(x => x.Name == "AudioCodec").Value;
                    var audioChannels = float.Parse(elements.SingleOrDefault(x => x.Name == "AudioChannels").Value);

                    var frameRate = float.Parse(elements.SingleOrDefault(x => x.Name == "MediaInfo").Elements()
                        .SingleOrDefault(x => x.Name == "Video").Elements()
                        .SingleOrDefault(x => x.Name == "FrameRate").Value
                    );

                    var rating = float.Parse(
                        !string.IsNullOrEmpty(elements.SingleOrDefault(x => x.Name == "IMDBrating").Value)
                            ? elements.SingleOrDefault(x => x.Name == "IMDBrating").Value
                            : elements.SingleOrDefault(x => x.Name == "Rating").Value
                    );

                    byte[] poster;
                    using (var stream = new MemoryStream())
                    {
                        var posterImage = (Image)(new Bitmap(Image.FromFile(Directory.GetParent(xmlFile) + "\\folder.jpg"), new Size(200, 300)));
                        posterImage.Save(stream, ImageFormat.Jpeg);
                        poster = stream.ToArray();
                    }

                    var movie = new Movie()
                    {
                        TMDbId = tMDbId,
                        IMDbId = iMDbId,
                        Title = title,
                        Year = year,
                        MPAARating = mPAARating,
                        Rating = rating,
                        Overview = overview,
                        Language = language,
                        Poster = poster,

                        MediaInfo = new MediaInfo
                        {
                            Id = Guid.NewGuid(),
                            RunningTime = runningTime,
                            FileSize = fileSize,
                            Quality = quality,
                            VideoCodec = videoCodec,
                            VideoBitRate = videoBitRate,
                            Height = height,
                            Width = width,
                            AudioCodec = audioCodec,
                            AudioChannels = audioChannels,
                            FrameRate = frameRate,
                        }
                    };

                    CreateMovie(movie);
                }
            }

            var movies = GetMovies();
        }
Exemplo n.º 2
0
 public void CreateMovie(Movie movie)
 {
     var movieEntity = Mapper.EMap(movie);
     MovieRepository.Add(movieEntity);
 }