Example #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var db = new ComeAndTicketContext("come_and_ticket_user", "comeandticket"))
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        _logger.Info("Worker running at: {time}", DateTimeOffset.Now);

                        _logger.Info("Updating Drafthouse data from web");
                        await ComeAndTicketContext.UpdateDatabaseFromWebAsync(db);

                        _logger.Info("Writing to database");
                        await db.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex, "Exception while updating drafthouse data from web");
                    }

                    await Task.Delay(TimeSpan.FromMinutes(20));
                }
            }
        }
        private async Task <User> InnerGetUserAsync(string userName)
        {
            User user   = new User(userName);
            var  dbUser = await _dbContext.Users
                          .Include(u => u.DeviceNicknames)
                          .Include(u => u.MovieTitlesToWatch)
                          .Where(u => u.EMail == user.EMail)
                          .FirstOrDefaultAsync();

            if (dbUser == null)
            {
                dbUser = user;
                _dbContext.Users.Add(user);
                await _dbContext.SaveChangesAsync();
            }

            return(dbUser);
        }
Example #3
0
        private static async Task PushMoviesAsync(ComeAndTicketContext db)
        {
            var users = await db.Users
                        .Include(u => u.HomeMarket)
                        .Include(u => u.Notifications)
                        .Include(u => u.MovieTitlesToWatch)
                        .Include(u => u.DeviceNicknames)
                        .ToListAsync();

            foreach (var user in users)
            {
                if (string.IsNullOrEmpty(user.PushbulletApiKey))
                {
                    _logger.Warn($"User is not configured with a pushbullet API key: {user.EMail}");
                    continue;
                }

                _logger.Info($"Getting devices from Pushbullet for user {user.EMail}");
                var pushbulletApi             = new Pushbullet(user.PushbulletApiKey);
                var retrieveDevicesByNickname = user.DeviceNicknames.Select(async nickname => await pushbulletApi.GetDeviceByNickname(nickname.Value));
                var devices = await Task.WhenAll(retrieveDevicesByNickname);

                if (!devices.Any())
                {
                    continue;
                }

                _logger.Info($"Finding movies for user {user.EMail}");
                var showTimes = await FindMoviesAsync(user, db);

                IEnumerable <IGrouping <Movie, ShowTime> > moviesOnSale = (
                    from s in showTimes
                    where MovieTitleContains(s.Movie, user.MovieTitlesToWatch)
                    where !MovieAlreadySent(s, user)
                    where s.TicketsStatus != TicketsStatus.Past
                    select s
                    ).GroupBy(
                    s => s.Movie,
                    MovieComparer.TitleCurrentCultureIgnoreCase);

                if (!moviesOnSale.Any())
                {
                    continue;
                }

                var showTimesSent  = new List <ShowTime>();
                var messageBuilder = new StringBuilder();
                messageBuilder.AppendLine(Environment.MachineName);
                foreach (var movieOnSale in moviesOnSale)
                {
                    Movie m = movieOnSale.Key;
                    messageBuilder.AppendLine(m.Title);
                    foreach (ShowTime s in movieOnSale)
                    {
                        if (s.TicketsStatus == TicketsStatus.OnSale)
                        {
                            messageBuilder.AppendLine($" - {s.Date} (Left: {s.SeatsLeft} seats, Buy: {s.TicketsUrl} )");
                        }
                        else if (s.TicketsStatus == TicketsStatus.SoldOut)
                        {
                            messageBuilder.AppendLine($" - {s.Date} (Sold out)");
                        }
                        else
                        {
                            messageBuilder.AppendLine($" - {s.Date} (Unknown ticket status)");
                        }

                        showTimesSent.Add(s);
                    }

                    messageBuilder.AppendLine();
                }

                foreach (var device in devices)
                {
                    await pushbulletApi.PushNoteAsync(
                        "New tickets available",
                        messageBuilder.ToString(),
                        device.Id);
                }
                MarkMovieSent(showTimesSent, user);
            }

            await db.SaveChangesAsync();
        }