优化下载逻辑,已下载文件不进行下载,使用时请删除download方法的return

This commit is contained in:
2025-04-18 09:37:42 +08:00
parent f2dda4f354
commit 833fe44281

View File

@@ -74,7 +74,7 @@ class Zm extends Command
$this->savetxt($introduce,$curr_item['introduce']); $this->savetxt($introduce,$curr_item['introduce']);
$cover = $curr_item['cover']; $cover = $curr_item['cover'];
$this->download($cover,$c); $this->download($cover,$c,basename($cover));
$cover_name = basename($cover); $cover_name = basename($cover);
$cover_path = "{$c}/{$cover_name}-下载地址.txt"; $cover_path = "{$c}/{$cover_name}-下载地址.txt";
@@ -84,20 +84,36 @@ class Zm extends Command
$class_path = $c . '/' . $class['title']; $class_path = $c . '/' . $class['title'];
is_dir($class_path) || mkdir($class_path,0755,true); is_dir($class_path) || mkdir($class_path,0755,true);
$class_details = $this->getClassDetails($class['id']); $class_details = $this->getClassDetails($class['id']);
if($class_details == false){
dump('课程未授权,跳过下载',$class['title']);
break;
}
dump($class_path,$class_details); dump($class_path,$class_details);
$class_id = $class_details['data']['classSection']['id']; $class_id = $class_details['data']['classSection']['id'];
if(!empty($class_details['data']['classSection']['video'])){ if(!empty($class_details['data']['classSection']['video'])){
$this->download($class_details['data']['classSection']['video'],$class_path); $file_name = urldecode(basename($class_details['data']['classSection']['video']));
$downloadurl = $class_path . '/' . $file_name . '-下载地址.txt';
$this->savetxt($downloadurl,$class_details['data']['classSection']['video']);
$this->download($class_details['data']['classSection']['video'],$class_path,$file_name);
} }
if(!empty($class_details['data']['classSection']['courseware'])){ if(!empty($class_details['data']['classSection']['courseware'])){
$this->download($class_details['data']['classSection']['courseware'],$class_path); $file_name = urldecode(basename($class_details['data']['classSection']['courseware']));
$downloadurl = $class_path . '/' . $file_name . '-下载地址.txt';
$this->savetxt($downloadurl,$class_details['data']['classSection']['courseware']);
$this->download($class_details['data']['classSection']['courseware'],$class_path,$file_name);
} }
if(!empty($class_details['data']['classSection']['construction_draw'])){ if(!empty($class_details['data']['classSection']['construction_draw'])){
$this->download($class_details['data']['classSection']['construction_draw'],$class_path); $file_name = urldecode(basename($class_details['data']['classSection']['construction_draw']));
$downloadurl = $class_path . '/' . $file_name . '-下载地址.txt';
$this->savetxt($downloadurl,$class_details['data']['classSection']['construction_draw']);
$this->download($class_details['data']['classSection']['construction_draw'],$class_path,$file_name);
} }
if(!empty($class_details['data']['classSection']['construction_draw_3d'])){ if(!empty($class_details['data']['classSection']['construction_draw_3d'])){
$this->download($class_details['data']['classSection']['construction_draw_3d'],$class_path); $file_name = urldecode(basename($class_details['data']['classSection']['construction_draw_3d']));
$downloadurl = $class_path . '/' . $file_name . '-下载地址.txt';
$this->savetxt($downloadurl,urldecode($class_details['data']['classSection']['construction_draw_3d']));
$this->download($class_details['data']['classSection']['construction_draw_3d'],$class_path,$file_name);
} }
foreach($class_details['data']['classSection']['allEnclosure'] as $enclosure){ foreach($class_details['data']['classSection']['allEnclosure'] as $enclosure){
@@ -109,7 +125,7 @@ class Zm extends Command
is_dir($storage) || mkdir($storage,0755,true); is_dir($storage) || mkdir($storage,0755,true);
$downloadurl = $storage . '/' . urldecode(end($parts)) . '-下载地址.txt'; $downloadurl = $storage . '/' . urldecode(end($parts)) . '-下载地址.txt';
$this->savetxt($downloadurl,urldecode($enclosure['url'])); $this->savetxt($downloadurl,urldecode($enclosure['url']));
// $this->download($enclosure['url'],$storage); $this->download($enclosure['url'],$storage,basename($enclosure['url']));
} }
} }
} }
@@ -131,6 +147,16 @@ class Zm extends Command
private function checkfile($url,$file) private function checkfile($url,$file)
{ {
dump($url,$file); dump($url,$file);
if(is_file($file)){
$size = filesize($file);
if($size > 0){
return true;
}else{
dump('文件大小为0重新下载'.$file);
unlink($file);
return 2;
}
}
$key = 'zm_check_header_' . md5($url); $key = 'zm_check_header_' . md5($url);
try { try {
if(($head = Cache::get($key,null)) === NULL ){ if(($head = Cache::get($key,null)) === NULL ){
@@ -157,7 +183,6 @@ class Zm extends Command
foreach ($headers as $name => $values) { foreach ($headers as $name => $values) {
$head[$name] = implode(', ', $values); $head[$name] = implode(', ', $values);
} }
dump($head);
} catch (\GuzzleHttp\Exception\RequestException $e) { } catch (\GuzzleHttp\Exception\RequestException $e) {
dump("文件head请求失败: " . $e->getMessage()); dump("文件head请求失败: " . $e->getMessage());
return 0; return 0;
@@ -191,13 +216,20 @@ class Zm extends Command
} }
return 3; return 3;
} }
protected function download($downloadurl,$dir,$filename = null) protected function download($downloadurl,$dir,$filename)
{ {
return;
$result = $this->checkfile($downloadurl,$dir . '/' . $filename);
if($result === true){
dump('文件已存在,跳过下载:' . $filename);
return;
}
$num = 1; $num = 1;
while($num > 0){ while($num > 0){
$activeDownloads = $this->aria2->getGlobalStat(); $activeDownloads = $this->aria2->getGlobalStat();
$num = intval($activeDownloads['result']['numWaiting']); $num = intval($activeDownloads['result']['numWaiting']);
if($num > 10){ if($num > 50){
dump('当前任务过多,等待任务数:',$num); dump('当前任务过多,等待任务数:',$num);
sleep(1); sleep(1);
}else{ }else{
@@ -314,6 +346,8 @@ class Zm extends Command
return $this->getClassDetails($curr_id,$result['sign']); return $this->getClassDetails($curr_id,$result['sign']);
}else if($result['code'] == 0){ }else if($result['code'] == 0){
return $result; return $result;
}else if($result['code'] == -5){
return false;
}else{ }else{
dump($result['msg']); dump($result['msg']);
exit(); exit();