Implement fetching photos from photoprism #19

Merged
balki merged 7 commits from photoprism-fetch into main 2024-08-08 12:19:45 -04:00
Showing only changes of commit fa1c8e6963 - Show all commits

71
main.go
View File

@ -10,6 +10,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"regexp" "regexp"
@ -29,6 +30,8 @@ var (
collageNameGen *nameGen collageNameGen *nameGen
imagesDirFs fs.FS imagesDirFs fs.FS
listenAddr string listenAddr string
photoPrismURL string
photoPrismToken string
//go:embed web //go:embed web
webFS embed.FS webFS embed.FS
@ -39,6 +42,8 @@ func main() {
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development") 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(&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() flag.Parse()
@ -100,16 +105,33 @@ func main() {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
collageFile, err := MakeCollage(&collageReq) collageFilePath, err := MakeCollage(&collageReq)
if err != nil { if err != nil {
slog.Error("failed to make collage", "error", err) slog.Error("failed to make collage", "error", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return 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) 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)) addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
if err != nil { if err != nil {
slog.Error("anyhttp Serve failed", "error", err) slog.Error("anyhttp Serve failed", "error", err)
@ -141,3 +163,48 @@ func MakeCollage(req *collage.Request) (string, error) {
} }
return collageFile, nil 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
}