<?php


namespace App\Services;

use App\BilibiliCollections;
use App\BilibiliUpVideos;
use App\BilibiliVideoParts;
use App\BilibiliVideos;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class BilibiliService
{

    private $collectionNumberListUrl = "https://api.bilibili.com/medialist/gateway/base/created?ps=100&up_mid=279025&is_space=0&jsonp=jsonp";

    private $collectionListUrl = "https://api.bilibili.com/medialist/gateway/base/spaceDetail?ps=20&keyword=&order=mtime&type=0&tid=0&jsonp=jsonp";

    private $videoPartsUrl = "https://api.bilibili.com/x/player/pagelist?jsonp=jsonp&aid=";

    private $invalidTitle = "已失效视频";

    private $baseDir = "/Volumes/intel660p/video/mv/";

    public function queryPlayList()
    {
        $pageNo = 1;
        $url = "https://space.bilibili.com/ajax/member/getSubmitVideos?mid=391073761&pagesize=30&tid=0&keyword=&order=pubdate&page=";
        for ($i = 1; $i < 30; $i++) {
            # code...
            $result = json_decode(file_get_contents($url . $i), true);
            foreach ($result['data']['vlist'] as $item) {
                echo "av" . $item['aid'] . " ";
            }
            //            echo $result['data']['vlist'][0]['aid'] . "\t";
            //            $pageNo++;
        }
    }

    /**
     * 获取各个 up 主的视频 id
     * @param int $mediaId
     * @throws \Exception
     */
    public function queryUpVideoList($mediaId = 320491072)
    {
        Log::info("schedule queryUpVideoList inner started at: " . date("Y-m-d H:i:s"));


        // 475250 kyokyo
        // 3489535 熊小颖
        // 320491072 绯绯
        // 10139490 短短
        // 116683 咬人猫
        // 16539048 小仙若
        // 10278125 香草猫饼
        // 391073761 女团直拍
        $list = BilibiliUpVideos::all();
        foreach ($list as $item) {
            Log::info("schedule queryUpVideoList current up is {$item['up_name']}, started at: " . date("Y-m-d H:i:s"));
            $files = scandir($this->baseDir . "bilibili/" . $item['up_name']);

            $mediaId = $item['mid'];
            $videos = [];
            $pageNo = 1;
            $url = "https://api.bilibili.com/x/space/arc/search?mid={$mediaId}&ps=30&tid=0&keyword=&order=pubdate&jsonp=jsonp&pn=";

            //        https://space.bilibili.com/475250/video
            for ($i = 1; $i < 10; $i++) {
                $curl = curl_init();

                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://api.bilibili.com/x/space/arc/search?mid={$mediaId}&ps=30&tid=0&keyword=&order=pubdate&jsonp=jsonp&pn={$i}",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                        "Accept-Encoding: gzip, deflate, br",
                        "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
                        "Cache-Control: no-cache",
                        "Connection: keep-alive",
                        "Referer: https://www.bilibili.com",
                        "Pragma: no-cache",
                        "Sec-Fetch-Mode: navigate",
                        "Sec-Fetch-Site: none",
                        "Sec-Fetch-User: ?1",
                        "Upgrade-Insecure-Requests: 1",
                        "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
                        "cache-control: no-cache"
                    ),
                ));

                $response = curl_exec($curl);
                $err = curl_error($curl);

                curl_close($curl);

                if ($err) {
                    echo "cURL Error #:" . $err;
                } else {
                    //                echo $response;
                    $result = json_decode($response, true);
                    $result = Arr::get($result["data"]["list"], "vlist", []);
                    if (count($result) > 0) {
                        foreach ($result as $vItem) {
                            $bVideo = BilibiliVideos::firstOrCreate(["aid" => $vItem["aid"]],
                                [
                                    "title" => $vItem["title"],
                                    "from_type" => 2,
                                    "from_up_name" => $item["up_name"]
                                ]);
                            if ($bVideo->from_type != 2) {
                                $bVideo->from_type = 3;
                            }
                            $bVideo->from_up_name = $item["up_name"];
                            $bVideo->save();
                            $videos[] = $vItem["aid"];
                        }
                    } else {
                        break;
                    }
                }
                usleep(random_int(1000, 10000) * 1000);
            }
            $item['videos'] = json_encode($videos);
            //            $item['downloaded_videos'] = json_encode([]);
            $item->save();
        }


    }

    /**
     * 手动执行查询
     * @param int $mediaId
     * @throws \Exception
     */
    public function queryLocalUpVideoList($mediaId = 320491072)
    {
        Log::info("schedule queryUpVideoList inner started at: " . date("Y-m-d H:i:s"));


        // 475250 kyokyo
        // 3489535 熊小颖
        // 320491072 绯绯
        // 10139490 短短
        // 116683 咬人猫
        // 16539048 小仙若
        // 10278125 香草猫饼
        // 391073761 女团直拍
//        $list = BilibiliUpVideos::all();
        $list = [];
        $up = ["mid" => 391073761, "up_name" => "女团直拍"];
        $list[] = $up;
        foreach ($list as $item) {
            Log::info("schedule queryUpVideoList current up is {$item['up_name']}, started at: " . date("Y-m-d H:i:s"));
            $files = scandir($this->baseDir . "bilibili/" . $item['up_name']);

            $mediaId = $item['mid'];
            $videos = [];
            $pageNo = 1;
            $url = "https://api.bilibili.com/x/space/arc/search?mid={$mediaId}&ps=30&tid=0&keyword=&order=pubdate&jsonp=jsonp&pn=";

            //        https://space.bilibili.com/475250/video
            for ($i = 1; $i < 50; $i++) {
                $curl = curl_init();

                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://api.bilibili.com/x/space/arc/search?mid={$mediaId}&ps=30&tid=0&keyword=&order=pubdate&jsonp=jsonp&pn={$i}",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                        "Accept-Encoding: gzip, deflate, br",
                        "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
                        "Cache-Control: no-cache",
                        "Connection: keep-alive",
                        "Referer: https://www.bilibili.com",
                        "Pragma: no-cache",
                        "Sec-Fetch-Mode: navigate",
                        "Sec-Fetch-Site: none",
                        "Sec-Fetch-User: ?1",
                        "Upgrade-Insecure-Requests: 1",
                        "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
                        "cache-control: no-cache"
                    ),
                ));

                $response = curl_exec($curl);
                $err = curl_error($curl);

                curl_close($curl);

                if ($err) {
                    echo "cURL Error #:" . $err;
                } else {
                    //                echo $response;
                    $result = json_decode($response, true);
                    $result = Arr::get($result["data"]["list"], "vlist", []);
                    if (count($result) > 0) {
                        foreach ($result as $vItem) {
                            echo "av" . $vItem["aid"] . " ";
//                            $videos[] = $vItem["aid"];
                        }
                    } else {
                        break;
                    }
                }
                usleep(random_int(1000, 10000) * 1000);
            }
//            $item['videos'] = json_encode($videos);
            //            $item['downloaded_videos'] = json_encode([]);
//            $item->save();
        }


    }

    public function compareAndDownloadUpVideos()
    {
        $list = BilibiliUpVideos::all();
        foreach ($list as $item) {
            if ($item["is_downloaded"] == 1) {
                continue;
            }
            Log::info("schedule compareAndDownloadUpVideos current up is {$item['up_name']}, started at: " . date("Y-m-d H:i:s"));
            $unDownloaded = array_diff(json_decode($item["videos"]), json_decode($item["downloaded_videos"]));
            $videoList = [];
            foreach ($unDownloaded as $value) {
                $vItems = BilibiliVideos::where('aid', $value)->get();
                if (count($vItems) > 0) {
                    foreach ($vItems as $vItem) {
                        if ($vItem["is_download"] == 1 && $vItem["is_downloaded"] == 0) {
                            $videoList[] = "av" . $value;
                        }
                    }
                }
//                $videoList[] = "av" . $value;
            }
            $this->downloadBSitePlaylist($videoList, $this->baseDir . "bilibili/", $item['up_name']);
        }
    }

    public function compareAndDownloadCollectionVideos()
    {
        $list = BilibiliCollections::all();
        foreach ($list as $item) {
            echo "current title is " . $item["title"] . "\n";
            if ($item["is_downloaded"] == 1) {
                continue;
            }
            Log::info("schedule compareAndDownloadCollectionVideos current collection is {$item['title']}, started at: " . date("Y-m-d H:i:s"));
            $newVideos = json_decode($item["videos"], true);
            $oldVideos = json_decode($item["downloaded_videos"], true);
            $unDownloaded = array_diff($newVideos, $oldVideos);
            if (count($unDownloaded) == 0) {
                continue;
            }
            dump($unDownloaded);
            $videoList = [];
            foreach ($unDownloaded as $value) {
                $vItems = BilibiliVideos::where('aid', $value)->get();
                if (count($vItems) > 0) {
                    foreach ($vItems as $vItem) {
                        if ($vItem["is_download"] == 1 && $vItem["is_downloaded"] == 0) {
                            $videoList[] = "av" . $value;
                        }
                    }
                }
            }
            if ($item['title'] == "默认收藏夹") {
                dump("xxxxxxxxxxxx----------------");
                $this->downloadBSitePlaylist($videoList, $this->baseDir, "bilibili");
            } else {
                $this->downloadBSitePlaylist($videoList, $this->baseDir . "bilibili/", $item['title']);
            }
        }
    }

    public function queryFavList()
    {
        // aoa 50076725
        // default 50069625
        // 美腿 156009025
        // 允儿 154442125
        // 少女时代 153944525
        $invalidTitle = "已失效视频";
        $pageNo = 1;


        for ($i = 1; $i < 65; $i++) {
            $curl = curl_init();
            $url = "https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id=154442125&pn={$i}&ps=20&keyword=&order=mtime&type=0&tid=0&jsonp=jsonp";
            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Accept: application/json, text/plain, */*",
                    "Accept-Encoding: gzip, deflate, br",
                    "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,ja;q=0.5",
                    "Cache-Control: no-cache",
                    "Connection: keep-alive",
                    //                    "Cookie: buvid3=5566647C-DDE5-4AFF-8711-89C9DB2B7061110244infoc; LIVE_BUVID=AUTO2315591376644842; sid=kbyrjw72; stardustvideo=1; CURRENT_FNVAL=16; rpdid=|(J~|JluJmmk0J\'ullmuJ~~kJ; CURRENT_QUALITY=80; UM_distinctid=16b11bb4099eb-033fd8b69a435d-37647e03-13c680-16b11bb40bf97; fts=1562149769; im_notify_type_279025=0; _uuid=69A60E94-E8FE-54B8-DCCD-0F519F0DEF7480343infoc; DedeUserID=279025; DedeUserID__ckMd5=9a79e15294e6b8bb; SESSDATA=b114a39d%2C1573361994%2C2ed0a2a1; bili_jct=b35f8e9780e4a80dd07f316f781f179b; bp_t_offset_279025=315386152995357818",
                    "Cookie: SESSDATA=64a15917%2C1578628130%2Ceb05cdc1",
                    "Host: api.bilibili.com",
                    "Origin: https://space.bilibili.com",
                    "Postman-Token: c7f849ec-aaf9-4c71-851d-4ca061e725d0,f8f582bc-4872-4aa7-b209-b926364d4d1a",
                    "Referer: https://space.bilibili.com/279025/favlist",
                    "Sec-Fetch-Mode: cors",
                    "Sec-Fetch-Site: same-site",
                    "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
                    "cache-control: no-cache"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                $responseArr = json_decode($response, true);
                if ($responseArr['code'] == 0) {
                    $data = $responseArr['data'];
                    $medias = $data['medias'];
                    if (count($medias) > 0) {
                        foreach ($medias as $item) {
                            if ($item['title'] != $invalidTitle) {
                                echo "av" . $item['id'] . " ";
                            }
                        }
                    }
                }
            }
            usleep(random_int(1000, 10000) * 1000);
        }


    }

    public function getAoaFavList($id = 50076725)
    {
        $invalidTitle = "已失效视频";
        $pageNo = 1;


        for ($i = 1; $i < 65; $i++) {
            $curl = curl_init();
            $url = "https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id=50076725&pn={$i}&ps=20&keyword=&order=mtime&type=0&tid=0&jsonp=jsonp";
            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Accept: application/json, text/plain, */*",
                    "Accept-Encoding: gzip, deflate, br",
                    "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,ja;q=0.5",
                    "Cache-Control: no-cache",
                    "Connection: keep-alive",
                    "Cookie: buvid3=5566647C-DDE5-4AFF-8711-89C9DB2B7061110244infoc; LIVE_BUVID=AUTO2315591376644842; sid=kbyrjw72; stardustvideo=1; CURRENT_FNVAL=16; rpdid=|(J~|JluJmmk0J'ullmuJ~~kJ; CURRENT_QUALITY=80; UM_distinctid=16b11bb4099eb-033fd8b69a435d-37647e03-13c680-16b11bb40bf97; fts=1562149769; im_notify_type_279025=0; _uuid=69A60E94-E8FE-54B8-DCCD-0F519F0DEF7480343infoc; DedeUserID=279025; DedeUserID__ckMd5=9a79e15294e6b8bb; SESSDATA=b114a39d%2C1573361994%2C2ed0a2a1; bili_jct=b35f8e9780e4a80dd07f316f781f179b; bp_t_offset_279025=317592473398470273",
                    "Host: api.bilibili.com",
                    "Origin: https://space.bilibili.com",
                    "Postman-Token: c7f849ec-aaf9-4c71-851d-4ca061e725d0,f8f582bc-4872-4aa7-b209-b926364d4d1a",
                    "Referer: https://space.bilibili.com/279025/favlist",
                    "Sec-Fetch-Mode: cors",
                    "Sec-Fetch-Site: same-site",
                    "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
                    "cache-control: no-cache"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                $responseArr = json_decode($response, true);
                if ($responseArr['code'] == 0) {
                    $data = $responseArr['data'];
                    $medias = $data['medias'];
                    if (count($medias) > 0) {
                        foreach ($medias as $item) {
                            if ($item['title'] != $invalidTitle) {
                                echo "av" . $item['id'] . " ";
                            }
                        }
                    }
                }
            }
            usleep(random_int(1000, 10000) * 1000);
        }
    }

    public function queryCollectionList()
    {


        //        $curl = curl_init();
        //
        //        curl_setopt_array($curl, array(
        //            CURLOPT_URL => "https://api.bilibili.com/medialist/gateway/base/created?pn=1&ps=100&up_mid=279025&is_space=0&jsonp=jsonp",
        //            CURLOPT_RETURNTRANSFER => true,
        //            CURLOPT_ENCODING => "",
        //            CURLOPT_MAXREDIRS => 10,
        //            CURLOPT_TIMEOUT => 0,
        //            CURLOPT_FOLLOWLOCATION => true,
        //            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        //            CURLOPT_CUSTOMREQUEST => "GET",
        //            CURLOPT_HTTPHEADER => array(
        //                "Connection: keep-alive",
        //                "Pragma: no-cache",
        //                "Cache-Control: no-cache",
        //                "Accept: application/json, text/plain, */*",
        //                "Origin: https://space.bilibili.com",
        //                "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
        //                "Sec-Fetch-Site: same-site",
        //                "Sec-Fetch-Mode: cors",
        //                "Referer: https://space.bilibili.com/279025/favlist?spm_id_from=333.851.b_696e7465726e6174696f6e616c486561646572.15",
        //                "Accept-Encoding: gzip, deflate, br",
        //                "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
        //                "Cookie: _uuid=D6E5438B-5A95-439F-7512-FC3509457A9A28409infoc; buvid3=5566647C-DDE5-4AFF-8711-89C9DB2B7061110244infoc; LIVE_BUVID=AUTO3415734420289108; UM_distinctid=16e62db8b9934b-0f76f63ae51bee-1c3c6a5a-13c680-16e62db8b9acaf; CURRENT_FNVAL=16; stardustvideo=1; rpdid=|(ku|l|lRYlJ0J'ul~JYuYY|u; finger=0539dad4; im_notify_type_279025=0; sid=llgc5h9q; DedeUserID=279025; DedeUserID__ckMd5=9a79e15294e6b8bb; bili_jct=07b8811aa5ca68020eb0c11e0e01c22f; laboratory=1-1; CURRENT_QUALITY=80; SESSDATA=64a15917%2C1578628130%2Ceb05cdc1; bp_t_offset_279025=336855788264916597; INTVER=1"
        //            ),
        //        ));
        //
        //        $response = curl_exec($curl);
        $response = $this->requestCollectionNumberList($this->collectionNumberListUrl, 1);

        $res = json_decode($response, true);
        if ($res['code'] == 0) {
            $list = $res['data']['list'];
            foreach ($list as $item) {
                $collection = new BilibiliCollections();
                $collection->title = $item["title"];
                $collection->is_downloaded = 0;
                $collection->media_id = $item["id"];
                $currentList = [];
                for ($pageNo = 1; $pageNo < 100; $pageNo++) {
                    $listResponse = $this->requestCollectionList($this->collectionListUrl, $item['id'], $pageNo);
                    dump("pageNo: " . $pageNo);
                    dump($listResponse);
                    $responseArr = json_decode($listResponse, true);
                    if ($responseArr['code'] == 0) {
                        $data = $responseArr["data"];
                        $medias = Arr::get($data, "medias", []);
                        if (count($medias) > 0) {
                            foreach ($medias as $media) {
                                if ($media['title'] != $this->invalidTitle) {
                                    $currentList[] = $media["id"];
                                }
                            }
                        } else {
                            break;
                        }
                    }
                }
                $collection->videos = json_encode($currentList);
                $collection->save();
            }
        }

        //        curl_close($curl);
        echo $response;

    }

    public function queryDBCollectionList()
    {
        $list = BilibiliCollections::all();
        foreach ($list as $item) {
            Log::info("schedule queryDBCollectionList current collection is: " . $item['title'] . " started at :" . date("Y-m-d H:i:s"));
            $currentList = [];
            for ($pageNo = 1; $pageNo < 100; $pageNo++) {
                $listResponse = $this->requestCollectionList($this->collectionListUrl, $item['media_id'], $pageNo);
//                dump("pageNo: " . $pageNo);
//                dump($listResponse);
                $responseArr = json_decode($listResponse, true);
                if ($responseArr['code'] == 0) {
                    $data = $responseArr["data"];
                    $medias = Arr::get($data, "medias", []);
                    if (count($medias) > 0) {
                        foreach ($medias as $media) {
                            if ($media['title'] != $this->invalidTitle) {
                                $bVideo = BilibiliVideos::firstOrCreate(["aid" => $media["id"]],
                                    [
                                        "title" => $media["title"],
                                        "from_type" => 1,
                                        "from_collection_name" => $item['title']
                                    ]);
                                if ($bVideo->from_type != 1) {
                                    $bVideo->from_type = 3;
                                    $bVideo->from_collection_name = $item["title"];
                                }
                                $bVideo->save();
                                $currentList[] = $media["id"];
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
            $item->videos = json_encode($currentList);
            $item->save();
        }
    }

    public function queryForVideoParts()
    {
        $i = 1;
        $list = BilibiliVideos::simplePaginate(50, null, 'page', $i);
//        dump($list->items()[0]->aid);
        while ($list->isNotEmpty()) {
            foreach ($list->items() as $item) {
                dump("current item", [$item->getAttributes()]);
                $response = $this->requestVideoParts($item->aid);
                $partList = Arr::get(json_decode($response, true), "data");
                if (count($partList) > 0) {
                    foreach ($partList as $part) {
                        if ($item["from_type"] == 1) {
                            if ($item["from_collection_name"] == "默认收藏夹") {
                                $downloadPath = $this->baseDir . "bilibili";
                            } else {
                                $downloadPath = $this->baseDir . "bilibili/" . $item["from_collection_name"];
                            }
                        } else {
                            $downloadPath = $this->baseDir . "bilibili/" . $item["from_up_name"];
                        }
                        $pItem = BilibiliVideoParts::firstOrCreate([
                            "aid" => $item->aid,
                            "page" => $part["page"]
                        ], [
                            "title" => $item["title"],
                            "part" => $part["part"],
                            "duration" => $part["duration"],
                            "to_download_path" => $downloadPath
                        ]);
                        $pItem["title"] = $item["title"];
                        $pItem["to_download_path"] = $downloadPath;
                        $pItem->save();
                    }
                    $item->total_parts = count($partList);
                    $item->save();
                }

            }
            $i++;
            $list = BilibiliVideos::simplePaginate(50, null, 'page', $i);
            try {
                usleep(random_int(10, 1000) * 1000);
            } catch (\Exception $e) {
            }
        }

//        while ($list->hasPages()) {
//            foreach ($list as $item) {
//                $item->
//            }
//            $list = BilibiliVideos::simplePaginate()
//        }
    }

    /**
     * 检查是否已下载
     */
    public function checkVideoHasDownload()
    {
        $i = 1;
        $list = BilibiliVideos::simplePaginate(50, null, 'page', $i);
        $dirFiles = [];
        while ($list->isNotEmpty()) {
            foreach ($list->items() as $item) {
                dump("current item", [$item->getAttributes()]);
                if ($item['is_download'] == 0) {
                    continue;
                }
                $list = BilibiliVideoParts::where(['aid' => $item['aid']])->get();
                if (count($list) > 0) {
                    foreach ($list as $part) {
//                        dump("here is parts", [$part->getAttributes()]);
                        if (is_dir($part["to_download_path"])) {
//                            dump("this is path ".$part["to_download_path"]);
                            if (array_key_exists($part["to_download_path"], $dirFiles)) {
                                $files = Arr::get($dirFiles, $part["to_download_path"]);
                            } else {
                                $files = scandir($part["to_download_path"]);
                                $dirFiles[$part["to_download_path"]] = $files;
                            }
//                            dump($files);
                            $itemCount = 0;
                            $toRemove = [];
                            foreach ($files as $file) {
                                if ($file == "." || $file == "..") {
                                    continue;
                                } else {
//                                    dump("current file: ". $file ." ".$part["title"]);
                                    $pos = strpos($file, $part["title"]);
//                                    dump("current compare result " . $pos);
                                    if ($pos !== false) {
//                                        dump($file);
                                        $itemCount++;
                                        if (strpos($file, $part["title"] . " P" . $part["page"]) !== false) {
                                            Log::info("file matched " . $file);
                                            $toRemove[] = $file;
                                            $part["is_downloaded"] = 1;
                                            $part->save();
                                        }
                                    }
                                }
                            }
                            $dirFiles[$part["to_download_path"]] = array_diff($files, $toRemove);
                            if ($itemCount == $item["total_parts"]) {
                                $item["is_downloaded"] = 1;
                                $item->save();
                            }
                        }
                    }
                }
//                exit;
            }
            $i++;
            $list = BilibiliVideos::simplePaginate(50, null, 'page', $i);
//            exit;
        }
    }

    public function requestVideoParts($aid)
    {
        return $this->request($this->videoPartsUrl . $aid);
    }

    public function requestCollectionList($url, $mediaId, $pn)
    {
        return $this->request($url . "&media_id=" . $mediaId . "&pn=" . $pn, true);
    }

    public function requestCollectionNumberList($url, $pn)
    {
        return $this->request($url . "&pn=" . $pn);
    }

    public function request($url, $userCookie = false)
    {
        $curl = curl_init();

        $headerArray = array(
            "Connection: keep-alive",
            "Pragma: no-cache",
            "Cache-Control: no-cache",
            "Accept: application/json, text/plain, */*",
            "Origin: https://space.bilibili.com",
            "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
            "Sec-Fetch-Site: same-site",
            "Sec-Fetch-Mode: cors",
            "Referer: https://space.bilibili.com/279025/favlist?spm_id_from=333.851.b_696e7465726e6174696f6e616c486561646572.15",
            "Accept-Encoding: gzip, deflate, br",
            "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
            //                "Cookie: SESSDATA=64a15917%2C1578628130%2Ceb05cdc1"
        );
        if ($userCookie) {
            $headerArray[] = "Cookie: SESSDATA=b169300a%2C1581262828%2C3654f611;";
//            $headerArray[] = "Cookie: _uuid=D6E5438B-5A95-439F-7512-FC3509457A9A28409infoc; buvid3=5566647C-DDE5-4AFF-8711-89C9DB2B7061110244infoc; LIVE_BUVID=AUTO3415734420289108; UM_distinctid=16e62db8b9934b-0f76f63ae51bee-1c3c6a5a-13c680-16e62db8b9acaf; CURRENT_FNVAL=16; stardustvideo=1; rpdid=|(ku|l|lRYlJ0J'ul~JYuYY|u; im_notify_type_279025=0; sid=llgc5h9q; laboratory=1-1; CURRENT_QUALITY=80; INTVER=1; DedeUserID=279025; DedeUserID__ckMd5=9a79e15294e6b8bb; SESSDATA=b169300a%2C1581262828%2C3654f611; bili_jct=597a5b9adb6170698e396fb053bc4aba; bp_t_offset_279025=343238964368145599";
        }

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => $headerArray
        ));

        $response = curl_exec($curl);

        $err = curl_error($curl);
        curl_close($curl);

        if ($err) {
            \Log::error($err);
            return null;
        }
        return $response;
    }

    public function downloadBSitePlaylist($aidList, $dir = "/Volumes/intel660p/video/mv/mp4", $subDir)
    {
        dump("downloadBSitePlaylist : xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        $dirExists = is_dir($dir);
        if ($dirExists) {
            $innerDir = $dir . "/" . $subDir;
            if (!is_dir($innerDir)) {
                mkdir($innerDir);
            }
            foreach ($aidList as $aid) {
                Log::info("current download command is : cd {$innerDir} && annie -d -r https://www.bilibili.com/video/av80815149  -p " . $aid);
                $downloadResult = shell_exec("cd '{$innerDir}' && annie -d -r https://www.bilibili.com/video/av80815149 -p " . $aid);
                Log::info($downloadResult);
                Log::info("current download result: " . $downloadResult);
                try {
                    usleep(random_int(1000, 10000) * 1000);
                } catch (\Exception $e) {
                }
            }
        }
    }

    public function deleteFileNotContainP()
    {
        $dir = "/Volumes/intel660p/video/mv/mp4";
        $files = scandir($dir);
        foreach ($files as $file) {
            if ($file == "." || $file == "..") {
                continue;
            }
            if (preg_match("#P\d{1,2}#", $file)) {
                echo $file . "\n";
            } else {
                echo "to delete file " . $file . "\n";
                unlink($dir . "/" . $file);
            }
        }
    }

    public function compareAndDownloadNewVideos()
    {
        $list = BilibiliCollections::all();
        foreach ($list as $item) {
            if ($item['is_downloaded'] == 1) {
                continue;
            }
            $undownloaded = array_diff($item['videos'], $item['downloaded_videos']);
        }
        dump($list);
    }

    public function commonRequest($url, $cookie, $media_id, $pageNo)
    {
        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => 'https://api.bilibili.com',
            // You can set any number of default request options.
            'timeout' => 2.0,
        ]);
        $jar = new \GuzzleHttp\Cookie\CookieJar;
        $r = $client->request('GET', 'http://httpbin.org/cookies', [
            'cookies' => $jar
        ]);
    }
}
