优化zm下载功能。
This commit is contained in:
@@ -7,6 +7,7 @@ use think\console\input\Option;
|
|||||||
use think\console\input\Argument;
|
use think\console\input\Argument;
|
||||||
use think\console\input\InputOption;
|
use think\console\input\InputOption;
|
||||||
use Aria2;
|
use Aria2;
|
||||||
|
use think\facade\Cache;
|
||||||
use think\facade\Log;
|
use think\facade\Log;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
@@ -104,7 +105,11 @@ class Zm extends Command
|
|||||||
|
|
||||||
$path = parse_url($enclosure['url'], PHP_URL_PATH);
|
$path = parse_url($enclosure['url'], PHP_URL_PATH);
|
||||||
$parts = explode('/', $path);
|
$parts = explode('/', $path);
|
||||||
$this->download($enclosure['url'],$class_path . '/' . urldecode($parts[count($parts)-2]));
|
$storage = $class_path . '/' . urldecode($parts[count($parts)-2]);
|
||||||
|
is_dir($storage) || mkdir($storage,0755,true);
|
||||||
|
$downloadurl = $storage . '/' . urldecode(end($parts)) . '-下载地址.txt';
|
||||||
|
$this->savetxt($downloadurl,urldecode($enclosure['url']));
|
||||||
|
// $this->download($enclosure['url'],$storage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,8 +128,82 @@ class Zm extends Command
|
|||||||
}
|
}
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
private function checkfile($url,$file)
|
||||||
|
{
|
||||||
|
dump($url,$file);
|
||||||
|
$key = 'zm_check_header_' . md5($url);
|
||||||
|
try {
|
||||||
|
if(($head = Cache::get($key,null)) === NULL ){
|
||||||
|
$head = [];
|
||||||
|
|
||||||
|
$client = new Client(['timeout' => 5.0,]);
|
||||||
|
try {
|
||||||
|
// 发送 HEAD 请求,并添加自定义请求头
|
||||||
|
$response = $client->head($url, [
|
||||||
|
'headers' => [
|
||||||
|
'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36 Core/1.116.489.400 QQBrowser/13.7.6351.400',
|
||||||
|
'referer'=>'https://edu.zmrobo.com/courses',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 读取响应状态码
|
||||||
|
$statusCode = $response->getStatusCode();
|
||||||
|
if($statusCode != 200){
|
||||||
|
dump('文件head请求失败,状态码:'.$statusCode);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// 获取全部响应头(返回关联数组)
|
||||||
|
$headers = $response->getHeaders();
|
||||||
|
foreach ($headers as $name => $values) {
|
||||||
|
$head[$name] = implode(', ', $values);
|
||||||
|
}
|
||||||
|
dump($head);
|
||||||
|
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
||||||
|
dump("文件head请求失败: " . $e->getMessage());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Cache::set($key,$head,86400);
|
||||||
|
}
|
||||||
|
$kk = 'zm_file_' . pathinfo($file,PATHINFO_BASENAME);
|
||||||
|
if(isset($head['Content-Length']) && ($size = intval($head['Content-Length']))){
|
||||||
|
$kk .= $size;
|
||||||
|
if(is_file($file) && ($size !== filesize($file))){
|
||||||
|
dump('文件大小不符,重新下载'.$file);
|
||||||
|
unlink($file);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isset($head['ETag'])){
|
||||||
|
$t = $kk . md5($head['ETag']);
|
||||||
|
if(Cache::has($t)){
|
||||||
|
return Cache::get($t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(isset($head['Content-MD5'])){
|
||||||
|
$t = $kk . md5($head['Content-MD5']);
|
||||||
|
if(Cache::has($t)){
|
||||||
|
return Cache::get($t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
dump('检查文件失败:',$url,$file,$e);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
protected function download($downloadurl,$dir,$filename = null)
|
protected function download($downloadurl,$dir,$filename = null)
|
||||||
{
|
{
|
||||||
|
$num = 1;
|
||||||
|
while($num > 0){
|
||||||
|
$activeDownloads = $this->aria2->getGlobalStat();
|
||||||
|
$num = intval($activeDownloads['result']['numWaiting']);
|
||||||
|
if($num > 10){
|
||||||
|
dump('当前任务过多,等待任务数:',$num);
|
||||||
|
sleep(1);
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
$result = $this->aria2->addUri(
|
$result = $this->aria2->addUri(
|
||||||
[$downloadurl],
|
[$downloadurl],
|
||||||
['dir'=>$dir,
|
['dir'=>$dir,
|
||||||
|
|||||||
Reference in New Issue
Block a user