Moved the parameters to env variable + Added healthchecks

This commit is contained in:
2024-06-16 18:36:16 +02:00
parent 28bc2b92bc
commit 91d572fb1a
2 changed files with 110 additions and 103 deletions

View File

@@ -4,4 +4,4 @@ apk add --no-cache git gcc musl-dev nano
cd src/napnap75/immich-souvenirs/ cd src/napnap75/immich-souvenirs/
go mod init github.com/napnap75/multiarch-docker-files/immich-souvenirs go mod init github.com/napnap75/multiarch-docker-files/immich-souvenirs
go get -d -v go get -d -v
go run immich-souvenirs.go --immich-url="https://immich.nappez.com" --immich-key="wRJWYUGlEfWGQ3a5lckHivLCU40s7ldEZSmikpmsE30" --whatsapp-session-file=/config/ws.gob --whatsapp-group="120363288639885954@g.us" --run-once env IMMICH-URL="https://immich.nappez.com" env IMMICH-KEY="wRJWYUGlEfWGQ3a5lckHivLCU40s7ldEZSmikpmsE30" env WHATSAPP-SESSION-FILE="/config/ws.gob" env WHATSAPP-GROUP="120363288639885954@g.us" env RUN-ONCE=true go run immich-souvenirs.go

View File

@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@@ -21,28 +20,34 @@ import (
waLog "go.mau.fi/whatsmeow/util/log" waLog "go.mau.fi/whatsmeow/util/log"
) )
type parameters struct { type Parameters struct {
immichURL string ImmichURL string
immichKey string ImmichKey string
whatsappSessionFile string WhatsappSessionFile string
whatsappGroup string WhatsappGroup string
runOnce bool RunOnce bool
HealthchecksURL string
} }
func loadParameters() (parameters) { type Album struct {
param := new(parameters) ID string `json:"id"`
flag.StringVar(&param.immichURL, "immich-url", "", "The url of the Immich server to connect to") Name string `json:"albumName"`
flag.StringVar(&param.immichKey, "immich-key", "", "The API KEY to use with Immich") Shared bool `json:"shared"`
flag.StringVar(&param.whatsappSessionFile, "whatsapp-session-file", "", "The file to save the WhatsApp session to") HasSharedLink bool `json:"hasSharedLink"`
flag.StringVar(&param.whatsappGroup, "whatsapp-group", "", "The ID of the WhatsApp group to send the message to") StartDate time.Time `json:"startDate"`
flag.BoolVar(&param.runOnce, "run-once", false, "Run once and exits (default to false)") CreatedAt time.Time `json:"createdAt"`
flag.Parse() AlbumThumbnailAssetId string `json:"albumThumbnailAssetId"`
return *param
} }
func connect(param parameters) (*whatsmeow.Client, error) { type Key struct {
ID string `json:"id"`
Key string `json:"key"`
Album *Album `json:"album"`
}
func connect(param Parameters) (*whatsmeow.Client, error) {
dbLog := waLog.Stdout("Database", "ERROR", true) dbLog := waLog.Stdout("Database", "ERROR", true)
container, err := sqlstore.New("sqlite3", "file:" + param.whatsappSessionFile + "?_foreign_keys=on", dbLog) container, err := sqlstore.New("sqlite3", "file:" + param.WhatsappSessionFile + "?_foreign_keys=on", dbLog)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -102,7 +107,7 @@ func sendMessage(client *whatsmeow.Client, group string, message string, url str
return nil return nil
} }
func testConnexions(param parameters) error { func testConnexions(param Parameters) error {
// Create new WhatsApp connection and connect // Create new WhatsApp connection and connect
client, err := connect(param) client, err := connect(param)
if err != nil { if err != nil {
@@ -112,7 +117,7 @@ func testConnexions(param parameters) error {
defer client.Disconnect() defer client.Disconnect()
// Prints the available groups if none provided // Prints the available groups if none provided
if param.whatsappGroup == "" { if param.WhatsappGroup == "" {
fmt.Fprintf(os.Stdout, "No WhatsApp group provided, showing all available groups\n") fmt.Fprintf(os.Stdout, "No WhatsApp group provided, showing all available groups\n")
groups, err := client.GetJoinedGroups() groups, err := client.GetJoinedGroups()
if err != nil { if err != nil {
@@ -124,13 +129,13 @@ func testConnexions(param parameters) error {
return fmt.Errorf("No WhatsApp group provided") return fmt.Errorf("No WhatsApp group provided")
} else { } else {
jid, err := types.ParseJID(param.whatsappGroup) jid, err := types.ParseJID(param.WhatsappGroup)
if err != nil { if err != nil {
return fmt.Errorf("Incorrect group identifier '%s': %v", param.whatsappGroup, err) return fmt.Errorf("Incorrect group identifier '%s': %v", param.WhatsappGroup, err)
} }
_, err = client.GetGroupInfo(jid) _, err = client.GetGroupInfo(jid)
if err != nil { if err != nil {
return fmt.Errorf("Unknown WhatsApp group %s", param.whatsappGroup) return fmt.Errorf("Unknown WhatsApp group %s", param.WhatsappGroup)
} }
} }
@@ -138,147 +143,143 @@ func testConnexions(param parameters) error {
spaceClient := http.Client{ spaceClient := http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
} }
req, err := http.NewRequest(http.MethodGet, param.immichURL + "/api/albums", nil) req, err := http.NewRequest(http.MethodGet, param.ImmichURL + "/api/albums", nil)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
req.Header.Set("x-api-key", param.immichKey) req.Header.Set("x-api-key", param.ImmichKey)
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
res, err := spaceClient.Do(req) res, err := spaceClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
if res.Body != nil { if res.Body != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
return fmt.Errorf("Error connecting to Immich with URL '%s': Status code %d", param.immichURL, res.StatusCode) return fmt.Errorf("Error connecting to Immich with URL '%s': Status code %d", param.ImmichURL, res.StatusCode)
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
var albums []map[string]interface{} var albums []Album
err = json.Unmarshal([]byte(body), &albums) err = json.Unmarshal([]byte(body), &albums)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
return nil return nil
} }
func getSharingKey(album map[string]interface{}, param parameters) (string, error) { func getSharingKey(album Album, param Parameters) (string, error) {
spaceClient := http.Client{ spaceClient := http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
} }
albumId := album["id"].(string) if (album.HasSharedLink) {
albumName := album["albumName"].(string)
if (album["hasSharedLink"].(bool)) {
// Retrieve the existing key // Retrieve the existing key
req, err := http.NewRequest(http.MethodGet, param.immichURL + "/api/shared-links", nil) req, err := http.NewRequest(http.MethodGet, param.ImmichURL + "/api/shared-links", nil)
if err != nil { if err != nil {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err)
} }
req.Header.Set("x-api-key", param.immichKey) req.Header.Set("x-api-key", param.ImmichKey)
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
res, err := spaceClient.Do(req) res, err := spaceClient.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err)
} }
if res.Body != nil { if res.Body != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': Status code %d", albumName, res.StatusCode) return "", fmt.Errorf("Error retrieving sharing key for album '%s': Status code %d", album.Name, res.StatusCode)
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err)
} }
var keys []map[string]interface{} var keys []Key
err = json.Unmarshal([]byte(body), &keys) err = json.Unmarshal([]byte(body), &keys)
if err != nil { if err != nil {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err)
} }
for _, key := range keys { for _, key := range keys {
alb := key["album"].(map[string]interface{}) if (album.ID == key.Album.ID) {
if (albumId == alb["id"].(string)) { return key.Key, nil
return key["key"].(string), nil
} }
} }
return "", fmt.Errorf("Error retrieving sharing key for album '%s': no key found for this albume", albumName) return "", fmt.Errorf("Error retrieving sharing key for album '%s': no key found for this albume", album.Name)
} else { } else {
// Create the missing key // Create the missing key
var jsonData = []byte(`{ var jsonData = []byte(`{
"albumId": "` + albumId + `", "albumId": "` + album.ID + `",
"allowDownload": true, "allowDownload": true,
"allowUpload": false, "allowUpload": false,
"showMetadata": true, "showMetadata": true,
"type": "ALBUM" "type": "ALBUM"
}`) }`)
req, err := http.NewRequest(http.MethodPost, param.immichURL + "/api/shared-links", bytes.NewBuffer(jsonData)) req, err := http.NewRequest(http.MethodPost, param.ImmichURL + "/api/shared-links", bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
return "", fmt.Errorf("Error creating missing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error creating missing key for album '%s': %v", album.Name, err)
} }
req.Header.Set("x-api-key", param.immichKey) req.Header.Set("x-api-key", param.ImmichKey)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/octet-stream") req.Header.Set("Accept", "application/octet-stream")
res, err := spaceClient.Do(req) res, err := spaceClient.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("Error creating missing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error creating missing key for album '%s': %v", album.Name, err)
} }
if res.Body != nil { if res.Body != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if res.StatusCode != 201 { if res.StatusCode != 201 {
return "", fmt.Errorf("Error creating missing key for album '%s': Status code %d", albumName, res.StatusCode) return "", fmt.Errorf("Error creating missing key for album '%s': Status code %d", album.Name, res.StatusCode)
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("Error creating missing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error creating missing key for album '%s': %v", album.Name, err)
} }
var key map[string]interface{} var key Key
err = json.Unmarshal([]byte(body), &key) err = json.Unmarshal([]byte(body), &key)
if err != nil { if err != nil {
return "", fmt.Errorf("Error creating missing key for album '%s': %v", albumName, err) return "", fmt.Errorf("Error creating missing key for album '%s': %v", album.Name, err)
} }
return key["key"].(string), nil return key.Key, nil
} }
} }
func getThumbnail(album map[string]interface{}, param parameters) ([]byte, error) { func getThumbnail(album Album, param Parameters) ([]byte, error) {
spaceClient := http.Client{ spaceClient := http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
} }
albumName := album["albumName"].(string)
req, err := http.NewRequest(http.MethodGet, param.immichURL + "/api/assets/" + album["albumThumbnailAssetId"].(string) + "/thumbnail?size=preview", nil) req, err := http.NewRequest(http.MethodGet, param.ImmichURL + "/api/assets/" + album.AlbumThumbnailAssetId + "/thumbnail?size=preview", nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", albumName, err) return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
req.Header.Set("x-api-key", param.immichKey) req.Header.Set("x-api-key", param.ImmichKey)
req.Header.Set("Accept", "application/octet-stream") req.Header.Set("Accept", "application/octet-stream")
res, err := spaceClient.Do(req) res, err := spaceClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", albumName, err) return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
if res.Body != nil { if res.Body != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': Status code %d", albumName, res.StatusCode) return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': Status code %d", album.Name, res.StatusCode)
} }
thumbnail, err := ioutil.ReadAll(res.Body) thumbnail, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", albumName, err) return nil, fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
return thumbnail, nil return thumbnail, nil
} }
func runLoop(param parameters) error { func runLoop(param Parameters) error {
// Create new WhatsApp connection and connect // Create new WhatsApp connection and connect
client, err := connect(param) client, err := connect(param)
if err != nil { if err != nil {
@@ -292,87 +293,76 @@ func runLoop(param parameters) error {
spaceClient := http.Client{ spaceClient := http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
} }
req, err := http.NewRequest(http.MethodGet, param.immichURL + "/api/albums", nil) req, err := http.NewRequest(http.MethodGet, param.ImmichURL + "/api/albums", nil)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
req.Header.Set("x-api-key", param.immichKey) req.Header.Set("x-api-key", param.ImmichKey)
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
res, err := spaceClient.Do(req) res, err := spaceClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
if res.Body != nil { if res.Body != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
return fmt.Errorf("Error connecting to Immich with URL '%s': Status code %d", param.immichURL, res.StatusCode) return fmt.Errorf("Error connecting to Immich with URL '%s': Status code %d", param.ImmichURL, res.StatusCode)
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
var albums []map[string]interface{} var albums []Album
err = json.Unmarshal([]byte(body), &albums) err = json.Unmarshal([]byte(body), &albums)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.immichURL, err) return fmt.Errorf("Error connecting to Immich with URL '%s': %v", param.ImmichURL, err)
} }
for _, album := range albums { for _, album := range albums {
albumName := album["albumName"].(string)
albumDate, err := time.Parse(time.RFC3339, album["startDate"].(string))
if err != nil {
return fmt.Errorf("Incorrect time format received from Immich: %v", err)
}
createDate, err := time.Parse(time.RFC3339, album["createdAt"].(string))
if err != nil {
return fmt.Errorf("Incorrect time format received from Immich: %v", err)
}
isShared := album["shared"].(bool)
// Get albums from x years ago // Get albums from x years ago
if isShared && (albumDate.Month() == time.Now().Month()) && (albumDate.Day() == time.Now().Day()) { if album.Shared && (album.StartDate.Month() == time.Now().Month()) && (album.StartDate.Day() == time.Now().Day()) {
// Retrieve the sharing key // Retrieve the sharing key
sharingKey, err := getSharingKey(album, param) sharingKey, err := getSharingKey(album, param)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", albumName, err) return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err)
} }
// Retrieve the thumbnail // Retrieve the thumbnail
thumbnail, err := getThumbnail(album, param) thumbnail, err := getThumbnail(album, param)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", albumName, err) return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
// Send the message // Send the message
link := param.immichURL + "/share/" + sharingKey link := param.ImmichURL + "/share/" + sharingKey
err = sendMessage(client, param.whatsappGroup, fmt.Sprintf("Il y a %d an(s) : %s", time.Now().Year()-albumDate.Year(), link), link, albumName, thumbnail) err = sendMessage(client, param.WhatsappGroup, fmt.Sprintf("Il y a %d an(s) : %s", time.Now().Year()-album.StartDate.Year(), link), link, album.Name, thumbnail)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", albumName, err) fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err)
continue continue
} }
} }
// Get albums created yesterday // Get albums created yesterday
if isShared && (createDate.Year() == time.Now().AddDate(0, 0, -1).Year()) && (createDate.Month() == time.Now().AddDate(0, 0, -1).Month()) && (createDate.Day() == time.Now().AddDate(0, 0, -1).Day()) { if album.Shared && (album.CreatedAt.Year() == time.Now().AddDate(0, 0, -1).Year()) && (album.CreatedAt.Month() == time.Now().AddDate(0, 0, -1).Month()) && (album.CreatedAt.Day() == time.Now().AddDate(0, 0, -1).Day()) {
// Retrieve the sharing key // Retrieve the sharing key
sharingKey, err := getSharingKey(album, param) sharingKey, err := getSharingKey(album, param)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", albumName, err) return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err)
} }
// Retrieve the thumbnail // Retrieve the thumbnail
thumbnail, err := getThumbnail(album, param) thumbnail, err := getThumbnail(album, param)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", albumName, err) return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
// Send the message // Send the message
link := param.immichURL + "/share/" + sharingKey link := param.ImmichURL + "/share/" + sharingKey
err = sendMessage(client, param.whatsappGroup, fmt.Sprintf("Nouvel album : %s", link), link, albumName, thumbnail) err = sendMessage(client, param.WhatsappGroup, fmt.Sprintf("Nouvel album : %s", link), link, album.Name, thumbnail)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", albumName, err) fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err)
continue continue
} }
@@ -395,15 +385,22 @@ func main() {
}() }()
// Load the parameters // Load the parameters
param := loadParameters() param := new(Parameters)
if param.runOnce { param.ImmichURL = os.Getenv("IMMICH-URL")
err := runLoop(param) param.ImmichKey = os.Getenv("IMMICH-KEY")
param.WhatsappSessionFile = os.Getenv("WHATSAPP-SESSION-FILE")
param.WhatsappGroup = os.Getenv("WHATSAPP-GROUP")
param.HealthchecksURL = os.Getenv("HEALTHCHECKS-URL")
_, param.RunOnce = os.LookupEnv("RUN-ONCE")
if param.RunOnce {
err := runLoop(*param)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
} }
} else { } else {
// Test the connexion on startup // Test the connexion on startup
err := testConnexions(param) err := testConnexions(*param)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect: %v\n", err) fmt.Fprintf(os.Stderr, "Unable to connect: %v\n", err)
return return
@@ -421,10 +418,20 @@ func main() {
fmt.Fprintf(os.Stderr, "Sleeping for: %s\n", d) fmt.Fprintf(os.Stderr, "Sleeping for: %s\n", d)
time.Sleep(d) time.Sleep(d)
err := runLoop(param) err := runLoop(*param)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
continue continue
} else {
if param.HealthchecksURL != "" {
client := http.Client{
Timeout: time.Second * 10,
}
_, err := client.Head(param.HealthchecksURL)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
}
}
} }
} }
} }