This commit is contained in:
make
2025-12-18 01:29:20 +08:00
parent 19314f6077
commit ae57b68f99

View File

@@ -483,46 +483,34 @@ EOF;
*/
public function generateVideoJsonByPage(int $intPage): void
{
// ===== 1. 显式输出,防止静默失败 =====
echo "[JSON] start page={$intPage}\n";
$intStart = ($intPage - 1) * $this->intPageSize;
foreach ($this->getDomain() as $DomainModel) {
// ===== 2. 安全路径处理 =====
$strDomainDir = rtrim($this->strSiteMapPath, '/') . '/' . $DomainModel->d_domain;
if (!is_dir($strDomainDir)) {
if (!mkdir($strDomainDir, 0755, true) && !is_dir($strDomainDir)) {
file_put_contents(
'/tmp/video_json_error.log',
"[mkdir fail] {$strDomainDir}\n",
FILE_APPEND
);
continue;
}
mkdir($strDomainDir, 0755, true);
}
$strJsonFile = $strDomainDir . "/video-list-{$intPage}.json";
$finalFile = $strDomainDir . "/video-list-{$intPage}.json";
$tmpFile = $finalFile . '.tmp';
$fp = @fopen($strJsonFile, 'w');
$fp = fopen($tmpFile, 'w');
if ($fp === false) {
file_put_contents(
'/tmp/video_json_error.log',
"[fopen fail] {$strJsonFile}\n",
FILE_APPEND
);
file_put_contents('/tmp/video_json_error.log', "[open fail] {$tmpFile}\n", FILE_APPEND);
continue;
}
// ===== 3. JSON 数组开始 =====
// ===== 一定先写 '[' =====
fwrite($fp, "[\n");
$isFirst = true;
foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) {
// ===== 4. 生成 URL =====
$strUri = $DomainModel->getFomartUrlEx(
"VIDEO_INFO_URL",
$DomainModel->d_domain,
@@ -534,33 +522,33 @@ EOF;
]
);
$strFullUrl = 'https://www.' . $DomainModel->d_domain . $strUri;
$fullUrl = 'https://www.' . $DomainModel->d_domain . $strUri;
// ===== 5. 构建数据(强制字符串) =====
$row = [
'keyword' => (string)$Video->v_name,
'site' => (string)$DomainModel->d_domain,
'href' => (string)$strFullUrl,
'href' => (string)$fullUrl,
'url' => '#',
];
// ===== 6. UTF-8 强制修复(关键) =====
foreach ($row as $k => $v) {
foreach ($row as &$v) {
if (is_string($v)) {
$row[$k] = mb_convert_encoding($v, 'UTF-8', 'UTF-8');
$v = mb_convert_encoding($v, 'UTF-8', 'UTF-8');
}
}
unset($v);
// ===== 7. JSON 编码(容错) =====
$json = json_encode(
$row,
JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_SLASHES // ⭐ 关键
| JSON_INVALID_UTF8_SUBSTITUTE
);
if ($json === false) {
file_put_contents(
'/tmp/video_json_error.log',
"[json_encode fail] " . json_last_error_msg() . "\n",
"[json fail] " . json_last_error_msg() . "\n",
FILE_APPEND
);
continue;
@@ -574,11 +562,14 @@ EOF;
$isFirst = false;
}
// ===== 8. JSON 数组结束 =====
// ===== 一定写 ']' =====
fwrite($fp, "\n]");
fclose($fp);
echo "[JSON] done {$strJsonFile}\n";
// ===== 原子替换,防止半文件 =====
rename($tmpFile, $finalFile);
echo "[JSON] done {$finalFile}\n";
}
}
}