Skip to content

Commit 987dd00

Browse files
committed
feat: support year directory structure and auto-create missing folders
1 parent 116392c commit 987dd00

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

scraper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
// 日付付きの JSON ファイルとして保存(例: docs/v2/20250826.json)
3535
// 最新データとして today.json にも保存
3636
$storage = new ProgramStorage();
37-
$storage->save($programs, "docs/{$version}/" . $date->format('Ymd') . '.json');
37+
$storage->save($programs, "docs/{$version}/" . $date->format('Y') . '/' . $date->format('Ymd') . '.json');
3838
$storage->save($programs, "docs/{$version}/today.json");

src/ProgramStorage.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ final class ProgramStorage
5656
public function save(array $programs, string $path): void
5757
{
5858
$contents = json_encode(['programs' => $programs]);
59-
if ($contents !== false && file_put_contents($path, $contents) === false) {
59+
if ($contents === false) {
60+
throw new \RuntimeException("Failed to encode programs to JSON");
61+
}
62+
63+
$dir = dirname($path);
64+
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
65+
throw new \RuntimeException("Failed to create directory: {$dir}");
66+
}
67+
68+
if (file_put_contents($path, $contents) === false) {
6069
throw new \RuntimeException("Failed to save programs to {$path}");
6170
}
6271
}

0 commit comments

Comments
 (0)