diff --git a/README.md b/README.md index 36fa31dc4..464011f91 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/server.go b/server.go index a28794c81..f90b35a66 100644 --- a/server.go +++ b/server.go @@ -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 == "/" {