nieuw formaat JSON voor NOS

This commit is contained in:
Peter Kleiweg
2026-05-05 14:32:47 +02:00
parent 214e1ae13c
commit 395997d799

View File

@@ -11,7 +11,12 @@ import (
"time"
)
type Graph struct {
Items []Item `json:"@graph"`
}
type Item struct {
Type any `json:"@type"`
Title string `json:"name"`
Text string `json:"articleBody"`
Cats []string `json:"articleSection"`
@@ -54,8 +59,7 @@ func main() {
x(err)
fp, err := os.Create("out/" + filename[:len(filename)-5] + ".txt")
x(err)
var item Item
x(json.Unmarshal(b, &item))
item := getItem(b, filename)
for _, cat := range item.Cats {
x(fmt.Fprintf(fp, "##META text cat = %s\n", fixSpace(cat)))
}
@@ -68,6 +72,25 @@ func main() {
}
}
func getItem(b []byte, filename string) Item {
var graph Graph
if json.Unmarshal(b, &graph) == nil {
if graph.Items != nil {
for _, item := range graph.Items {
switch v := item.Type.(type) {
case string:
if v == "NewsArticle" {
return item
}
}
}
}
}
var item Item
x(json.Unmarshal(b, &item), filename)
return item
}
func addEnd(s string) string {
s = strings.TrimSpace(s)
n := len(s)