实现请求缓存1天。

This commit is contained in:
2025-04-18 15:38:58 +08:00
parent 2d3f7d8511
commit 96da83a1ab
209 changed files with 19400 additions and 1657 deletions

View File

@@ -11,6 +11,16 @@ use think\facade\Cache;
use think\facade\Log;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcher;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage;
use Symfony\Component\Cache\Adapter\RedisAdapter;
class Zm extends Command
{
private $aria2 = null;
@@ -35,10 +45,13 @@ class Zm extends Command
$this->aria2->setOption('enable-http-pipelining', true); //设置http管道化
$this->aria2->setOption('enable-http-keep-alive', true); //设置http保持连接
// Disable SSL verification for cURL requests
$cachePath = runtime_path() . 'guzzle_cache';
$this->client = new Client([
'verify' => false,
]);
$this->completed = false;
while($this->completed === false){
try {
@@ -69,14 +82,19 @@ class Zm extends Command
$curr_total++;
$curr_item = $value;
$curr_item_title = $curr_item['title'];
$c = $savepath . '/' . $lable_title . '/' . $curr_item_title;
$c = trim($savepath . '/' . $lable_title . '/' . $curr_item_title);
// dump('存储路径',$c);
is_dir($c) || mkdir($c,0755,true);
try {
is_dir($c) || mkdir($c,0755,true);
} catch (\Exception $e) {
dump('创建目录失败:',$c,$e->getMessage());
throw $e;
}
$author = $savepath . '/' . $lable_title . '/' . $curr_item_title . '/' . '作者.txt';
$author = $c . '/' . '作者.txt';
$this->savetxt($author,$curr_item['author']);
$introduce = $savepath . '/' . $lable_title . '/' . $curr_item_title . '/' . '介绍.txt';
$introduce = $c . '/' . '介绍.txt';
$this->savetxt($introduce,$curr_item['introduce']);
$cover = $curr_item['cover'];
@@ -87,8 +105,13 @@ class Zm extends Command
$this->savetxt($cover_path,$cover);
$curr_details = $this->getCurrDetails($curr_item['id']);
foreach($curr_details['data']['curriculum']['allClassSectionTrue'] as $class){
$class_path = $c . '/' . $class['title'];
is_dir($class_path) || mkdir($class_path,0755,true);
$class_path = trim($c . '/' . $class['title']);
try {
is_dir($class_path) || mkdir($class_path,0755,true);
} catch (\Exception $e) {
dump('创建目录失败:',$class_path,$e->getMessage());
throw $e;
}
$class_details = $this->getClassDetails($class['id']);
if($class_details == false){
dump('课程未授权,跳过下载',$class['title']);
@@ -265,11 +288,16 @@ class Zm extends Command
if($result['code'] == -3){
return $this->getLable($result['sign']);
}else{
return $result;
}
}
public function getCurr($label_id,$page,$sign = 'null')
{
$cacheKey = 'zm_curr_' . $label_id . '_' . $page;
if(Cache::has($cacheKey)){
return Cache::get($cacheKey);
}
$options = [
'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',
@@ -289,6 +317,7 @@ class Zm extends Command
if($result['code'] == -3){
return $this->getCurr($label_id,$page,$result['sign']);
}else if($result['code'] == 0){
Cache::set($cacheKey,$result,86400);
return $result;
}else{
dump($result['msg']);
@@ -299,6 +328,10 @@ class Zm extends Command
public function getCurrDetails($curr_id,$sign = 'null')
{
$cacheKey = 'zm_curr_details_' . $curr_id;
if(Cache::has($cacheKey)){
return Cache::get($cacheKey);
}
$options = [
'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',
@@ -318,6 +351,7 @@ class Zm extends Command
if($result['code'] == -3){
return $this->getCurrDetails($curr_id,$result['sign']);
}else if($result['code'] == 0){
Cache::set($cacheKey,$result,86400);
return $result;
}else{
dump($result['msg']);
@@ -326,6 +360,10 @@ class Zm extends Command
}
public function getClassDetails($curr_id,$sign = 'null')
{
$cacheKey = 'zm_class_details_' . $curr_id;
if(Cache::has($cacheKey)){
return Cache::get($cacheKey);
}
$options = [
'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',
@@ -346,6 +384,7 @@ class Zm extends Command
if($result['code'] == -3){
return $this->getClassDetails($curr_id,$result['sign']);
}else if($result['code'] == 0){
Cache::set($cacheKey,$result,86400);
return $result;
}else if($result['code'] == -5){
return false;
@@ -355,8 +394,6 @@ class Zm extends Command
}
}
private function savetxt(string $file,string $content)
{
if(is_file($file)){