<?php


namespace app\utils;


class HaoKanUtil
{

    # 配置
    protected $option = [
        'url' => 'https://sv.baidu.com/',
        'header' => [
            'referer: https://sv.baidu.com/',
            'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
        ],
    ];
    # dom
    protected $xp;
    # 分类
    public $fenlei=[];
    # 获取到的视频链接
    protected $vsrc=[];

    public function __construct($option=[])
    {
        $this->option = array_merge($this->option,$option);
    }


    # 这里你们不需要了
    # 创建hexo文章
    public function create_hexo(){
        $this->get_active_sp();
        $title = '好看短视频'.date('Ymd');
        $date = date('Y-m-d H:i:s');
        $md = <<<MDTITLE
title: $title
entitle: ''
author: lzz
avatar: /images/favicon.ico
authorLink: 'https://lzz1.top'
authorAbout: 'https://lzz1.top'
authorDesc: "另一个搬\U0001F9F1的劳斯基\U0001F601️️"
categories: 采集
date: $date
tags:
    - 采集
keywords: 采集
description: 好看短视频采集
photos:
---
 
MDTITLE;
        foreach ($this->vsrc as $v){
            $md .= <<<MDCONTENT
### {$v['name']}\n
MDCONTENT;
            foreach ($v['list'] as $kk=>$vv){
                if(!empty($vv)){
                    $md .= <<<MDLIST
> <video style="width:50%;max-height:240px" controls><source src="$vv" type="video/mp4"/></video>\n
MDLIST;
                }
            }

        }
        //file_put_contents('source/_posts/'.$title.'.md',$md);
    }

    # 获取分类下的短视频链接
    public function get_active_sp(){
        $this->get_fenlei();
        foreach ($this->fenlei as $k=>$f){
            $href = str_replace('#','',$f['href']);
            $url = "https://sv.baidu.com/videoui/list/tab?source=wise-channel&pd=&subTab=$href&caller=bdwise";
            $list = $this->http_curl($url);
            preg_match_all('/(?<=data-vsrc=).*?.mp4/',$list,$arr);
            $this->vsrc[$k] = [
                'href' => $f['href'],
                'name' => $f['name'],
                'list' => [

                ]
            ];
            foreach ($arr[0] as $a){
                if(strlen($a)>255){
                    unset($a);
                }
                if(isset($a)){
                    array_push($this->vsrc[$k]['list'],str_replace('\"','',$a));
                }
            }
            /*preg_match('/"list":[\s\S]*?,"hasMore"/i', $list, $arr);
            $result=json_decode(substr($arr[0],7,-10));*/
        }
        return $this->vsrc;
    }
    # 获取分类
    public function get_fenlei(){
        $list = $this->c_dom($this->option['url'])->xp->query('//div[@class="video-tabs-wrapper"]/ul/li/a');
        for($i=0;$i<$list->length;$i++){
            array_push($this->fenlei,[
                'href' => $list->item($i)->getAttribute('href'),
                'name' => $list->item($i)->textContent
            ]);
        }
        return $this;
    }

    # 创建Dom
    public function c_dom($url){
        $dom = new \DOMDocument('1.0','utf-8');
        $html = $this->http_curl($url);
        @$dom->loadHTML($html);
        $dom->normalize();
        $xp = new \DOMXPath($dom);
        $this->xp = $xp;
        return $this;
    }
    #
    public function http_curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//要访问的url
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https网站取消ssl验证
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);//允许30*跳转
        curl_setopt($ch, CURLOPT_TIMEOUT,30);//设置超时时间
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this->option['header']);
        $response = curl_exec($ch);
        if ($response === false) {
            $error_info =  curl_error($ch);
            echo $error_info;
            curl_close($ch);//关闭curl
            return false;
        } else {
            //echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
            curl_close($ch);//关闭curl
            return $response;
        }
    }
}