-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (49 loc) · 1.6 KB
/
main.go
File metadata and controls
60 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"context"
"fmt"
"log"
"time"
"happylittleais/config"
imgGen "happylittleais/image_generator"
"happylittleais/social"
)
// TODO refresh IG token as part of flow
// TODO return named values
// TODO clean up naming
// TODO unit tests
const timeout = 5 * time.Minute
func main() {
configuration, err := config.LoadConfig(".")
if err != nil {
log.Printf("cannot load config from file. Error %v", err)
configuration = config.LoadFromEnvVars()
log.Println("loading config from environment variables")
if len(configuration.ChatGptToken) == 0 || len(configuration.IgID) == 0 || len(configuration.IgToken) == 0 {
log.Panic("Env vars not set.")
}
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
prompt, err := imgGen.GeneratePrompt(ctx, configuration.ChatGptToken)
if err != nil {
log.Panicf("Error generating prompt. %v", err)
}
log.Printf("Prompt:\n%s\n", prompt)
imgUrl, err := imgGen.GenerateImage(ctx, fmt.Sprintf("Digital art, %s", prompt), configuration.ChatGptToken)
if err != nil {
log.Panicf("Generating image failed. %v", err)
}
log.Printf("Encoded image URL:\n%s\n", imgUrl)
caption := fmt.Sprintf("Prompt: %s", prompt)
mediaID, err := social.CreateMedia(ctx, imgUrl, caption, configuration.IgToken, configuration.IgID)
if err != nil {
log.Panicf("Media not created. Error: %v", err)
}
log.Printf("Media ID: %s", mediaID)
err = social.PostImage(ctx, mediaID, configuration.IgToken, configuration.IgID)
if err != nil {
log.Panicf("Could not post image. Error %v\n", err)
}
fmt.Println("Image posted to Instagram.")
}