-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
149 lines (134 loc) · 4.74 KB
/
process.php
File metadata and controls
149 lines (134 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* User: Yejia
* Email: ye91@foxmail.com
*/
//初始化变量
$requestHost = 'huaban.com';
$requestUrl = '/partner/uc/aimeinv/pins?limit=10&wfl=1';
$imageHost = 'hbimg.b0.upaiyun.com';
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'images/';
//初始化管道
$requestChan = new Chan(500);
$parserChan = new Chan(500);
$downloadChan = new Chan(500);
//入口开始
go(function () use ($requestChan, $requestHost, $requestUrl) {
$requestChan->push([
'host' => $requestHost,
'url' => $requestUrl
]);
});
for ($p = 1; $p <= swoole_cpu_num() * 2; $p++) {
$pro = new Swoole\Process(function (\Swoole\Process $worker) use ($requestChan, $parserChan, $downloadChan, $requestHost, $requestUrl, $dir, $imageHost) {
for ($w = 1; $w <= 100; $w++) {
go(function () use ($requestChan, $parserChan) {
while (true) {
$data = $requestChan->pop();
get_html($data['host'], $data['url'], $parserChan);
}
});
go(function () use ($parserChan, $requestChan, $downloadChan, $requestHost, $requestUrl) {
while (true) {
$html = $parserChan->pop();
parse($html, $downloadChan, $requestChan, $requestHost, $requestUrl);
}
});
go(function () use ($downloadChan, $dir, $imageHost, $worker) {
while (true) {
download($downloadChan->pop(), $dir, $imageHost, $worker->pid);
}
});
}
});
$pro->start();
}
\Swoole\Process::wait();
/**
* 获取内容
*
* @param string $url Url地址
* @param Chan $chan 解析管道
*/
function get_html(string $host, string $url, Chan $chan)
{
$client = new \Swoole\Coroutine\Http\Client($host, 443, true);
$client->setHeaders([
'Host' => $host,
"User-Agent" => getUserAgent(),
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
]);
$client->get($url);
$chan->push($client->body);
}
/**
* 下载文件
*
* @param string $url 文件Url
* @param string $dir 存储目录
* @param string $imageHost 文件基础Url
* @param int $pid PID
*/
function download(string $url, string $dir, string $imageHost, int $pid)
{
if (!is_dir($dir)) {
mkdir($dir);
}
if (!strpos($url, '-')) {
return;
}
$client = new \Swoole\Coroutine\Http\Client($imageHost, 80, false);
$client->setHeaders([
'Host' => $imageHost,
"User-Agent" => getUserAgent(),
]);
$client->download(DIRECTORY_SEPARATOR . $url, $dir . $url . '.png');
echo sprintf("PID: %d, %s\n", $pid, $imageHost . DIRECTORY_SEPARATOR . $url);
}
/**
* 解析数据
*
* @param string $html html内容
* @param Chan $downloadChan 下载管道
* @param Chan $requestChan 请求管道
* @param string $requestHost 请求Url
* @param string $requestUrl 请求Url
*/
function parse(string $html, Chan $downloadChan, Chan $requestChan, string $requestHost, string $requestUrl)
{
//解析图片
preg_match_all('/"key":"(.*?)"/', $html, $images);
if (!empty($images) && !empty($images[1])) {
foreach ($images[1] as $image) {
$downloadChan->push($image);
}
}
//解析下一页
preg_match_all('/"pin_id":(\d+),/', $html, $next);
if (!empty($next) && !empty($next[1])) {
$requestChan->push([
'host' => $requestHost,
'url' => $requestUrl . "&max=" . end($next[1])
]);
}
}
/**
* @return mixed
*/
function getUserAgent()
{
if (!empty($_SERVER['HTTP_USER_AGENT'])) return $_SERVER['HTTP_USER_AGENT'];
$userAgents = [
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
];
return $userAgents[array_rand($userAgents, 1)];
}