WatermarkService::getApi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
15
// +----------------------------------------------------------------------
16
17
namespace DtApp\ThinkLibrary\service\kuaishou;
18
19
use DtApp\ThinkLibrary\facade\Pregs;
20
use DtApp\ThinkLibrary\Service;
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Cookie\CookieJar;
23
use GuzzleHttp\Psr7\Stream;
24
25
/**
26
 * 快手-视频去水印
27
 * Class WatermarkService
28
 * @package DtApp\ThinkLibrary\service\kuaishou
29
 */
30
class WatermarkService extends Service
31
{
32
    /**
33
     * @var
34
     */
35
    private $url, $contents, $backtrack;
36
37
    /**
38
     * 设置网址
39
     * @param $url
40
     * @return $this
41
     */
42
    public function setUrl($url): self
43
    {
44
        if (Pregs::isLink($url)) {
45
            $this->url = $url;
46
        } else {
47
            preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $url, $match);
48
            $this->url = $match[0][0];
49
        }
50
        return $this;
51
    }
52
53
    /**
54
     * 获取接口全部信息
55
     * @return WatermarkService
56
     * @throws \GuzzleHttp\Exception\GuzzleException
57
     */
58
    private function getApi(): self
59
    {
60
        $this->contents = $this->getContents($this->url);
61
        return $this;
62
    }
63
64
    /**
65
     * 获取全部信息
66
     * @return $this
67
     * @throws \GuzzleHttp\Exception\GuzzleException
68
     */
69
    public function getAll(): self
70
    {
71
        $this->getApi();
72
        $data = [
73
            'video_src' => $this->contents['video_src'],
74
            'cover_image' => $this->contents['cover_image'],
75
        ];
76
        $this->backtrack = $data;
77
        return $this;
78
    }
79
80
    /**
81
     * 返回Array
82
     * @return array|mixed
83
     */
84
    public function toArray()
85
    {
86
        if (empty($this->backtrack)) {
87
            return [];
88
        }
89
        if (is_array($this->backtrack)) {
90
            return $this->backtrack;
91
        }
92
        return json_decode($this->backtrack, true);
93
    }
94
95
    /**
96
     * 获取
97
     * @param $url
98
     * @return array
99
     * @throws \GuzzleHttp\Exception\GuzzleException
100
     */
101
    private function getContents($url): array
102
    {
103
        $headers = [
104
            'Connection' => 'keep-alive',
105
            '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'
106
        ];
107
        $client = new Client(['timeout' => 2, 'headers' => $headers, 'http_errors' => false,]);
108
        $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...
109
        $data['verify'] = __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem';
110
        $jar = new CookieJar();
111
        $data['cookies'] = $jar;
112
        $response = $client->request('GET', $url, $data);
113
        $body = $response->getBody();
114
        if ($body instanceof Stream) {
115
            $body = $body->getContents();
116
        }
117
        $result = htmlspecialchars_decode($body);
118
        $pattern = '#"srcNoMark":"(.*?)"#';
119
        preg_match($pattern, $result, $match);
120
        $data['video_src'] = $match[1];
121
        $pattern = '#"poster":"(.*?)"#';
122
        preg_match($pattern, $result, $match);
123
        if (!empty($match[1])) {
124
            $data['cover_image'] = $match[1];
125
            return $data;
126
        }
127
        return [];
128
    }
129
}