-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeal.php
70 lines (67 loc) · 1.77 KB
/
deal.php
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
<?php
set_time_limit(0);
$redownload = function ($id)
{
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/download/xiami/api.php');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// time out
curl_setopt($ch, CURLOPT_TIMEOUT, 1200);
// post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['action'=>'download', 'songId' => $id]);
// grab URL and pass it to the browser
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
echo $response. "\n";
ob_flush();
};
$dir = dirname(__FILE__). '/mp3/';
$d = dir($dir);
$songIds = [];
while (false !== ($entry = $d->read())) {
if ($entry{0} === '.') {
continue;
}
preg_match("/^\d+/", $entry, $match);
if (empty($match) or empty($match[0])) {
continue;
}
$songIds[$match[0]] = $match[0];
}
$d->close();
ob_start();
foreach ($songIds as $id) {
usleep(1000);
$redownload($id);
}
// 移除冗余文件
$dir = dirname(__FILE__). '/mp3/';
$d = dir($dir);
$songIds = [];
while (false !== ($entry = $d->read())) {
if ($entry{0} === '.') {
continue;
}
preg_match("/^\d+/", $entry, $match);
if (empty($match) or empty($match[0])) {
continue;
}
if (isset($songIds[$match[0]])) {
$f1 = $dir. $songIds[$match[0]];
$f2 = $dir. $entry;
if (filemtime($f1) > filemtime($f2)) {
unlink($f2);
} else {
unlink($f1);
$songIds[$match[0]] = $entry;
}
}
}
$d->close();