public async Task ConnectAsync(string input, IBrowserProfile browserProfile)
        {
            var cc = GetCookieContainer(browserProfile);

            var list = GetCommentProviderInternals(_options, _siteOptions, _userStoreManager, _dataSource, _logger, this, SiteContextGuid);
            var cu   = await GetCurrentUserInfo(browserProfile);

            if (cu.IsLoggedIn)
            {
                foreach (var f in list)
                {
                    var isValid = f.IsValidInput(input);
                    if (isValid)
                    {
                        _internal = f;
                        break;
                    }
                }
            }
            else
            {
                //未ログインでもWebSocket経由なら取れる。
                var f = new NewLiveInternalProvider(_options, _siteOptions, _userStoreManager, _logger, _dataSource)
                {
                    SiteContextGuid = SiteContextGuid,
                };
                var isValid = f.IsValidInput(input);
                if (isValid)
                {
                    _internal = f;
                }
            }
            if (_internal == null)
            {
                //非対応のInput
                //AfterDisconnected();
                return;
            }
            BeforeConnect();
            _internal.MetadataUpdated += (s, e) => MetadataUpdated?.Invoke(s, e);
            _internal.MessageReceived += (s, e) => MessageReceived?.Invoke(s, e);
            try
            {
                await _internal.ConnectAsync(input, cc);
            }
            catch (Exception ex)
            {
                throw new NicoException("", $"input={input},browser={browserProfile.Type}({browserProfile.ProfileName})", ex);
            }
            finally
            {
                AfterDisconnected();
            }
        }
Esempio n. 2
0
        public async Task ConnectAsync(string input, IBrowserProfile browserProfile)
        {
            _blocker.Reset();
            var cc = GetCookieContainer(browserProfile);

            var list = GetCommentProviderInternals(_options, _siteOptions, _userStoreManager, _dataSource, _logger, this, SiteContextGuid);
            var cu   = await GetCurrentUserInfo(browserProfile);

            if (cu.IsLoggedIn)
            {
                foreach (var f in list)
                {
                    var isValid = f.IsValidInput(input);
                    if (isValid)
                    {
                        _internal = f;
                        break;
                    }
                }
            }
            else
            {
                //未ログインでもWebSocket経由なら取れる。
                var f = new NewLiveInternalProvider(_options, _siteOptions, _userStoreManager, _logger, _dataSource)
                {
                    SiteContextGuid = SiteContextGuid,
                };
                var isValid = f.IsValidInput(input);
                if (isValid)
                {
                    _internal = f;
                }
            }
            if (_internal == null)
            {
                //非対応のInput
                //AfterDisconnected();
                return;
            }
            BeforeConnect();
            _internal.MetadataUpdated += (s, e) => MetadataUpdated?.Invoke(s, e);
            _internal.MessageReceived += (s, e) =>
            {
                if (e.Message is INicoComment nicoComment)
                {
                    var userId     = nicoComment.UserId;
                    var comment    = nicoComment.Text;
                    var postedDate = nicoComment.PostedAt;
                    if (!_blocker.IsUniqueComment(userId, comment, postedDate))
                    {
                        Debug.WriteLine("ニコ生で二重コメントを発見したため無視します");
                        return;
                    }
                }
                MessageReceived?.Invoke(s, e);
            };
            try
            {
                await _internal.ConnectAsync(input, cc);
            }
            catch (Exception ex)
            {
                throw new NicoException("", $"input={input},browser={browserProfile.Type}({browserProfile.ProfileName})", ex);
            }
            finally
            {
                _internal.MetadataUpdated -= (s, e) => MetadataUpdated?.Invoke(s, e);
                _internal.MessageReceived -= (s, e) => MessageReceived?.Invoke(s, e);
                AfterDisconnected();
            }
        }