Пример #1
0
        public void testCookieInterceptor()
        {
            CookieInterceptor cookieInterceptor = new CookieInterceptor(TestConstants.username, TestConstants.password);

            client = new CloudantClientBuilder(TestConstants.account)
            {
                interceptors = new List <IHttpConnectionInterceptor>()
                {
                    cookieInterceptor
                }
            }.GetResult();

            db = client.Database(DBName);

            Assert.DoesNotThrow(async() =>
            {
                await db.EnsureExistsAsync().ConfigureAwait(continueOnCapturedContext: false);
            },
                                "Exception thrown while creating database using cookie interceptor. ");

            Assert.NotNull(db);
        }
Пример #2
0
        public override void Run(params string[] arguments)
        {
            InitEnvorimentAndVerify();

            try
            {
#if !NET_CORE
                if (CookieInterceptor != null)
                {
                    this.Log("尝试获取 Cookie...", LogLevel.Info);
                    var cookie = NetworkCenter.Current.Execute("getcookie", () => CookieInterceptor.GetCookie());
                    if (cookie == null)
                    {
                        this.Log("获取 Cookie 失败, 爬虫无法继续.", LogLevel.Warn);
                        return;
                    }
                    else
                    {
                        Site.CookiesStringPart = cookie.CookiesStringPart;
                        Site.SetCookies(cookie.CookiesDictionary);
                    }
                }
#endif

                this.Log("创建爬虫...", LogLevel.Info);

                foreach (var entity in Entities)
                {
                    string entiyName = entity.Entity.Name;
                    var    pipelines = new List <BaseEntityPipeline>();
                    foreach (var pipeline in EntityPipelines)
                    {
                        var newPipeline = pipeline.Clone();
                        newPipeline.InitiEntity(entity);
                        if (newPipeline.IsEnabled)
                        {
                            pipelines.Add(newPipeline);
                        }
                    }
                    if (pipelines.Count > 0)
                    {
                        AddPipeline(new EntityPipeline(entiyName, pipelines));
                    }
                }

                CheckIfSettingsCorrect();

                bool   needInitStartRequest = true;
                string key = "locker-" + Identity;
                if (Db != null)
                {
                    while (!Db.LockTake(key, "0", TimeSpan.FromMinutes(10)))
                    {
                        Thread.Sleep(1000);
                    }
                    var lockerValue = Db.HashGet(InitStatusSetKey, Identity);
                    needInitStartRequest = lockerValue != "init finished";
                }

                if (arguments.Contains("rerun"))
                {
                    Scheduler.Init(this);
                    Scheduler.Dispose();
                    //DELETE verify record.
                    Db?.HashDelete(ValidateStatusKey, Identity);
                    needInitStartRequest = true;
                }

                this.Log("构建内部模块、准备爬虫数据...", LogLevel.Info);
                InitComponent();

                if (needInitStartRequest)
                {
                    if (PrepareStartUrls != null)
                    {
                        for (int i = 0; i < PrepareStartUrls.Length; ++i)
                        {
                            var prepareStartUrl = PrepareStartUrls[i];
                            this.Log($"[步骤 {i + 2}] 添加链接到调度中心.", LogLevel.Info);
                            prepareStartUrl.Build(this, null);
                        }
                    }
                }

                Db?.LockRelease(key, 0);

                RegisterControl(this);

                if (!arguments.Contains("running-test"))
                {
                    base.Run();
                }
                else
                {
                    _scheduler.IsExited = true;
                }

                TaskFinished();

                HandleVerifyCollectData();
            }
            finally
            {
                Dispose();
            }
        }