Some checks failed
Code Analysis and Production Deploy / analyze (push) Has been skipped
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m2s
Code Analysis and Production Deploy / analyze (pull_request) Failing after 33s
Code Analysis and Production Deploy / deploy-production (pull_request) Has been skipped
Code Analysis and Production Deploy / deploy-test (pull_request) Has been skipped
Require Package Version Change / check (pull_request) Failing after 10s
- Created `import-spielplan.js` to fetch and parse the match schedule from the specified URL, saving the output as JSON. - Added `run-spielplan-import.sh` to automate the execution of the import script and log output. - Introduced `spielplan.html` file to store the downloaded HTML content for further processing.
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { promises as fs } from 'node:fs'
|
|
import path from 'node:path'
|
|
import {
|
|
convertImportedSpielplanToJson,
|
|
validateImportedSpielplan
|
|
} from '../server/utils/spielplan-data.js'
|
|
|
|
const DEFAULT_INPUT = 'temp/webpage-downloads/data/harheimer_tc_spielplan.json'
|
|
const PUBLIC_SEASONS_DIR = 'public/data/spielplaene'
|
|
const INTERNAL_SEASONS_DIR = 'server/data/public-data/spielplaene'
|
|
|
|
function getArgValue(name) {
|
|
const index = process.argv.indexOf(name)
|
|
return index >= 0 ? process.argv[index + 1] : null
|
|
}
|
|
|
|
async function writeJsonAtomic(filePath, data) {
|
|
await fs.mkdir(path.dirname(filePath), { recursive: true })
|
|
const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now()}`
|
|
const content = `${JSON.stringify(data, null, 2)}\n`
|
|
|
|
await fs.writeFile(tmpPath, content, 'utf8')
|
|
await fs.rename(tmpPath, filePath)
|
|
}
|
|
|
|
const inputPath = getArgValue('--input') || DEFAULT_INPUT
|
|
const publicSeasonsDir = getArgValue('--output-dir') || PUBLIC_SEASONS_DIR
|
|
const raw = await fs.readFile(inputPath, 'utf8')
|
|
const imported = JSON.parse(raw)
|
|
|
|
validateImportedSpielplan(imported)
|
|
|
|
const spielplan = convertImportedSpielplanToJson(imported)
|
|
const seasonSlug = imported.source?.season?.seasonSlug
|
|
if (!seasonSlug) {
|
|
throw new Error('Saison-Slug fehlt in der Import-Datei')
|
|
}
|
|
|
|
const publicSeasonPath = path.join(publicSeasonsDir, `spielplan-${seasonSlug}.json`)
|
|
const internalSeasonPath = path.join(INTERNAL_SEASONS_DIR, `spielplan-${seasonSlug}.json`)
|
|
|
|
await writeJsonAtomic(publicSeasonPath, spielplan)
|
|
await writeJsonAtomic(internalSeasonPath, spielplan)
|
|
|
|
console.log(`Validiert: ${imported.matchCount} Spiele, Saison ${seasonSlug}`)
|
|
console.log(`Saison-Datei: ${publicSeasonPath}`)
|
|
console.log(`Interne Saison-Datei: ${internalSeasonPath}`)
|