123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace Spider\Lib;
- use Dever;
- class Api
- {
- private $queue;
- public function run($id)
- {
- # 写入队列
- $project = new Project($id);
- $config = $project->get();
- if (!$this->queue) {
- $this->queue = new Queue('db');
- }
- $send['id'] = $config['id'];
- $send['collect_rule'] = $config['collect_rule'];
- foreach ($config['url'] as $k => $v) {
- $this->queue->push($v, $send, $config['page_num']);
- }
- }
- public function test_api($id)
- {
- Dever::setInput('test', 1);
- $this->queue = new Queue('data');
- $this->run($id);
- $this->cron();
- return 'yes';
- }
- private function cron()
- {
- if (!$this->queue) {
- $this->queue = new Queue('db');
- }
- Dever::import('task');
- $state = true;
- while ($state) {
- $state = $this->load();
- }
- }
- private function load()
- {
- $config = $this->queue->pop();
- if ($config) {
- # 此处开task
- Dever::task(function() use($config, $this)
- {
- $col = $this->col($config['id']);
- $this->parse($config['url'], $config['id'], $config['collect_rule'], $col);
- });
-
- $state = true;
- } else {
- $state = false;
- }
- return $state;
- }
- private function col($project)
- {
- return Dever::db('spider/col')->getList(['where_project_id' => $project]);
- }
- private function parse($url, $project, $rule, $col)
- {
- $parse = new Parse($url, $project, $rule, $col);
- return $parse->get();
- }
- }
|