Created
August 27, 2026 06:05
-
-
Save dtjm/12efc8f287165fc1deec266a6297366a to your computer and use it in GitHub Desktop.
Example Immich script in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "regexp" | |
| "strconv" | |
| "time" | |
| "github.com/samber/lo" | |
| immich "harrisonhjones.com/immich-go-sdk/sdk" | |
| ) | |
| func main() { | |
| // 1. Configuration: Fetch credentials from environment variables | |
| // Set these in your terminal: | |
| // export IMMICH_URL=https://your-immich-instance.com | |
| // export IMMICH_API_KEY=your-api-key-here | |
| serverURL := "http://127.0.0.1:2283" | |
| apiKey := "XXX" | |
| if serverURL == "" || apiKey == "" { | |
| log.Fatal("Please set IMMICH_URL and IMMICH_API_KEY environment variables.") | |
| } | |
| cfg := immich.NewConfiguration() | |
| // The SDK appends "/api" to the server URL by default; override with the | |
| // full base path so we can point at any deployment layout. | |
| cfg.Servers = immich.ServerConfigurations{ | |
| {URL: serverURL + "/api"}, | |
| } | |
| client := immich.NewAPIClient(cfg) | |
| // Build an authenticated context when an API key is provided. | |
| ctx := context.WithValue(context.Background(), immich.ContextAPIKeys, map[string]immich.APIKey{ | |
| "api_key": {Key: apiKey}, | |
| }) | |
| res, _, err := client.SearchAPI.SearchAssets(ctx).MetadataSearchDto(immich.MetadataSearchDto{ | |
| OriginalFileName: new("_"), | |
| }).Execute() | |
| if err != nil { | |
| log.Fatalf("Search failed: %w", err) | |
| } | |
| // 2009_11_21_20_05_36 | |
| rx := regexp.MustCompile(`^(\d\d\d\d)_(\d\d)_(\d\d)_(\d\d)_(\d\d)_(\d\d)`) | |
| for _, asset := range res.Assets.Items { | |
| matches := rx.FindAllStringSubmatch(asset.OriginalFileName, -1) | |
| fmt.Printf("%s : %+v\n", asset.OriginalFileName, matches) | |
| if len(matches) == 0 { | |
| continue | |
| } | |
| nn := lo.Map(matches[0][1:], func(s string, _ int) int { | |
| n, _ := strconv.Atoi(s) | |
| return n | |
| }) | |
| d := time.Date(nn[0], time.Month(nn[1]), nn[2], nn[3], nn[4], nn[5], 0, time.Local) | |
| diff := asset.FileCreatedAt.Sub(d) | |
| fmt.Printf("diff: %s\n", diff.String()) | |
| if diff.Hours() > 24 { | |
| fmt.Print("updating...\n\n") | |
| _, _, err := client.AssetsAPI.UpdateAsset(ctx, asset.Id). | |
| UpdateAssetDto(immich.UpdateAssetDto{ | |
| DateTimeOriginal: new(d.Format(time.RFC3339)), | |
| }).Execute() | |
| if err != nil { | |
| log.Fatal(err.Error()) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment