setDescription('自动采集fashionsnap.com'); $this->addOption('brandId', 'b', InputOption::VALUE_OPTIONAL, '指定的品牌id', false); } private function _getTask($brand): \Generator { $query = Db::table('app_brands'); if ($brand) { $query->whereIn('id', explode(',', $brand)); } else { $query->where('spider_origin', '=', 'fashionsnap')->orderBy('id'); } foreach ($query->cursor() as $row) { if (!$row) { throw new ExitException('END.'); } yield $row; } } public function execute(InputInterface $input, OutputInterface $output): int { $brand = $input->getOption('brandId'); run(function () use ($brand) { foreach ($this->_getTask($brand) as $task) { list($result, $httpCode) = $this->request($this->getBaseUrl() . "/api/algolia/article/?blogIds=4&brandName={$task->name}&limit=50"); echo $task->name . '--' . $httpCode . PHP_EOL; if ($httpCode == 200) { $isSuccess = false; $result = json_decode($result, true); if ($result['totalCount'] == 0 || $result['totalCount'] > 200) { continue; } foreach ($result['articles'] ?? [] as $item) { $model = $this->getArticleModel(['title' => $item['mainCategory']['name'], 'platform' => static::PLATFORM, 'brand' => $task->id]); $model->title = $item['mainCategory']['name']; $model->year = AppHelper::getYear($model->title); $model->brand = $task->id; $model->module = 0; $model->platform = self::getPlatform(); $saveImages = []; foreach ($item['mainGalleryImages'] as $image) { $saveImages[] = [ 'src' => 'https://fashionsnap-assets.com/asset/width=4096' . $image ]; } $model->images = json_encode($saveImages); $model->cover = $saveImages[0]['src'] ?? ''; // permalink $model->source_url = 'https://fashionsnap.com' . $item['permalink']; if ($model->cover) { $isSuccess = $model->save(); } } if ($isSuccess) { $brandModel = AppBrand::find($task->id); $brandModel->spider_origin = self::getPlatform(); $brandModel->save(); } } } }); return 0; } }