Implement fetching photos from photoprism #19
71
main.go
71
main.go
@ -10,6 +10,7 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -29,6 +30,8 @@ var (
|
||||
collageNameGen *nameGen
|
||||
imagesDirFs fs.FS
|
||||
listenAddr string
|
||||
photoPrismURL string
|
||||
photoPrismToken string
|
||||
|
||||
//go:embed web
|
||||
webFS embed.FS
|
||||
@ -39,6 +42,8 @@ func main() {
|
||||
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
|
||||
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
|
||||
flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
|
||||
flag.StringVar(&photoPrismURL, "pp-url", "", "Base url for photoprism")
|
||||
flag.StringVar(&photoPrismToken, "pp-token", "", "API token for photoprism")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
@ -100,16 +105,33 @@ func main() {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
collageFile, err := MakeCollage(&collageReq)
|
||||
collageFilePath, err := MakeCollage(&collageReq)
|
||||
if err != nil {
|
||||
slog.Error("failed to make collage", "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if _, err := w.Write([]byte(collageFile)); err != nil {
|
||||
if _, err := w.Write([]byte(collageFilePath)); err != nil {
|
||||
slog.Error("Failed to write collageFile", "error", err)
|
||||
}
|
||||
})
|
||||
|
||||
http.HandleFunc("/get-albums", func(w http.ResponseWriter, r *http.Request) {
|
||||
albums, err := GetAlbums()
|
||||
if err != nil {
|
||||
slog.Error("failed to get albums", "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
jData, err := json.Marshal(albums)
|
||||
if err != nil {
|
||||
slog.Error("failed to marshal albums", "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jData)
|
||||
})
|
||||
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
|
||||
if err != nil {
|
||||
slog.Error("anyhttp Serve failed", "error", err)
|
||||
@ -141,3 +163,48 @@ func MakeCollage(req *collage.Request) (string, error) {
|
||||
}
|
||||
return collageFile, nil
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
Title string `json:"Title"`
|
||||
UID string `json:"UID"`
|
||||
}
|
||||
|
||||
func GetAlbums() ([]Album, error) {
|
||||
u, err := url.Parse(photoPrismURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u.Path = "/api/v1/albums"
|
||||
v := url.Values{}
|
||||
v.Add("count", "10")
|
||||
v.Add("type", "album")
|
||||
u.RawQuery = v.Encode()
|
||||
|
||||
req, err := http.NewRequest("GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", photoPrismToken))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
albums := []Album{}
|
||||
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(respBytes, &albums)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// List albums
|
||||
// yq -oc '[.[] | pick(["UID", "Title"])]' albums.json | pandoc -fcsv -tplain
|
||||
return albums, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user