示例#1
0
        public TwitchHelixClient(HelixInitData initData, ILogger <TwitchHelixClient> logger)
        {
            _logger = logger;

            CheckInitData(initData);
            _clientId     = initData.ClientId;
            _clientSecret = initData.ClientSecret;

            _httpClient = GetHttpClient();
        }
示例#2
0
        private bool CheckInitData(HelixInitData data)
        {
            if (string.IsNullOrWhiteSpace(data.ClientId))
            {
                throw new ArgumentException($"{nameof(data.ClientId)} is missing");
            }

            if (string.IsNullOrWhiteSpace(data.ClientSecret))
            {
                throw new ArgumentException($"{nameof(data.ClientId)} is missing");
            }

            return(true);
        }
示例#3
0
        public async Task <List <LiveStreamData> > GetLiveStreamsData()
        {
            var config = _config.CurrentValue;

            if (!CheckConfig(config))
            {
                return(null);
            }

            _logger.LogInformation($"TwitchConfig logins count: {config.Logins.Count}");
            if (config.Logins.Count == 0)
            {
                return(null);
            }

            var initData = new HelixInitData()
            {
                ClientId     = config.ClientId,
                ClientSecret = config.ClientSecret,
            };
            var twitchClient = new TwitchHelixClient(initData, _loggerFactory.CreateLogger <TwitchHelixClient>());

            await twitchClient.Authorize();

            var streamsTask = twitchClient.GetStreamsDataByLogin(config.Logins);
            //TODO Make cache for users and games
            //TODO Request users by id (get ids from streamTaskResult)
            var usersTask = twitchClient.GetUsersDataByLogin(config.Logins);

            Task.WaitAll(streamsTask, usersTask);

            var streamsResult = streamsTask.Result;

            if (streamsResult == null)
            {
                _logger.LogDebug($"{nameof(streamsResult)} is null");
                return(null);
            }

            if (streamsResult.Data.Count == 0)
            {
                _logger.LogInformation($"No one streaming right now");
                return(null);
            }

            var usersResult = usersTask.Result;

            if (usersResult == null)
            {
                _logger.LogDebug($"{nameof(usersResult)} is null");
                return(null);
            }

            if (usersResult.Data.Count == 0)
            {
                _logger.LogWarning($"{nameof(usersResult)} Data count is 0");
                return(null);
            }

            var gameIds = streamsResult.Data.Select(x => x.Game_id).ToList();

            var gamesResult = await twitchClient.GetGamesDataById(gameIds);

            if (gamesResult == null)
            {
                _logger.LogDebug($"{nameof(gamesResult)} is null");
                return(null);
            }

            var liveStreamsData = new List <LiveStreamData>();

            foreach (var streamResult in streamsResult.Data)
            {
                var lsData = MakeStreamData(streamResult, gamesResult, usersResult);
                if (lsData == null)
                {
                    continue;
                }

                liveStreamsData.Add(lsData);
            }

            return(liveStreamsData);
        }