Exemple #1
0
 public TwitchService(UserService userService, ILogger <TwitchService> logger, ChatService chatService,
                      ConfigurationService configurationService, IntegrationService integrationService)
 {
     this.userService          = userService;
     this.logger               = logger;
     this.chatService          = chatService;
     this.configurationService = configurationService;
     this.integrationService   = integrationService;
 }
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            pollingInterval = minimumPollingInterval;
            delay           = defaultDelay;

            cancellationToken.Register(() =>
            {
                logger.LogDebug($"YoutubeBackgroundService background task is stopping.");
            });

            using (var scope = serviceScopeFactory.CreateScope())
            {
                youtubeService   = scope.ServiceProvider.GetRequiredService <YoutubeService>();
                chatService      = scope.ServiceProvider.GetRequiredService <ChatService>();
                dashboardService = scope.ServiceProvider.GetRequiredService <DashboardService>();

                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        if (!youtubeService.IsEnabled().Result)
                        {
                            await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Disabled, cancellationToken);

                            await Task.Delay(delay, cancellationToken);

                            continue;
                        }

                        if (!youtubeService.IsConfigured())
                        {
                            await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.NotConfigured, cancellationToken);

                            await Task.Delay(delay, cancellationToken);

                            continue;
                        }

                        var token = await youtubeService.GetToken();

                        if (token == null)
                        {
                            await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Forbidden, cancellationToken);

                            await Task.Delay(delay, cancellationToken);

                            continue;
                        }

                        if (token.Status == AccessTokenStatus.Expired && token.HasRefreshToken)
                        {
                            await youtubeService.RefreshToken(token);
                        }

                        if (!youtubeService.IsValidToken(token))
                        {
                            await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Forbidden, cancellationToken);

                            await Task.Delay(delay, cancellationToken);

                            continue;
                        }

                        while (!cancellationToken.IsCancellationRequested && youtubeService.IsEnabled().Result)
                        {
                            if (string.IsNullOrEmpty(liveChatId))
                            {
                                var liveBroadcast = await youtubeService.GetLiveBroadcast();

                                if (liveBroadcast == null)
                                {
                                    await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Offline, cancellationToken);

                                    await Task.Delay(delay, cancellationToken);

                                    continue;
                                }

                                liveChatId = liveBroadcast.snippet.liveChatId;
                            }

                            var liveChatMessages = await youtubeService.GetLiveChatMessages(liveChatId);

                            await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Connected, cancellationToken);

                            if (liveChatMessages == null || !liveChatMessages.items.Any())
                            {
                                await Task.Delay(TimeSpan.FromMilliseconds(pollingInterval), cancellationToken);

                                continue;
                            }

                            foreach (var item in liveChatMessages.items)
                            {
                                await chatService.CreateMessage(youtubeService.MapToChatMessage(item));
                            }

                            pollingInterval = Math.Max(liveChatMessages.pollingIntervalMillis, minimumPollingInterval);
                            await Task.Delay(pollingInterval, cancellationToken);
                        }

                        await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Stopped, cancellationToken);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex.GetBaseException(), ex.GetBaseException().Message);
                        await dashboardService.UpdateStatus(ApiSource.Youtube, BackgroundServiceStatus.Error, cancellationToken);
                    }
                    await Task.Delay(delay, cancellationToken);
                }
            }
        }