Created
May 11, 2018 15:36
-
-
Save Rhincodon/543ed2be816155713d72422866b384e8 to your computer and use it in GitHub Desktop.
parse.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use GuzzleHttp\Promise\EachPromise; | |
use Illuminate\Console\Command; | |
use GuzzleHttp\Client; | |
use Psr\Http\Message\ResponseInterface; | |
class ParseProducts extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'parse:products'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Parse Products'; | |
private $client; | |
private $responses = ['success' => 0, 'fail' => 0]; | |
/** | |
* Create a new command instance. | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->client = new Client(['base_uri' => 'https://jcs.ua/']); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->info('Start'); | |
$total = 2000; | |
$offset = 0; | |
$bar = $this->output->createProgressBar($total - $offset); | |
$start = \Carbon\Carbon::now(); | |
$promises = (function () use ($total, $offset) { | |
foreach (range($offset, $total) as $number) { | |
yield $this->client->requestAsync('GET', | |
'svetovoe-oborudovanie/staticheskie-svetovye-pribory/prozhektory-par/prozhektor_marq_onset120ww/', [ | |
'headers' => [ | |
'cookie' => 'BITRIX_SM_GUEST_ID=2560245; BITRIX_SM_LAST_VISIT=11.05.2017+18%3A10%3A14' | |
] | |
])->then(function ( | |
ResponseInterface $response | |
) use ($number) { | |
$data = $response->getStatusCode(); | |
return ['id' => $number, 'data' => $data]; | |
}); | |
} | |
})(); | |
(new EachPromise($promises, [ | |
'concurrency' => 10, | |
'fulfilled' => function ($data) use ($bar) { | |
$bar->advance(); | |
$this->info($data['data']); | |
$this->responses['success'] += 1; | |
}, | |
'rejected' => function ($exception, $index) { | |
$this->warn(503 . '-' . $index); | |
$this->responses['fail'] += 1; | |
}, | |
]))->promise()->wait(); | |
$bar->finish(); | |
$this->info('Success: ' . $this->responses['success']); | |
$this->info('Fail: ' . $this->responses['fail']); | |
$this->info('Start: ' . $start->toDateTimeString()); | |
$this->info('End: ' . \Carbon\Carbon::now()->toDateTimeString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment