Exemple #1
0
        private async Task ConvertTwitchUsernamesToChannelIds(List <LivestreamFileEntry> livestreamFileEntries)
        {
            var twitchStreams = livestreamFileEntries.Where(x => x.StreamProvider == TwitchApiClient.API_NAME).ToList();

            var twitchUsers = new List <User>();
            var query       = new GetUsersQuery();

            query.UserNames.AddRange(twitchStreams.Select(x => x.ChannelId));
            var users = await twitchTvHelixReadonlyClient.GetUsers(query);

            twitchUsers.AddRange(users);

            foreach (var twitchStream in twitchStreams)
            {
                var matchingUser = twitchUsers.FirstOrDefault(x => x.Login.IsEqualTo(twitchStream.ChannelId));
                if (matchingUser != null)
                {
                    twitchStream.DisplayName = matchingUser.DisplayName;
                    twitchStream.ChannelId   = matchingUser.Id;
                }
                else
                {
                    livestreamFileEntries.Remove(twitchStream); // will have to be manually re-imported, most likely the channel has been removed/banned
                }
            }
        }
        public async Task <List <LivestreamQueryResult> > AddChannel(ChannelIdentifier newChannel)
        {
            if (newChannel == null)
            {
                throw new ArgumentNullException(nameof(newChannel));
            }

            // shorter implementation of QueryChannels
            var  queryResults = new List <LivestreamQueryResult>();
            User user;

            if (long.TryParse(newChannel.ChannelId, out var _))
            {
                var users = await twitchTvHelixClient.GetUsers(new GetUsersQuery()
                {
                    UserIds = new List <string>()
                    {
                        newChannel.ChannelId
                    }
                });

                user = users.FirstOrDefault();
            }
            else
            {
                user = await twitchTvHelixClient.GetUserByUsername(newChannel.ChannelId);
            }

            if (user == null)
            {
                throw new InvalidOperationException("No user found for id " + newChannel.ChannelId);
            }

            newChannel.OverrideChannelId(user.Id);
            newChannel.DisplayName      = user.DisplayName;
            channelIdToUserMap[user.Id] = user;
            var livestream = new LivestreamModel(user.Id, newChannel)
            {
                DisplayName = user.DisplayName
            };

            var onlineStreams = await twitchTvHelixClient.GetStreams(new GetStreamsQuery()
            {
                UserIds = new List <string>()
                {
                    user.Id
                }
            });

            var onlineStream = onlineStreams.FirstOrDefault();

            if (onlineStream != null)
            {
                livestream.PopulateWithStreamDetails(onlineStream);
            }

            queryResults.Add(new LivestreamQueryResult(newChannel)
            {
                LivestreamModel = livestream
            });

            if (queryResults.All(x => x.IsSuccess))
            {
                moniteredChannels.Add(newChannel);
            }
            return(queryResults);
        }