Added /album endpoint

This commit is contained in:
DataHoarder 2022-02-21 10:49:17 +01:00
parent 5d73ea2b35
commit 73e5f71789
2 changed files with 21 additions and 2 deletions

View file

@ -28,6 +28,8 @@ Small Golang server that loads all albums in an index at startup and is used to
* read cmd ex. `/cddb?cmd=cddb+read+Soundtrack54742+730dec08`
* Fetch parsed track Lyrics entries
* ex. `/lyrics/Clockup_Flowers`
* Fetch album entry by pageid
* ex. `/album/54742`
# License

View file

@ -658,7 +658,9 @@ func parseLyrics(filePath, pageName string) (lyrics *Lyrics) {
var contents []byte
if contents, err = ioutil.ReadFile(path.Join(filePath, "pages_by_name", "Lyrics:_"+pageName+".wiki")); err != nil {
if contents, err = ioutil.ReadFile(path.Join(filePath, "pages_by_name", "Lyrics:"+pageName+".wiki")); err != nil {
return
if contents, err = ioutil.ReadFile(path.Join(filePath, "pages", pageName+".wiki")); err != nil {
return
}
}
}
@ -1070,7 +1072,7 @@ func main() {
} else if strings.Index(request.URL.Path, "/lyrics/") == 0 {
writer.Header().Set("Content-Type", "application/json")
lyrics := parseLyrics(*servePath, wikitext_parser.NormalizeWikiTitle(strings.Join(strings.Split(request.URL.Path, "/")[2:], "/")))
lyrics := parseLyrics(*servePath, wikitext_parser.NormalizeWikiTitle(strings.TrimPrefix(request.URL.Path, "/lyrics/")))
if lyrics == nil {
writer.WriteHeader(http.StatusNotFound)
@ -1081,6 +1083,21 @@ func main() {
jsonBytes, _ := json.Marshal(lyrics)
writer.Write(jsonBytes)
} else if strings.Index(request.URL.Path, "/album/") == 0 {
writer.Header().Set("Content-Type", "application/json")
if pageid, err := strconv.Atoi(strings.TrimPrefix(request.URL.Path, "/album/")); err == nil {
if album, ok := cdIndex[pageid]; ok {
jsonBytes, _ := json.Marshal(album)
writer.Write(jsonBytes)
return
}
}
writer.WriteHeader(http.StatusNotFound)
writer.Write([]byte("{}"))
return
} else {
filePath := path.Join(*servePath, strings.TrimLeft(request.URL.Path, "/"))
if request.URL.Path == "/" {