public IHttpActionResult CreateSubscription([FromBody] int movieId)
        {
            try
            {
                var userId = User.Identity.GetUserId();

                var startSubscription = db.StartSalesSubscriptions.FirstOrDefault(p => p.MovieId == movieId && p.UserId == userId);

                if (startSubscription == null)
                {
                    var subscription = new StartSalesSubscription
                    {
                        MovieId = movieId,
                        UserId  = userId
                    };

                    db.StartSalesSubscriptions.Add(subscription);
                    db.SaveChanges();
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(new ExceptionResult(ex, this));
            }
        }
Exemple #2
0
        public static void CheckStartSubscriptionsSend()
        {
            try
            {
                using (BookingBreakerContext db = new BookingBreakerContext())
                {
                    var runningSalesMoviesIds     = db.ShowTimes.Select(p => p.MovieId).Distinct().ToList();
                    var startedSalesSubscriptions = db.StartSalesSubscriptions.Where(p => p.IsUserNotified == false && runningSalesMoviesIds.Contains(p.MovieId))
                                                    .Include(p => p.Movie).Include(p => p.User).ToList();

                    if (startedSalesSubscriptions.Count == 0)
                    {
                        _logger.Info("No subscriptions up to date");
                        return;
                    }

                    Parallel.ForEach(startedSalesSubscriptions, subscription =>
                    {
                        var isSent = MailService.DefaultInstance.SendEmail(subscription.User.Email, "Старт продаж билетов на " + subscription.Movie.Title,
                                                                           "<h1>Стартовали продажи билетов на " + subscription.Movie.Title + "! Не пропусти шанс занять лучшие места!</h1>");

                        subscription.IsUserNotified = isSent;
                        _logger.Info("Working with subscription - Movie: " + subscription.Movie.Title +
                                     ", UserEmail: " + subscription.User.Email + ", IsSent: " + isSent);
                    });

                    _logger.Info("Try save subscription status");
                    db.SaveChanges();
                    _logger.Info("Save subscription status changes");
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
            }
        }
        public static async Task ExecuteMovieParcing()
        {
            _logger.Info("Start Movie Parcing");

            try
            {
                var response = await _client.GetAsync(@"https://planetakino.ua/showtimes/xml");

                var responseString = await response.Content.ReadAsStringAsync();

                var xmlDoc    = XDocument.Parse(responseString);
                var films     = xmlDoc.Descendants("movies").Descendants("movie");
                var showtimes = xmlDoc.Descendants("showtimes").Descendants("day").Descendants("show");

                using (BookingBreakerContext db = new BookingBreakerContext())
                {
                    var cinema = GetParcingCinema(db);

                    foreach (var film in films)
                    {
                        var filmTitle = film.Descendants("title").First().Value;
                        var startDate = film.Descendants("dt-start").First().Value;
                        var endDate   = film.Descendants("dt-end").First().Value;

                        var cinemaMovieId   = film.Attribute("id").Value;
                        var cinemaMovieLink = film.Attribute("url").Value;



                        var movie = db.Movies.FirstOrDefault(p => p.Title == filmTitle);
                        if (movie == null)
                        {
                            movie = new Movie {
                                Title = filmTitle
                            };
                            db.Movies.Add(movie);
                        }



                        var showDuration = db.ShowDurations.FirstOrDefault(p => p.Movie.Title == movie.Title && p.Cinema.Title == cinema.Title);

                        if (startDate.Contains("0000"))
                        {
                            continue;
                        }
                        if (endDate.Contains("0000"))
                        {
                            endDate = DateTime.Now.AddYears(1).ToShortDateString();
                        }

                        if (showDuration == null)
                        {
                            showDuration = new ShowDuration();
                            db.ShowDurations.Add(showDuration);
                        }
                        showDuration.StartShowDate = DateTime.Parse(startDate);
                        showDuration.EndShowDate   = DateTime.Parse(endDate);
                        showDuration.Cinema        = cinema;
                        showDuration.Movie         = movie;

                        var localMovieIdentity = db.LocalMovieIdentities.FirstOrDefault(p => p.Movie.Title == movie.Title && p.Cinema.Title == cinema.Title);
                        if (localMovieIdentity == null)
                        {
                            localMovieIdentity = new LocalMovieIdentity();
                            db.LocalMovieIdentities.Add(localMovieIdentity);
                        }

                        localMovieIdentity.LocalMovieLink  = cinemaMovieLink;
                        localMovieIdentity.LocalIdentifier = cinemaMovieId;
                        localMovieIdentity.Cinema          = cinema;
                        localMovieIdentity.Movie           = movie;
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                _logger.Warn("Exception: " + ex.Message);
                _logger.Warn("Exception Trace: " + ex.StackTrace);
            }

            _logger.Info("End Movie Parcing");
        }
        public static async Task ExecuteShowTimesParcing()
        {
            try
            {
                var response = await _client.GetAsync(@"https://planetakino.ua/showtimes/xml");

                var responseString = await response.Content.ReadAsStringAsync();

                var xmlDoc    = XDocument.Parse(responseString);
                var showtimes = xmlDoc.Descendants("showtimes").Descendants("day").Descendants("show");

                using (BookingBreakerContext db = new BookingBreakerContext())
                {
                    var cinema = GetParcingCinema(db);

                    db.ShowTimes.RemoveRange(db.ShowTimes.Where(p => p.CinemaHall.Cinema.Title == cinema.Title));

                    foreach (var show in showtimes)
                    {
                        var movieId    = show.Attribute("movie-id").Value;
                        var startTime  = DateTime.Parse(show.Attribute("full-date").Value);
                        var url        = show.Attribute("order-url").Value;
                        var hallId     = show.Attribute("hall-id").Value;
                        var technology = show.Attribute("technology").Value;
                        if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(movieId))
                        {
                            continue;
                        }
                        var hallIdInt  = Int32.Parse(hallId);
                        var cinemaHall = db.CinemaHalls.FirstOrDefault(p => p.LocalCinemaHallId == hallIdInt);
                        if (cinemaHall == null)
                        {
                            cinemaHall = new CinemaHall();
                            cinemaHall.LocalCinemaHallId        = Int32.Parse(hallId);
                            cinemaHall.PlacesRepresentationTech = PlacesRepresentationTechEnum.SeatCharts;
                            cinemaHall.Cinema = cinema;
                            db.CinemaHalls.Add(cinemaHall);
                        }


                        var showtimeId = url.Substring(url.IndexOf("show_id=") + 8, url.IndexOf(@"&theatre_id") - url.IndexOf("show_id") - 8);

                        var showresponse = await _client.GetAsync(@"https://cabinet.planetakino.ua" + "/Hall/HallScheme?" + "showtimeId=" + showtimeId + "&theaterId=imax-kiev&hallId=" + hallId + "&attrTechnology=" + technology + "&transactionId=");

                        var placesContent = await showresponse.Content.ReadAsStringAsync();

                        var showPlaces = ParseShowTime(placesContent, cinemaHall);

                        if (showPlaces == null)
                        {
                            continue;
                        }

                        var showTimeTech = TechnologyEnum.TwoD;

                        if (technology.ToLower().Contains("3d"))
                        {
                            showTimeTech = TechnologyEnum.ThreeD;
                        }
                        if (technology.ToLower().Contains("imax"))
                        {
                            showTimeTech = TechnologyEnum.IMAX;
                        }

                        var showtimeEntity = new ShowTime
                        {
                            CinemaHall     = cinemaHall,
                            Movie          = db.LocalMovieIdentities.Include(c => c.Movie).First(p => p.LocalIdentifier == movieId).Movie,
                            StartTime      = startTime,
                            Link           = url,
                            ShowTimePlaces = showPlaces,
                            Technology     = showTimeTech
                        };
                        showPlaces.ForEach(p => p.ShowTime = showtimeEntity);

                        db.ShowTimes.Add(showtimeEntity);
                    }
                    ;
                    db.SaveChanges();
                }
            }

            catch (Exception ex)
            {
                _logger.Warn("Exception: " + ex.Message);
                _logger.Warn("Exception Trace: " + ex.StackTrace);
            }
        }