Passed
Push — v6 ( 32f4f3...27c58a )
by 光春
03:39
created

WatermarkService::getContents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 27
rs 9.568
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
namespace DtApp\ThinkLibrary\service\kuaishou;
21
22
use DtApp\ThinkLibrary\facade\Pregs;
23
use DtApp\ThinkLibrary\Service;
24
use GuzzleHttp\Client;
25
use GuzzleHttp\Cookie\CookieJar;
26
use GuzzleHttp\Psr7\Stream;
27
28
/**
29
 * 快手-视频去水印
30
 * Class WatermarkService
31
 * @package DtApp\ThinkLibrary\service\kuaishou
32
 */
33
class WatermarkService extends Service
34
{
35
    /**
36
     * @var
37
     */
38
    private $url, $contents, $backtrack;
39
40
    /**
41
     * 设置网址
42
     * @param $url
43
     * @return $this
44
     */
45
    public function setUrl($url): self
46
    {
47
        if (Pregs::isLink($url)) {
48
            $this->url = $url;
49
        } else {
50
            preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $url, $match);
51
            $this->url = $match[0][0];
52
        }
53
        return $this;
54
    }
55
56
    /**
57
     * 获取接口全部信息
58
     * @return WatermarkService
59
     * @throws \GuzzleHttp\Exception\GuzzleException
60
     */
61
    private function getApi(): self
62
    {
63
        $this->contents = $this->getContents($this->url);
64
        return $this;
65
    }
66
67
    /**
68
     * 获取全部信息
69
     * @return $this
70
     * @throws \GuzzleHttp\Exception\GuzzleException
71
     */
72
    public function getAll(): self
73
    {
74
        $this->getApi();
75
        $data = [
76
            'video_src' => $this->contents['video_src'],
77
            'cover_image' => $this->contents['cover_image'],
78
        ];
79
        $this->backtrack = $data;
80
        return $this;
81
    }
82
83
    /**
84
     * 返回Array
85
     * @return array|mixed
86
     */
87
    public function toArray()
88
    {
89
        if (empty($this->backtrack)) {
90
            return [];
91
        }
92
        if (is_array($this->backtrack)) {
93
            return $this->backtrack;
94
        }
95
        return json_decode($this->backtrack, true);
96
    }
97
98
    /**
99
     * 获取
100
     * @param $url
101
     * @return array
102
     * @throws \GuzzleHttp\Exception\GuzzleException
103
     */
104
    private function getContents($url): array
105
    {
106
        $headers = [
107
            'Connection' => 'keep-alive',
108
            'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1'
109
        ];
110
        $client = new Client(['timeout' => 2, 'headers' => $headers, 'http_errors' => false,]);
111
        $data['headers'] = $headers;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
112
        $data['verify'] = __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem';
113
        $jar = new CookieJar();
114
        $data['cookies'] = $jar;
115
        $response = $client->request('GET', $url, $data);
116
        $body = $response->getBody();
117
        if ($body instanceof Stream) {
118
            $body = $body->getContents();
119
        }
120
        $result = htmlspecialchars_decode($body);
121
        $pattern = '#"srcNoMark":"(.*?)"#';
122
        preg_match($pattern, $result, $match);
123
        $data['video_src'] = $match[1];
124
        $pattern = '#"poster":"(.*?)"#';
125
        preg_match($pattern, $result, $match);
126
        if (!empty($match[1])) {
127
            $data['cover_image'] = $match[1];
128
            return $data;
129
        }
130
        return [];
131
    }
132
}