<?php


namespace App\Services;

class BilibiliService {

    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 < 22; $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++;
        }
    }

    public function queryUpVideoList($mediaId = 475250)
    {
        $pageNo = 1;
        $url = "https://api.bilibili.com/x/space/arc/search?mid=475250&ps=30&tid=0&keyword=&order=pubdate&jsonp=jsonp&pn=";

//        https://space.bilibili.com/475250/video
        for ($i= 1; $i < 22; $i++) {
            # code...


            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.bilibili.com/x/space/arc/search?mid=475250&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);
                foreach ($result['data']['list']['vlist'] as $item) {
                    echo "av" . $item['aid'] . " ";
                }
            }
            exit;

            //            echo $result['data']['vlist'][0]['aid'] . "\t";
            //            $pageNo++;
        }

    }

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


        for ($i = 1; $i < 65; $i++) {
            $curl = curl_init();
            $url = "https://api.bilibili.com/medialist/gateway/base/spaceDetail?media_id=50069625&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=6cf335a1%2C1576033995%2C69d8d2b1",
                    "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'] . "\t";
                            }
                        }
                    }
                }
            }
            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 downloadBSitePlaylist($aid, $dir = "/Volumes/intel660p/video/mv/mp4")
    {
        $dirExists = is_dir($dir);
        if ($dirExists) {
            echo shell_exec("cd {$dir} && annie -n 100 -p " .$aid);
        }
    }

    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);
            }
        }
    }
}
