php實現(xiàn)多進程下載百度網(wǎng)盤文件

大家知道百度網(wǎng)盤下載對于非會員是有下載限速的, 最大速度基本上維持在 100kB/s以內(nèi),要下個電影啥的,那就有得等了. 之前還可以把網(wǎng)盤里的文件鏈接解析出來放到Uget或迅雷之類的下載工具里去多線程下載, 但是現(xiàn)在百度的文件服務(wù)器對ua做了校驗, 沒找到好用的可編輯http header的下載工具, 于是自己動手寫一個了.基本上可以充分利用已有的帶寬

download_demo.gif

PS: 請自行安裝swoole拓展和Guzzle http包.
直接上代碼吧:

<?php

include 'vendor/autoload.php';
// $service = new Service('http://peterq.cn/movie/api/video_redirect?fid=543468589252145', __DIR__);
$service = new Service('http://peterq.cn/movie/api/video_redirect?fid=364402848596280', __DIR__);
$service->start();

use GuzzleHttp\Client;


class Service
{

    /**
     * @var Client;
     */
    protected $client;

    protected $worker_pool; // 下載進程池

    protected $available_worker_queue; // 可用的進程隊列

    protected $worker_number = 16; // 定義需要開多少個進程, 文件較小時, 并不一定全部用得上, 取決于你的分片大小

    protected $started = false; // 是否已經(jīng)開始下載

    protected $url; // 下載鏈接

    protected $length; // 文件大小

    protected $dir; // 保存目錄

    protected $filename; // 文件絕對路徑

    protected $downloaded = 0; // 已下載字節(jié)數(shù)

    protected $speedArr = []; // 用來計算下載速度的數(shù)組

    protected $distributed = 0; // 對于要下載的文件, 已經(jīng)分配到哪個位置了

    public function __construct($url, $dir)
    {
        $this->url = $url;
        $this->dir = realpath($dir);
    }

    public function start()
    {
        if ($this->started) return;
        $this->available_worker_queue = new SplQueue();
        $this->started = true;
        // 創(chuàng)建客戶端
        $this->client = new Client([
            'headers' => [
                "Accept"           => "application/json, text/javascript, text/html, */*; q=0.01",
                "Accept-Encoding"  => "gzip, deflate, sdch",
                "Accept-Language"  => "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2",
                "Referer"          => "http://pan.baidu.com/disk/home",
                "X-Requested-With" => "XMLHttpRequest",
                "User-Agent"       => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
                "Connection"       => "keep-alive",
            ],
        ]);
        // 設(shè)置進程名稱
        swoole_set_process_name('download-master');
        echo 'master pid:' . posix_getpid() . PHP_EOL;
        // 創(chuàng)建多個下載進程
        for ($i = 0; $i < $this->worker_number; $i++) {
            $process = $this->createProcess($i);
            $this->worker_pool[$i] = $process;
            // 通信通道加入事件輪訓(xùn), 進行異步通信
            swoole_event_add($process->pipe, function ($pipe) use ($process) {
                $data = $process->read();
                $data = unserialize($data);
                $this->handleChildMessage($process, $data['type'], $data['data']);
            });
            $process->start();
            $this->available_worker_queue->enqueue($process);
        }

        // 子進程退出回收
        swoole_process::signal(SIGCHLD, function ($sig) {
            static $exited = 0;
            // 必須為false,非阻塞模式
            while ($ret = swoole_process::wait(false)) {
                echo "child process exited, PID={$ret['pid']}\n";
                $exited++;
                if ($exited == count($this->worker_pool)) exit();
            }
        });

        $this->initDownload();

    }

    // 初始化下載
    protected function initDownload()
    {
        $resp = $this->client->request('GET', $this->url, [
            'stream'          => true,
            'read_timeout'    => 10,
        ]);
        // 處理重定向
        while (in_array($resp->getStatusCode(), [301, 302])) {
            $this->url = $resp->getBody()->read(1024);
            dump('redirect: ' . $this->url);
            $resp = $this->client->request('GET', $this->url, [
                'stream'          => true,
                'read_timeout'    => 10,
            ]);
        }
        if (!$resp->getHeader('Content-Disposition')) {
            dump('not a file download url');
        }
        $this->length = intval($resp->getHeader('Content-Length')[0]);
        $fname = $resp->getHeader('Content-Disposition')[0];
        $fname = substr($fname, strpos($fname, 'filename=') + strlen('filename='));
        $fname = urldecode($fname);
        $this->filename = $this->dir . '/' . $fname;
        dump([
            '文件' => $this->filename,
            '大小' => round($this->length / 1024 / 1024, 2) . 'MB'
        ]);
        file_put_contents($this->filename, '');
        $this->download();
    }

    // 啟動下載
    protected function download()
    {
        while (
            $this->distributed < $this->length
            && $this->available_worker_queue->count()
            && $process = $this->available_worker_queue->dequeue()) {
            $this->distributeSegment($process);
        }
    }

    // 分配下一塊區(qū)間給一個進程
    protected function distributeSegment($process)
    {
        // 分成 1 MB 一個段去下載
        $size = 1 * 1024 * 1024;
        $process->write(serialize([
            'type' => 'new-segment',
            'data' => [
                'url' => $this->url,
                'file' => $this->filename,
                'start' => $this->distributed,
                'length' => min($size, $this->length - $this->distributed),
            ]
        ]));
        $this->distributed += $size;
    }

    // 進程間通信處理
    protected function handleChildMessage($process, $type, $data)
    {
        method_exists($this, 'on' . ucfirst($type)) and $this->{'on' . ucfirst($type)}($process, $data);
    }

    // 當(dāng)下載進程下載一小塊時, 通過此回調(diào)通知master進程
    protected function onRange(swoole_process $process, $data)
    {
        $this->downloaded += $data;
        static $lastClearTime = 0;
        $time = time();
        $this->speedArr[$time] = $this->speedArr[$time] ?? 0;
        $this->speedArr[$time] += $data;
        // 取過去 5 秒作為平均速度 作為速度顯示, 粗略計算, 并不準確
        if ($time > $lastClearTime) {
            $lastClearTime = $time;
            foreach ($this->speedArr as $t => $size) {
                if ($t < $time - 5) unset($this->speedArr[$t]);
            }
        }
        $speed = array_sum($this->speedArr) / count($this->speedArr);
        $percent = $this->downloaded / $this->length * 100;
        $percent = round($percent, 2);
        $size = humanSize($this->downloaded);
        $speed = humanSize($speed);
        echo "\r\033[2K" . "已下載: $size, $percent%; 當(dāng)前速度: $speed/s";
    }

    // 當(dāng)分配給下載進程的下載任務(wù)完成時執(zhí)行的回調(diào)
    protected function onTaskFinished($process, $data)
    {
        if ($this->distributed < $this->length)
            $this->distributeSegment($process);
        else {
            $this->available_worker_queue->enqueue($process);
            if ($this->available_worker_queue->count() == count($this->worker_pool)) {
                dump('文件下載完成');
                foreach ($this->worker_pool as $worker) {
                    $worker->write(serialize([
                        'type' => 'exit', 'data' => ''
                    ]));
                }
            }
        }
    }

    // 創(chuàng)建下載進程
    protected function createProcess($index = null)
    {
        $process = new swoole_process(function (swoole_process $process) use ($index) {
            swoole_set_process_name('download worker' . $index);
            echo sprintf('worker:%s, pid:%s', $index, posix_getpid()) . PHP_EOL;
            $downloader = null;
            // 通信通道加入事件輪訓(xùn), 進行異步通信
            swoole_event_add($process->pipe, function ($pipe) use ($process, &$downloader) {
                $data = $process->read();
                $data = unserialize($data);
                $type = $data['type'];
                $data = $data['data'];
                // 這里會阻塞掉, 后續(xù)改進
                if ($type == 'new-segment') {
                    $downloader = new Downloader($process, $this->client, $data['url'], $data['file'], $data['start'], $data['length']);
                    $downloader->download();
                    $process->write(serialize([
                        'type' => 'taskFinished',
                        'data' => ''
                    ]));
                    $downloader = null;
                    return;
                }
                if ($type == 'exit') exit(0);
            });
        }, false, 2);

        return $process;
    }
}


// 下載器類
class Downloader
{

    protected $client; // guzzle實例

    protected $process; // 當(dāng)前進程實例

    protected $file; // 文件名

    protected $url;

    protected $start; // 開始位置

    protected $length; // 下載長度

    protected $offset; // 已經(jīng)下到哪一個位置了

    public function __construct(swoole_process $process, Client $client, $url, $file, $start, $length)
    {
        $this->process = $process;
        $this->client = $client;
        $this->url = $url;
        $this->file = $file;
        $this->start = $start;
        $this->length = $length;
    }


    public function download()
    {
        $this->offset = $this->start;
        $res = fopen($this->file, 'rb+');
        fseek($res, $this->start, SEEK_SET);
        $resp = $this->client->request('GET', $this->url, [
            'stream' => true,
            'headers' => [
                'Range' => 'bytes=' . $this->start . '-' . ($this->start + $this->length)
            ]
        ]);
        $loaded = 0;
        while (!$resp->getBody()->eof()) {
            // 5 kb 的下載
            $size = 1024 * 5;
            $data = $resp->getBody()->read($size);
            $loaded += strlen($data);
            fwrite($res, $data);
            $this->process->write(serialize([
                'type' => 'range',
                'data' => strlen($data)
            ]));
            if ($loaded >= $this->length) break; // eof 貌似不起作用, 手動退出
        }
        fclose($res);
        dump($this->length / 1024 / 1024 . 'MB下載完成');
    }
}

// 把文件大小從字節(jié)轉(zhuǎn)換為合適的單位
function humanSize($size) {
    $units = ['B', 'KB', 'MB', 'GB'];
    foreach ($units as $unit) {
        if ($size > 1024)
            $size /= 1024;
        else break;
    }
    return round($size, 2) . $unit;
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http://www.360doc.com/content/14/0508/23/4891348_37594709...
    Albert新榮閱讀 31,304評論 0 32
  • 自恃小景攀西子,對面柳承近岸風(fēng)。漣漪不及蘆葦叢,春來潭水妝新衡。
    將相河閱讀 211評論 0 3
  • 今天去博印堂參加分享會,心裡不得不感歎這是一隻充滿責(zé)任感、使命感的團隊! 這是一隻年輕的團隊,他們每個人的內(nèi)心都住...
    夢瑤閱讀 236評論 0 0
  • 今天,我們在昨天聊到的學(xué)會止盈的基礎(chǔ)上再細聊一下定投如何止盈。 定投止盈看起來很難,但最終其實就是兩個問題。 1、...
    有斗閱讀 1,375評論 0 4
  • 自從人工智能誕生以來,人類似乎就沒停止過對于人工智能未來的擔(dān)憂,甚至包括霍金。這也從側(cè)面佐證了,腦洞大開的編劇們在...
    比持卡丘閱讀 242評論 0 0

友情鏈接更多精彩內(nèi)容