vasily-kartashov/downloader

此包的最新版本(0.2.0)没有可用的许可证信息。

下载器库

0.2.0 2021-10-01 03:05 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:11 UTC


README

CI Status Packagist Packagist Coverage Status Psalm coverage

示例

<?php

$redisClient = new Redis();
$redisClient->connect('localhost', 6379);
$redisCachePool = new RedisCachePool($redisClient);

$downloader = Downloader($redisCachePool);

$task = Task::builder()
    ->batch(12)
    ->retry(3)
    ->validate(function ($response) {
        return strlen($response) > 1024;
    })
    ->cache('pages.', 12 * 3600)
    ->options([
        CURLOPT_SSL_VERIFYHOST => false
    ])
    ->throttle(120)
    ->add(1, 'http://example/page/1')
    ->add(2, 'http://example/page/2')
    ...
    ->add(9, 'http://example/page/9')
    ->build();

这将

  • 向指定的URL发送多个curl请求,每批12个。
  • 成功的响应将被缓存12小时。
  • 下载器将尝试下载每一页3次,然后转到下一批。
  • 如果最后一次失败时间少于2分钟,则不会尝试新的下载。
  • 只有长度超过1024的响应被视为成功
$results = $downloader->execute($task);
foreach ($results as $result) {
    if ($result->successful()) {
        echo $result->content();
    } elseif ($result->failed()) {
        echo 'Failed to fetch';
    } elseif ($result->skipped()) {
        echo 'Skipping result, to avoid too many retries';
    }
}

待办事项

  • 嵌入Guzzle并使用标准,仅保持这个接口精简
  • 添加更多测试