implemented album for uploads

This commit is contained in:
Niklas Kapelle 2025-10-29 15:54:02 +01:00
parent d8a97c1257
commit 6e46f52b52
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
3 changed files with 57 additions and 6 deletions

View File

@ -12,6 +12,7 @@ type args struct {
APIKey string `arg:"--api-key,required,env:API_KEY" placeholder:"API_KEY"`
UserdataDir string `arg:"--steam-userdata-dir,env:USERDATA_DIR" default:"$HOME/.var/app/com.valvesoftware.Steam/.local/share/Steam/userdata"`
DeviceID string `arg:"--device-id,env:DEVICE_ID" default:"steam-immich"`
Album string `arg:"--album,env:ALBUM"`
}
func main() {
@ -23,5 +24,6 @@ func main() {
BaseURL: args.BaseURL,
UserdataDir: os.ExpandEnv(args.UserdataDir),
DeiveID: args.DeviceID,
Album: args.Album,
})
}

View File

@ -12,17 +12,23 @@ import (
"time"
)
type ImmichAPIUploadResponse struct {
type uploadResponse struct {
ID string `json:"id"`
Status string `json:"status"`
}
type ImmichAPIAssetPutRequest struct {
type assetPutRequest struct {
Ids []string `json:"ids"`
Description string `json:"description,omitempty"`
}
func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*ImmichAPIUploadResponse, error) {
type bulkIdResponseDto struct {
Error string `json:"error"`
Id string `json:"id"`
Success bool `json:"success"`
}
func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadResponse, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
@ -80,7 +86,7 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*ImmichA
return nil, fmt.Errorf("Immich upload failed (%d): %s", resp.StatusCode, string(respBody))
}
var result ImmichAPIUploadResponse
var result uploadResponse
if err := json.Unmarshal(respBody, &result); err != nil {
return nil, fmt.Errorf("Failed to parse immich response: %s", err)
}
@ -89,7 +95,7 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*ImmichA
}
func setAssetMetadata(assets []string, description, baseURL, apiKey string) error {
body, _ := json.Marshal(ImmichAPIAssetPutRequest{
body, _ := json.Marshal(assetPutRequest{
Ids: assets,
Description: description,
})
@ -111,3 +117,36 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro
return nil
}
func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error {
body, _ := json.Marshal(struct {
IDs []string `json:"ids"`
}{IDs: assets})
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/api/albums/%s/assets", baseURL, album), bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Set("x-api-key", apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
respBody, _ := io.ReadAll(res.Body)
if res.StatusCode >= 300 {
return fmt.Errorf("Failed to add assets to album (%d): %s", res.StatusCode, string(respBody))
}
var result []bulkIdResponseDto
if err := json.Unmarshal(respBody, &result); err != nil {
return fmt.Errorf("Failed to parse immich response: %s", err)
}
return nil
}

View File

@ -48,7 +48,17 @@ func Run(config Config) {
assetsInGame = append(assetsInGame, res.ID)
}
setAssetMetadata(assetsInGame, fmt.Sprintf("Game: %s", gameName), config.BaseURL, config.APIKey)
err := setAssetMetadata(assetsInGame, fmt.Sprintf("Game: %s", gameName), config.BaseURL, config.APIKey)
if err != nil {
fmt.Printf("Stopped: %s", err)
}
if config.Album != "" {
err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, config.APIKey)
if err != nil {
fmt.Printf("Stopped: %s", err)
}
}
fmt.Println()
}