public YouTubeLiveSiteContext(ICommentOptions options, IYouTubeLiveServer server, ILogger logger, IUserStoreManager userStoreManager)
     : base(options, userStoreManager, logger)
 {
     _options = options;
     _server  = server;
     _logger  = logger;
 }
        internal async Task <IVidResult> GetResultFromChannelId(IYouTubeLiveServer server, string channelId)
        {
            if (string.IsNullOrEmpty(channelId))
            {
                throw new ArgumentNullException(nameof(channelId));
            }

            var vids = await GetVidsFromChannelId3(server, channelId);

            if (vids.Count == 1)
            {
                return(new VidResult {
                    Vid = vids[0]
                });
            }
            else if (vids.Count == 0)
            {
                return(new NoVidResult());
            }
            else
            {
                return(new MultiVidsResult {
                    Vids = vids
                });
            }
        }
        public static async Task <List <string> > GetVidsAsync(IYouTubeLiveServer server, string channelId)
        {
            //まずは配信中という前提でデータを取得する
            {
                var url = GetChannelLiveListUrl(channelId, ListType.LiveNow);
                var(ytInitialData, type) = await GetYtinitialData(server, url);

                if (type == ListType.LiveNow)
                {
                    return(GetVidsFromYtInitialData(ytInitialData));
                }
            }
            //配信の予約が入っているのかもしれない
            {
                var url = GetChannelLiveListUrl(channelId, ListType.UpcomingLiveStreams);
                var(ytInitialData, type) = await GetYtinitialData(server, url);

                if (type == ListType.UpcomingLiveStreams)
                {
                    return(GetVidsFromYtInitialData(ytInitialData));
                }
            }
            //これ以外にチャットを取得できる場面は無いから諦める
            return(new List <string>());
        }
        public CommentProviderNext(ICommentOptions options, IYouTubeLiveServer server, YouTubeLiveSiteOptions siteOptions, ILogger logger, IUserStoreManager userStoreManager)
            : base(logger, options)
        {
            _options          = options;
            _siteOptions      = siteOptions;
            _logger           = logger;
            _userStoreManager = userStoreManager;
            _server           = server;

            CanConnect    = true;
            CanDisconnect = false;
        }
        internal async Task <string> GetChannelIdFromUserId(IYouTubeLiveServer server, string userId)
        {
            var url  = "https://www.youtube.com/user/" + userId;
            var html = await server.GetAsync(url);

            var match = Regex.Match(html, "<meta property=\"og:url\" content=\"https://www.youtube.com/channel/([^\"]+)\">");

            if (match.Success)
            {
                var channelId = match.Groups[1].Value;
                return(channelId);
            }
            throw new ParseException(html);
        }
        public async Task <IVidResult> GetVid(IYouTubeLiveServer server, Input.IInput input)
        {
            if (input is Input.Vid vid)
            {
                return(new VidResult {
                    Vid = vid.Raw
                });
            }
            else if (input is WatchUrl watchUrl)
            {
                return(new VidResult {
                    Vid = watchUrl.Vid
                });
            }
            else if (input is Input.ChannelUrl channelUrl)
            {
                var channelId = channelUrl.ChannelId;
                return(await GetResultFromChannelId(server, channelId));
            }
            else if (input is Input.UserUrl userUrl)
            {
                var userId    = userUrl.UserId;
                var channelId = await GetChannelIdFromUserId(server, userId);

                return(await GetResultFromChannelId(server, channelId));
            }
            else if (input is Input.CustomChannelUrl customChannelUrl)
            {
                var(channelId, _) = await TryGetChannelIdFromCustomChannel(server, customChannelUrl.Raw);

                return(await GetResultFromChannelId(server, channelId));
            }
            else if (input is Input.StudioUrl studioUrl)
            {
                return(new VidResult {
                    Vid = studioUrl.Vid
                });
            }
            return(new NoVidResult());
        }
 public MetadataProvider(IYouTubeLiveServer server, ILogger logger) : base(logger)
 {
     _server = server;
 }
 internal Task <List <string> > GetVidsFromChannelId3(IYouTubeLiveServer server, string channelId)
 {
     return(ChannelLiveResearcher.GetVidsAsync(server, channelId));
 }
        internal async Task <(string channelId, string reason)> TryGetChannelIdFromCustomChannel(IYouTubeLiveServer server, string input)
        {
            var match1 = _regexCustomChannel.Match(input);

            if (match1.Success)
            {
                var userId = match1.Groups[1].Value;
                var html   = await server.GetAsync($"https://www.youtube.com/c/{userId}");

                var match2 = Regex.Match(html, "property=\"og:url\" content=\"https://www\\.youtube\\.com/channel/(?<channelid>[^/\"?]+)\">");
                if (match2.Success)
                {
                    var channelId = match2.Groups["channelid"].Value;
                    return(channelId, null);
                }
            }
            return(null, "");
        }
        private static async Task <(string ytInitialData, ListType)> GetYtinitialData(IYouTubeLiveServer server, string url)
        {
            var html = await server.GetEnAsync(url);

            var ytInitialData = Tools.ExtractYtInitialDataFromChannelHtml(html);
            var type          = GetType(ytInitialData);

            return(ytInitialData, type);
        }