90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package steamimmich
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"slices"
|
|
)
|
|
|
|
type Config struct {
|
|
BaseURL string
|
|
APIKey string
|
|
UserdataDir string
|
|
DeiveID string
|
|
Album string
|
|
}
|
|
|
|
func Run(config Config) {
|
|
screenshotFiles, err := listScreenshots(config.UserdataDir)
|
|
|
|
if err != nil {
|
|
log.Fatalf("Failed to scan for screenshots: %s", err)
|
|
return
|
|
}
|
|
|
|
if len(screenshotFiles) == 0 {
|
|
log.Println("No screenshots found.")
|
|
return
|
|
}
|
|
|
|
immichClient := newImmichHttpClient(config.APIKey)
|
|
|
|
localState, err := loadLocalState("./state.json")
|
|
if err != nil {
|
|
log.Fatalf("Failed to load local state: %s", err)
|
|
}
|
|
|
|
gameNameCache := make(map[string]string)
|
|
|
|
for appid, files := range screenshotFiles {
|
|
gameName := getGameName(appid, gameNameCache)
|
|
assetsInGame := make([]string, 0)
|
|
|
|
for _, filepath := range files {
|
|
|
|
idx := slices.IndexFunc(localState.Assets, func(e AssetState) bool { return e.FilePath == filepath })
|
|
|
|
if idx != -1 {
|
|
log.Printf("Asset already uploaded: %s", filepath)
|
|
continue
|
|
}
|
|
|
|
res, err := uploadToImmich(filepath, appid, config.BaseURL, config.DeiveID, &immichClient)
|
|
|
|
if err != nil {
|
|
log.Fatalf("Failed to upload to immich: %s", err)
|
|
return
|
|
}
|
|
|
|
log.Printf("Assest uploaded: %s (%s) %s with ID: %s", gameName, appid, filepath, res.ID)
|
|
|
|
localState.Assets = append(localState.Assets, AssetState{FilePath: filepath, ImmichID: res.ID})
|
|
|
|
assetsInGame = append(assetsInGame, res.ID)
|
|
}
|
|
|
|
err := setAssetMetadata(assetsInGame, fmt.Sprintf("Game: %s", gameName), config.BaseURL, immichClient)
|
|
if err != nil {
|
|
log.Fatalf("Failed to set asset metadata: %s", err)
|
|
return
|
|
}
|
|
|
|
if config.Album != "" {
|
|
err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, immichClient)
|
|
if err != nil {
|
|
log.Fatalf("Failed to add assets to album: %s", err)
|
|
return
|
|
}
|
|
|
|
log.Printf("Added %d assets to album %s", len(assetsInGame), config.Album)
|
|
}
|
|
}
|
|
|
|
log.Println("Finished uploading screenshots")
|
|
err = saveLocalState("./state.json", *localState)
|
|
if err != nil {
|
|
log.Fatalf("Failed to save local cache: %s", err)
|
|
return
|
|
}
|
|
}
|