feat: initial commit
This commit is contained in:
77
misc/quiz/openquizzdb/embed.go
Normal file
77
misc/quiz/openquizzdb/embed.go
Normal file
@ -0,0 +1,77 @@
|
||||
package openquizzdb
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
//go:embed *.json
|
||||
var quizzFS embed.FS
|
||||
|
||||
func LoadAll() ([]*OpenQuiz, error) {
|
||||
quizz := make([]*OpenQuiz, 0)
|
||||
err := fs.WalkDir(quizzFS, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if d.IsDir() || !strings.HasSuffix(path, ".json") {
|
||||
return nil
|
||||
}
|
||||
|
||||
var q OpenQuiz
|
||||
|
||||
data, err := fs.ReadFile(quizzFS, path)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "could not read quiz '%s'", path)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &q); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
quizz = append(quizz, &q)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return quizz, nil
|
||||
}
|
||||
|
||||
type OpenQuiz struct {
|
||||
Provider string `json:"fournisseur"`
|
||||
License string `json:"licence"`
|
||||
Writer string `json:"rédacteur"`
|
||||
Difficulty string `json:"difficulté"`
|
||||
Version int `json:"version"`
|
||||
UpdatedAt string `json:"mise-à-jour"`
|
||||
Categories Categories `json:"catégorie-nom-slogan"`
|
||||
Quizz Quizz `json:"quizz"`
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
Name string `json:"catégorie"`
|
||||
Label string `json:"nom"`
|
||||
Slogan string `json:"slogan"`
|
||||
}
|
||||
|
||||
type Categories map[string]Category
|
||||
|
||||
type Entry struct {
|
||||
ID int `json:"id"`
|
||||
Question string `json:"question"`
|
||||
Propositions []string `json:"propositions"`
|
||||
Answer string `json:"réponse"`
|
||||
Anecdote string `json:"anecdote"`
|
||||
}
|
||||
|
||||
type Quiz struct {
|
||||
Beginner []Entry `json:"débutant"`
|
||||
Intermediate []Entry `json:"confirmé"`
|
||||
Expert []Entry `json:"expert"`
|
||||
}
|
||||
|
||||
type Quizz map[string]Quiz
|
Reference in New Issue
Block a user