From 90f6db5515b38c00b29fe4f268c6ac1098f500fd Mon Sep 17 00:00:00 2001 From: make Date: Wed, 17 Dec 2025 23:44:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=A6=E6=83=85json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/app/task/logic/VideoSiteMapLogic.php | 181 ++++++++++++++++++---- 1 file changed, 152 insertions(+), 29 deletions(-) diff --git a/code/app/task/logic/VideoSiteMapLogic.php b/code/app/task/logic/VideoSiteMapLogic.php index 9914444..a4e8d0a 100644 --- a/code/app/task/logic/VideoSiteMapLogic.php +++ b/code/app/task/logic/VideoSiteMapLogic.php @@ -92,33 +92,66 @@ class VideoSiteMapLogic return $this->arrDomainModel; } + /** - * Generate Site Map + * Generate Site Map & Video JSON * * @return void */ public function generateSiteMap() { - $arrResult = $this->VideoModel->findPaginatedWithCache([], 1, 1); + // 1. 只查一次总数(非常重要) + $arrResult = $this->VideoModel->findPaginatedWithCache([], 1, 1); $this->intTotal = $arrResult['total']; + // 2. sitemap index $this->generateMapIndex(); + // 3. sitemap main $this->generateMapMain(); + // 4. 统一分页数 $intTotalPage = ceil($this->intTotal / $this->intPageSize); + + // 5. 分页生成 sitemap + json(推荐放一起) for ($intPage = 1; $intPage <= $intTotalPage; $intPage++) { + + // 原有 sitemap $this->generateMapInfo($intPage); + + // 新增 JSON + $this->generateVideoJsonByPage($intPage); } echo date('Y-m-d H:i:s') . PHP_EOL; } + /** + * Generate Site Map + * + * @return void + */ + // public function generateSiteMap() + // { + // $arrResult = $this->VideoModel->findPaginatedWithCache([], 1, 1); + // $this->intTotal = $arrResult['total']; + + // $this->generateMapIndex(); + + // $this->generateMapMain(); + + // $intTotalPage = ceil($this->intTotal / $this->intPageSize); + // for ($intPage = 1; $intPage <= $intTotalPage; $intPage++) { + // $this->generateMapInfo($intPage); + // } + + // echo date('Y-m-d H:i:s') . PHP_EOL; + // } protected function generateMapInfo(int $intPage) { - $intStartPage = $intPage ; - $intStart = ($intStartPage-=1) * $this->intPageSize; + $intStartPage = $intPage; + $intStart = ($intStartPage -= 1) * $this->intPageSize; foreach ($this->getDomain() as $DomainModel) { $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; @@ -147,9 +180,9 @@ EOF; file_put_contents($strMapVideosFile, $strHead); - + # 取 intpage * limit ~ intpage * (limit+1) 写入以上文件 - + foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) { # 1 '/voddetail/{strPinYin}-{intVId}', @@ -168,17 +201,15 @@ EOF; $strContent = sprintf($strTemplate, $strUrl, $strPriority); file_put_contents($strMapVideosFile, $strContent, FILE_APPEND); file_put_contents($strMapVideosFileTxt, $strUrl . PHP_EOL, FILE_APPEND); - } file_put_contents($strMapVideosFile, $strEnd, FILE_APPEND); - } } /** - * 分批查询小说数据,并以生成器方式返回 + * 分批查询数据,并以生成器方式返回 * * @param int $intStart 跳过的记录数(skip) * @param int $intCount 需要查询的总记录数 @@ -194,9 +225,7 @@ EOF; while ($intRecordsFetched < $intRemainingRecords) { $intLimit = min($this->intBatchSize, $intRemainingRecords - $intRecordsFetched); $Cursor = $this->VideoModel->getCol()->find( - [ - - ], + [], [ 'skip' => $intStart + $intRecordsFetched, 'limit' => $intLimit, @@ -296,23 +325,23 @@ EOF; // foreach ($this->arrVideoArea as $strVideoAreaKey => $strVideoAreaVal) { // foreach ($this->arrVideoYear as $strVideoYearKey => $strVideoYearVal) { - $strKey = "VIDEO_CATEGORY_URL"; - $strDomain = $DomainModel->d_domain; - $strController = ""; - $strAction = ""; - $arrArgs = [ - 'strParentCategory' => $arrParentCategory['v_category_en'], - 'strCategory' => $arrChildrenCategory['v_category_en'], - 'strYear' => 'all', - 'strArea' => 'all', - 'strOrder' => $strSortKey, - 'strLang' => 'all', - 'intPage' => 1, - ]; + $strKey = "VIDEO_CATEGORY_URL"; + $strDomain = $DomainModel->d_domain; + $strController = ""; + $strAction = ""; + $arrArgs = [ + 'strParentCategory' => $arrParentCategory['v_category_en'], + 'strCategory' => $arrChildrenCategory['v_category_en'], + 'strYear' => 'all', + 'strArea' => 'all', + 'strOrder' => $strSortKey, + 'strLang' => 'all', + 'intPage' => 1, + ]; - $strUri = $DomainModel->getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); - $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); - $strContent = <<getFomartUrlEx($strKey, $strDomain, $strController, $strAction, $arrArgs); + $strUrl = sprintf("https://www.%s%s", $DomainModel->d_domain, $strUri); + $strContent = << {$strUrl} {$this->strDate} @@ -320,7 +349,7 @@ EOF; 0.8 EOF; - file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); + file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); // } // } // } @@ -412,4 +441,98 @@ EOF; file_put_contents($strMapIndexFile, $strContent, FILE_APPEND); } } + + public function generateVideoJsonAll(): void + { + $arrResult = $this->VideoModel->findPaginatedWithCache([], 1, 1); + $this->intTotal = $arrResult['total']; + + $intTotalPage = ceil($this->intTotal / $this->intPageSize); + + for ($intPage = 1; $intPage <= $intTotalPage; $intPage++) { + $this->generateVideoJsonByPage($intPage); + } + } + + + /** + * 分页生成 Video JSON(与 sitemap-videos-* 完全一致) + * + * 每个 JSON 文件格式: + * [ + * { + * "keyword": "...", + * "site": "...", + * "href": "...", + * "url": "#" + * } + * ] + */ + public function generateVideoJsonByPage(int $intPage): void + { + $intStart = ($intPage - 1) * $this->intPageSize; + + foreach ($this->getDomain() as $DomainModel) { + + $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; + if (!is_dir($strDomainDir)) { + mkdir($strDomainDir, 0755, true); + } + + $strJsonFile = sprintf( + '%s/video-list-%d.json', + $strDomainDir, + $intPage + ); + + $fp = fopen($strJsonFile, 'w'); + + // JSON 数组开始 + fwrite($fp, "[\n"); + + $isFirst = true; + + foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) { + + $strUri = $DomainModel->getFomartUrlEx( + "VIDEO_INFO_URL", + $DomainModel->d_domain, + '', + '', + [ + 'intVId' => $Video->v_id, + 'strPinYin' => $Video->v_name_en, + ] + ); + + $strFullUrl = sprintf( + 'https://www.%s%s', + $DomainModel->d_domain, + $strUri + ); + + $row = [ + 'keyword' => $Video->v_name, + 'site' => $DomainModel->d_domain, + 'href' => $strFullUrl, + 'url' => '#', + ]; + + if (!$isFirst) { + fwrite($fp, ",\n"); + } + + fwrite( + $fp, + json_encode($row, JSON_UNESCAPED_UNICODE) + ); + + $isFirst = false; + } + + // JSON 数组结束 + fwrite($fp, "\n]"); + fclose($fp); + } + } }