-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1083 lines (990 loc) · 31.8 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"context"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"github.com/golang-migrate/migrate/v4/source/iofs"
"golang/api"
"golang/models"
"golang/openai"
"golang/stablediffusion"
"golang/unsplash"
"golang/util"
"html/template"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
//go:embed sql/migrations/*.sql
var MigrationSrc embed.FS
var (
Settings map[string]string
Templates map[string]string
WordPressStatus = false
OpenAiStatus = false
SdStatus = false
UnsplashStatus = false
Greeting string
Selfie []byte
LastTestTime string
cronSrv *gocron.Scheduler
webSrv *http.Server
apiSrv *http.Server
wg *sync.WaitGroup
WebStatus = false
ApiStatus = false
CronStatus = false
RestartChannel chan Restart
)
type Restart struct{}
func main() {
util.Init()
util.Logger.Info().Msg("Starting Blogotron")
RestartChannel = make(chan Restart)
//DB
dbName := "data/blogtron.db"
err := models.ConnectDatabase(dbName)
if err != nil {
util.Logger.Error().Err(err).Msg("Could not connect to database file " + dbName)
return
}
migSrc, err := iofs.New(MigrationSrc, "sql/migrations")
if err != nil {
util.Logger.Error().Err(err).Msg("Could not load migrations")
return
}
err = models.MigrateDatabase(migSrc)
if err != nil {
util.Logger.Error().Err(err).Msg("Could not migrate database")
return
}
Settings, err = models.GetSettingsSimple()
if err != nil {
util.Logger.Error().Err(err).Msg("Could not load settings from db")
return
}
Templates, err = models.GetTemplatesSimple()
if err != nil {
util.Logger.Error().Err(err).Msg("Could not load templates from db")
return
}
if Settings["ENABLE_STARTUP_TESTS"] == "true" {
runSystemTests()
} else {
loadCachedTestResults()
}
//Thread Mgmt
wg = new(sync.WaitGroup)
util.Logger.Info().Msg("Starting Thread Management")
wg.Add(1)
//Web Service
startWebSrv()
//API Service
startApiSrv()
//Cron Service
startCronSrv()
util.Logger.Info().Msg("Startup Completed")
_ = <-RestartChannel
util.Logger.Info().Msg("Restarting System")
restartSystem()
util.Logger.Info().Msg("System Restarted")
wg.Wait()
}
func restartSystem() {
stopCronSrv()
stopWebSrv()
stopApiSrv()
//while servers statuses are true, wait
for WebStatus || ApiStatus || CronStatus {
time.Sleep(1 * time.Second)
}
util.Logger.Info().Msg("All Shut Down")
startCronSrv()
startWebSrv()
startApiSrv()
}
func startCronSrv() {
//Cron Service
autoPost := Settings["AUTO_POST_ENABLE"]
autoPostInterval := Settings["AUTO_POST_INTERVAL"]
autoPostImgEngine := Settings["AUTO_POST_IMG_ENGINE"]
autoPostLen := Settings["AUTO_POST_LEN"]
autoPostState := Settings["AUTO_POST_STATE"]
lowIdeaThreshold := Settings["LOW_IDEA_THRESHOLD"]
cronSrv = gocron.NewScheduler(time.UTC)
iThreshold, convErr := strconv.Atoi(lowIdeaThreshold)
if convErr != nil {
iThreshold = 0
}
if iThreshold > 0 {
util.Logger.Info().Msg("Auto Idea Generation Enabled - Low Idea Threshold Set to " + strconv.Itoa(iThreshold) + " ideas")
cronSrv.Every("1h").Do(func() {
util.Logger.Info().Msg("Checking Idea Levels")
//Check count of total open ideas
ideaCount := models.GetOpenIdeaCount()
util.Logger.Info().Msg("Idea Count: " + strconv.Itoa(ideaCount) + " Threshold: " + strconv.Itoa(iThreshold))
if ideaCount < iThreshold {
util.Logger.Info().Msg("Idea Count is below threshold - Generating 10 new concepts and 10 new ideas for each concept")
fullBrainstorm("10", false)
}
})
} else {
util.Logger.Info().Msg("Auto Idea Generation Disabled")
}
if autoPost == "true" {
util.Logger.Info().Msg("Auto Post Enabled - Interval Set to " + autoPostInterval)
cronSrv.Every(autoPostInterval).Do(func() {
util.Logger.Info().Msg("Auto Post Triggered")
//Get a Random Idea
idea := models.GetRandomIdea()
if idea.Id == 0 {
util.Logger.Info().Msg("Could not get random idea")
} else {
util.Logger.Info().Msg("Random Idea: " + idea.IdeaText)
//Create a new post from the idea
iLen, convErr := strconv.Atoi(autoPostLen)
if convErr != nil {
iLen = 750
}
publishStatus := autoPostState
unsplashSearch := ""
unsplashImg := false
generateImg := false
if autoPostImgEngine == "unsplash" {
unsplashImg = true
} else if autoPostImgEngine == "generate" {
generateImg = true
}
post := Post{
Prompt: idea.IdeaText,
Length: iLen,
PublishStatus: publishStatus,
UseGpt4: false,
ConceptAsTitle: false,
IncludeYt: false,
GenerateImg: generateImg,
DownloadImg: false,
UnsplashImg: unsplashImg,
IdeaId: strconv.Itoa(idea.Id),
UnsplashSearch: unsplashSearch,
Concept: idea.IdeaConcept,
}
err, post := writeArticle(post)
if err != nil {
util.Logger.Error().Err(err).Msg("Could not write article")
}
}
})
}
util.Logger.Info().Msg("Starting Cron Server")
go func() {
//Start cron
cronSrv.StartBlocking()
wg.Done()
util.Logger.Info().Msg("Cron Server Stopped")
}()
CronStatus = true
util.Logger.Info().Msg("Started Cron Server")
}
func startWebSrv() {
//WEB SERVER
webPort := Settings["BLOGOTRON_PORT"]
if webPort == "" {
webPort = "8666"
}
fs := http.FileServer(http.Dir("assets"))
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/create", createHandler)
mux.HandleFunc("/write", writeHandler)
mux.HandleFunc("/ideaList", ideaListHandler)
mux.HandleFunc("/aiIdea", aiIdeaHandler)
mux.HandleFunc("/idea", ideaHandler)
mux.HandleFunc("/ideaSave", ideaSaveHandler)
mux.HandleFunc("/ideaDel", ideaRemoveHandler)
mux.HandleFunc("/series", seriesHandler)
mux.HandleFunc("/seriesList", seriesListHandler)
mux.HandleFunc("/seriesSave", seriesSaveHandler)
mux.HandleFunc("/settings", settingsHandler)
mux.HandleFunc("/settingsSave", settingsSaveHandler)
mux.HandleFunc("/templates", templateHandler)
mux.HandleFunc("/templatesSave", templateSaveHandler)
mux.HandleFunc("/test", testHandler)
mux.HandleFunc("/retest", retestHandler)
mux.HandleFunc("/restart", restartHandler)
mux.HandleFunc("/articles", articleListHandler)
mux.HandleFunc("/article", articleHandler)
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
webSrv = &http.Server{Addr: ":" + webPort, Handler: mux}
wg.Add(1)
util.Logger.Info().Msg("Starting Web Server")
go func() {
err := webSrv.ListenAndServe()
if err != nil {
if err.Error() != "http: Server closed" {
util.Logger.Error().Err(err).Msg("Error stopping web server")
}
}
wg.Done()
util.Logger.Info().Msg("Web Server Stopped")
}()
WebStatus = true
util.Logger.Info().Msg("Started Web Server on Port " + webPort + "")
}
func startApiSrv() {
apiPort := Settings["BLOGOTRON_API_PORT"]
gin.SetMode(gin.ReleaseMode)
apiGin := gin.Default()
v1 := apiGin.Group("/api/v1")
{
v1.GET("idea", api.GetIdeas)
v1.GET("idea/:id", api.GetIdeaById)
v1.POST("idea", api.AddIdea)
v1.PUT("idea/:id", api.UpdateIdea)
v1.DELETE("idea/:id", api.DeleteIdea)
v1.OPTIONS("idea", api.Options)
}
util.Logger.Info().Msg("Starting Gin Server")
apiSrv = &http.Server{Addr: ":" + apiPort, Handler: apiGin}
wg.Add(1)
go func() {
err := apiSrv.ListenAndServe()
if err != nil {
util.Logger.Error().Err(err).Msg("Error starting gin server")
}
wg.Done()
util.Logger.Info().Msg("Gin Server Stopped")
}()
ApiStatus = true
util.Logger.Info().Msg("Started Gin Server on Port " + apiPort + "")
}
func stopApiSrv() {
util.Logger.Info().Msg("Stopping API Server")
//issue shutdown to apiSrv and wait for it to complete
apiSrv.Shutdown(context.Background())
ApiStatus = false
util.Logger.Info().Msg("API Server Stopped")
}
func stopWebSrv() {
util.Logger.Info().Msg("Stopping Web Server")
webSrv.Shutdown(context.Background())
WebStatus = false
util.Logger.Info().Msg("Web Server Stopped")
}
func stopCronSrv() {
util.Logger.Info().Msg("Stopping Cron Server")
cronSrv.Stop()
cronSrv.Clear()
CronStatus = false
util.Logger.Info().Msg("Cron Server Stopped")
}
func generateSizedImage(p string, iWidth int, iHeight int) ([]byte, error) {
var imgBytes []byte
imgSampler := Settings["IMG_SAMPLER"]
imgUpscaler := Settings["IMG_UPSCALER"]
imgNegativePrompts := Settings["IMG_NEGATIVE_PROMPTS"]
imgSteps := Settings["IMG_STEPS"]
iSteps, err := strconv.Atoi(imgSteps)
if err != nil {
iSteps = 30
}
if p != "" {
imgMode := Settings["IMG_MODE"]
if imgMode == "openai" {
aiApiKey := Settings["OPENAI_API_KEY"]
imgBytes, err = openai.GenerateImg(p, aiApiKey)
if err != nil {
return nil, err
}
} else if imgMode == "sd" {
sdUrl := Settings["SD_URL"]
ctx := context.Background()
images, err := stablediffusion.Generate(sdUrl, ctx, stablediffusion.SimpleImageRequest{
Prompt: p,
NegativePrompt: imgNegativePrompts,
Styles: nil,
Seed: -1,
SamplerName: imgSampler,
BatchSize: 1,
NIter: 1,
Steps: iSteps,
CfgScale: 7,
Width: iWidth,
Height: iHeight,
SNoise: 0,
OverrideSettings: struct{}{},
OverrideSettingsRestoreAfterwards: false,
SaveImages: true,
EnableHr: true,
HrScale: 2,
HrUpscaler: imgUpscaler,
})
if err != nil {
return nil, err
} else {
imgBytes = images.Images[0]
}
}
}
return imgBytes, nil
}
func generateImage(p string) ([]byte, error) {
imgWidth := Settings["IMG_WIDTH"]
imgHeight := Settings["IMG_HEIGHT"]
iWidth, err := strconv.Atoi(imgWidth)
if err != nil {
iWidth = 512
}
iHeight, err := strconv.Atoi(imgHeight)
if err != nil {
iHeight = 512
}
return generateSizedImage(p, iWidth, iHeight)
}
func writeArticle(post Post) (error, Post) {
newImgPrompt := ""
article := ""
title := ""
aiApiKey := Settings["OPENAI_API_KEY"]
if post.Prompt != "" {
if post.Keyword == "" {
kwTmpl := template.Must(template.New("keyword-prompt").Parse(openai.KeywordTemplate))
keywordPrompt := new(bytes.Buffer)
err := kwTmpl.Execute(keywordPrompt, post)
if err != nil {
return err, post
}
keywordResp, err := openai.GenerateKeywords(aiApiKey, post.UseGpt4, keywordPrompt.String(), Templates["system-prompt"])
if err != nil {
return err, post
}
post.Keyword = keywordResp
}
wpTmpl := template.Must(template.New("web-prompt").Parse(Templates["article-prompt"]))
webPrompt := new(bytes.Buffer)
err := wpTmpl.Execute(webPrompt, post)
if err != nil {
return err, post
}
util.Logger.Info().Msg("Generating Article from Prompt" + webPrompt.String() + "")
articleResp, err := openai.GenerateArticle(aiApiKey, post.UseGpt4, webPrompt.String(), Templates["system-prompt"])
if err != nil {
return err, post
}
article = articleResp
//Attempt to parse out title from h1 tag
if strings.Contains(article, "<h1>") && strings.Contains(article, "</h1>") && strings.HasPrefix(article, "<h1>") {
tempTitle := strings.Split(strings.Split(article, "<h1>")[1], "</h1>")[0]
if tempTitle != "Introduction" {
title = tempTitle
//Remove title from article
article = strings.Replace(article, "<h1>"+title+"</h1>", "", 1)
//Remove any leading newlines from article
article = strings.TrimPrefix(article, "\n")
}
}
if title == "" {
if !post.ConceptAsTitle {
titleResp, err := openai.GenerateTitle(aiApiKey, false, article, Templates["title-prompt"], Templates["system-prompt"])
if err != nil {
return err, post
}
title = titleResp
} else {
title = post.Prompt
}
}
//Generate description
if post.Description == "" {
descTmpl := template.Must(template.New("description-prompt").Parse(Templates["description-prompt"]))
descPrompt := new(bytes.Buffer)
err := descTmpl.Execute(descPrompt, post)
descResp, err := openai.GenerateDescription(aiApiKey, false, article, descPrompt.String(), Templates["system-prompt"])
if err != nil {
return err, post
}
post.Description = descResp
}
if post.IncludeYt && post.YtUrl != "" {
article = article + "\n<p>[embed]" + post.YtUrl + "[/embed]</p>"
}
//if title starts with a quote, remove it and if title ends with a quote, remove it
if strings.HasPrefix(title, "\"") {
title = strings.TrimPrefix(title, "\"")
}
if strings.HasSuffix(title, "\"") {
title = strings.TrimSuffix(title, "\"")
}
post.Content = article
post.Title = title
} else {
post.Error = "Please input an article idea first."
}
if post.Error == "" && post.GenerateImg {
if post.ImagePrompt == "" {
igTmpl := template.Must(template.New("imggen-prompt").Parse(Templates["imggen-prompt"]))
imgGenPrompt := new(bytes.Buffer)
err := igTmpl.Execute(imgGenPrompt, post)
imgGenResp, err := openai.GenerateImagePrompt(aiApiKey, false, title, imgGenPrompt.String(), Templates["system-prompt"])
if err != nil {
return err, post
}
imgGenResp = strings.Replace(imgGenResp, "\"", "", 1)
imgGenResp = strings.Replace(imgGenResp, "Create an image of ", "", 1)
imgGenResp = strings.Replace(imgGenResp, "Can you create an image of ", "", 1)
post.ImagePrompt = imgGenResp
}
util.Logger.Info().Msg("Img Prompt in is: " + post.ImagePrompt)
imgTmpl := template.Must(template.New("img-prompt").Parse(Templates["img-prompt"]))
imgBuiltPrompt := new(bytes.Buffer)
err := imgTmpl.Execute(imgBuiltPrompt, post)
if err != nil {
return err, post
}
newImgPrompt = imgBuiltPrompt.String()
util.Logger.Info().Msg("Img Prompt Out is: " + newImgPrompt)
imgBytes, err := generateImage(newImgPrompt)
if err != nil {
return err, post
}
post.Image = imgBytes
} else if post.Error == "" && post.DownloadImg && post.ImgUrl != "" {
response, err := http.Get(post.ImgUrl)
if err != nil {
return err, post
}
defer func() {
response.Body.Close()
}()
if response.StatusCode != 200 {
post.Error = "Bad response code downloading image: " + strconv.Itoa(response.StatusCode)
}
imgBytes, err := io.ReadAll(response.Body)
if err != nil {
return err, post
}
post.Image = imgBytes
} else if post.Error == "" && post.UnsplashImg && post.UnsplashSearch != "" {
unsplashKey := Settings["UNSPLASH_ACCESS_KEY"]
imgBytes, err := unsplash.GetImageBySearch(unsplashKey, post.UnsplashSearch)
if err != nil {
return err, post
}
post.Image = imgBytes
} else if post.Error == "" && post.UnsplashImg && post.UnsplashSearch == "" {
imgSearchResp, err := openai.GenerateImageSearch(aiApiKey, false, title, Templates["imgsearch-prompt"], Templates["system-prompt"])
if err != nil {
return err, post
}
post.UnsplashSearch = imgSearchResp
unsplashKey := Settings["UNSPLASH_ACCESS_KEY"]
imgBytes, err := unsplash.GetImageBySearch(unsplashKey, imgSearchResp)
if err != nil {
return err, post
}
post.Image = imgBytes
}
post.ImageB64 = base64.StdEncoding.EncodeToString(post.Image)
postId, mediaId, err := postToWordpress(post)
if err != nil {
return err, post
} else {
models.SetIdeaWritten(post.IdeaId)
}
post.WordPressId = postId
//Write Post as Article to DB
articleDb := models.Article{
Title: post.Title,
Content: post.Content,
Description: post.Description,
PrimaryKeyword: post.Keyword,
MediaId: mediaId,
Prompt: post.Prompt,
YtUrl: post.YtUrl,
ImgPrompt: newImgPrompt,
ImgSearch: post.UnsplashSearch,
ImgSrcUrl: post.ImgUrl,
Concept: post.Concept,
IdeaId: post.IdeaId,
Status: "written",
Version: 1,
WordPressId: postId,
}
articleId, err := models.UpsertArticle(articleDb)
if err != nil {
util.Logger.Error().Err(err).Msg("Error writing article to DB")
return err, post
}
post.ArticleId = int(articleId)
return nil, post
}
func generateIdeas(ideaCount string, builtConcept string, useGpt4 bool, sid int, ideaConcept string) {
prompt := Prompt{
IdeaCount: ideaCount,
IdeaConcept: builtConcept,
}
aiApiKey := Settings["OPENAI_API_KEY"]
ideaTmpl := template.Must(template.New("idea-prompt").Parse(openai.IdeaTemplate))
ideaPrompt := new(bytes.Buffer)
err := ideaTmpl.Execute(ideaPrompt, prompt)
if err != nil {
util.Logger.Error().Err(err).Msg("Error executing idea template")
} else {
util.Logger.Info().Msg("Prompt is: " + ideaPrompt.String())
ideaResp, err := openai.GenerateIdeas(aiApiKey, useGpt4, ideaPrompt.String(), Templates["system-prompt"])
if err != nil {
util.Logger.Error().Err(err).Msg("Error generating ideas")
} else {
ideaResp = strings.ReplaceAll(ideaResp, "\n", "")
util.Logger.Info().Msg("Idea Brainstorm Results: " + ideaResp)
ideaList := strings.Split(ideaResp, "|")
for index, value := range ideaList {
util.Logger.Info().Msgf("Index: %d, Value: %s\n", index, value)
if strings.TrimSpace(value) != "" {
idea := models.Idea{
IdeaText: strings.TrimSpace(value),
Status: "NEW",
IdeaConcept: ideaConcept,
SeriesId: sid,
}
_, err = models.AddIdea(idea)
if err != nil {
util.Logger.Error().Err(err).Msg("Error adding idea")
}
}
}
}
}
}
func fullBrainstorm(ideaCount string, useGpt4 bool) {
conceptList := ""
builtTopic := ""
aiApiKey := Settings["OPENAI_API_KEY"]
concepts, _ := models.GetIdeaConcepts()
series, _ := models.GetSeries()
for _, concept := range concepts {
conceptList = conceptList + ", " + concept
}
for _, s := range series {
conceptList = conceptList + ", " + s.SeriesPrompt
}
builtTopic = builtTopic + " Previous topics used include: " + conceptList + "."
ideaTmpl := template.Must(template.New("topic-prompt").Parse(openai.TopicTemplate))
ideaPrompt := new(bytes.Buffer)
prompt := Prompt{
IdeaCount: ideaCount,
IdeaConcept: builtTopic,
}
err := ideaTmpl.Execute(ideaPrompt, prompt)
if err != nil {
util.Logger.Error().Err(err).Msg("Error executing idea template")
} else {
util.Logger.Info().Msg("Topic Prompt is: " + ideaPrompt.String())
ideaResp, err := openai.GenerateTopics(aiApiKey, useGpt4, ideaPrompt.String(), Templates["system-prompt"])
if err != nil {
util.Logger.Error().Err(err).Msg("Error generating ideas")
} else {
ideaResp = strings.ReplaceAll(ideaResp, "\n", "")
util.Logger.Info().Msg("Idea Brainstorm Results: " + ideaResp)
ideaList := strings.Split(ideaResp, "|")
for _, value := range ideaList {
builtConcept := "The topic for the ideas is: \"" + value + "\"."
generateIdeas(ideaCount, builtConcept, useGpt4, 0, value)
}
}
}
}
func getWpTitles() ([]string, error) {
// Create an HTTP client
client := &http.Client{}
// Define the URL and request method
baseUrl := Settings["WP_URL"]
//IF baseUrl ends with a slash, remove it
if baseUrl[len(baseUrl)-1:] == "/" {
baseUrl = baseUrl[:len(baseUrl)-1]
}
url := baseUrl + "/wp-json/wp/v2/posts?per_page=100"
method := "GET"
// Create a request
req, err := http.NewRequest(method, url, nil)
if err != nil {
util.Logger.Error().Err(err).Msg("Error creating request for title list")
return nil, err
}
req = setReqHeaders(req, "", "application/json")
// Send the request
res, err := client.Do(req)
if err != nil {
util.Logger.Error().Err(err).Msg("Error sending request for title list")
return nil, err
}
// Read the response body
body, err := ioutil.ReadAll(res.Body)
if err != nil {
util.Logger.Error().Err(err).Msg("Error reading response body for title list")
return nil, err
}
// Close the response body
res.Body.Close()
// Parse the JSON data
var posts []map[string]interface{}
err = json.Unmarshal(body, &posts)
if err != nil {
util.Logger.Error().Err(err).Msg("Error parsing JSON for title list")
return nil, err
}
// Create a slice of strings to store the titles
var titles []string
// Loop through the posts and add the titles to the slice
for _, post := range posts {
titles = append(titles, post["title"].(map[string]interface{})["rendered"].(string))
}
// Return the titles
return titles, nil
}
type PostResponse struct {
ID int `json:"id"`
}
func doWordpressPost(endPoint string, postData map[string]interface{}) (int, error) {
// Convert the post data to JSON
jsonData, err := json.Marshal(postData)
if err != nil {
return -1, err
}
// Create an HTTP client
client := &http.Client{}
// Define the URL and request method
baseUrl := Settings["WP_URL"]
//IF baseUrl ends with a slash, remove it
if baseUrl[len(baseUrl)-1:] == "/" {
baseUrl = baseUrl[:len(baseUrl)-1]
}
url := baseUrl + endPoint //"/wp-json/wp/v2/posts"
method := "POST"
// Create a request with the JSON data
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil {
return -1, err
}
contentLength := strconv.Itoa(len(jsonData))
req = setReqHeaders(req, contentLength, "application/json")
postId := -1
var response PostResponse
// Send the request
res, err := client.Do(req)
if err != nil {
return -1, err
}
defer res.Body.Close()
// Parse the JSON response
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return -1, err
} else {
postId = response.ID
}
// Check the response status code
if res.StatusCode != http.StatusCreated {
return -1, errors.New("Post creation failed. Status code:" + strconv.Itoa(res.StatusCode))
}
return postId, nil
}
func setReqHeaders(req *http.Request, contentLength string, contentType string) *http.Request {
// Define the authentication credentials
username := Settings["WP_USERNAME"]
password := Settings["WP_PASSWORD"]
req.Header.Set("Content-Type", contentType)
// Set the content type header
if contentLength != "" {
req.Header.Set("Content-Length", contentLength)
}
// Set the content length header
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Accept", "*/*")
req.Header.Set("Connection", "keep-alive")
// Set the host header
req.Header.Set("Host", strings.ReplaceAll(strings.ReplaceAll(Settings["WP_URL"], "https://", ""), "http://", ""))
req.Header.Set("User-Agent", "PostmanRuntime/7.26.8")
//req.Header.Set("Accept-Encoding", "gzip, deflate, br")
// Encode the username and password in base64
auth := username + ":" + password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
// Set the Authorization header for basic authentication
req.Header.Set("Authorization", basicAuth)
return req
}
type MediaResponse struct {
ID int `json:"id"`
Link string `json:"link"`
}
func postImageToWordpress(imgBytes []byte, description string) int {
// Create a new multipart writer
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Create a new form file field for the image
part, err := writer.CreateFormFile("file", "image.jpg")
if err != nil {
util.Logger.Error().Err(err).Msg("Error creating form file field")
return 0
}
// Copy the image bytes to the form file field
_, err = io.Copy(part, bytes.NewReader(imgBytes))
if err != nil {
util.Logger.Error().Err(err).Msg("Error copying image bytes")
return 0
}
// Add the alt text as a form field
err = writer.WriteField("alt_text", description)
if err != nil {
util.Logger.Error().Err(err).Msg("Error writing alt text field")
}
// Close the multipart writer
err = writer.Close()
if err != nil {
util.Logger.Error().Err(err).Msg("Error closing multipart writer")
return 0
}
// Create an HTTP client
client := &http.Client{}
// Define the URL and request method
baseUrl := Settings["WP_URL"]
//IF baseUrl ends with a slash, remove it
if baseUrl[len(baseUrl)-1:] == "/" {
baseUrl = baseUrl[:len(baseUrl)-1]
}
url := baseUrl + "/wp-json/wp/v2/media"
method := "POST"
// Create a request with the multipart body
req, err := http.NewRequest(method, url, body)
mediaID := 0
if err != nil {
util.Logger.Error().Err(err).Msg("Error creating request for image upload")
return 0
} else {
// Calculate the content length
contentLength := strconv.Itoa(body.Len())
req = setReqHeaders(req, contentLength, writer.FormDataContentType())
// Send the request
res, err := client.Do(req)
if err != nil {
util.Logger.Error().Err(err).Msg("Error sending request")
return 0
}
defer res.Body.Close()
// Read the response body
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
util.Logger.Error().Err(err).Msg("Error reading response body")
return 0
}
// Check the response status code
if res.StatusCode != http.StatusCreated {
util.Logger.Error().Msg("Image upload failed with status code " + strconv.Itoa(res.StatusCode) + ". Response body: " + string(responseBody) + ".")
return 0
}
// Parse the response body to get the media ID
var mediaResp MediaResponse
err = json.Unmarshal(responseBody, &mediaResp)
if err != nil {
util.Logger.Error().Err(err).Msg("Error parsing response body")
return 0
}
mediaID = mediaResp.ID
util.Logger.Info().Msg("Image uploaded successfully! Media ID:" + strconv.Itoa(mediaID))
}
return mediaID
}
func postToWordpress(post Post) (int, int, error) {
postData := map[string]interface{}{}
mediaId := -1
if len(post.Image) > 0 {
util.Logger.Info().Msg("Processing Image Upload")
mediaId = postImageToWordpress(post.Image, post.ImagePrompt)
postData = map[string]interface{}{
"title": post.Title,
"content": post.Content,
"status": post.PublishStatus,
"featured_media": mediaId,
"excerpt": post.Description,
}
} else {
// Define the post data
postData = map[string]interface{}{
"title": post.Title,
"content": post.Content,
"status": post.PublishStatus,
"excerpt": post.Description,
}
}
postId, err := doWordpressPost("/wp-json/wp/v2/posts", postData)
if err != nil {
util.Logger.Error().Err(err).Msg("Error creating post")
return -1, -1, err
}
util.Logger.Info().Msg("Post created successfully!")
return postId, mediaId, nil
}
func loadSettings() {
newSettings, err := models.GetSettingsSimple()
if err != nil {
util.Logger.Error().Err(err).Msg("Error loading settings")
} else {
Settings = newSettings
}
}
func loadTemplates() {
newTemplates, err := models.GetTemplatesSimple()
if err != nil {
util.Logger.Error().Err(err).Msg("Error loading templates")
} else {
Templates = newTemplates
}
}
func testWordPress() {
//Test WordPress Connection
WordPressStatus = false
util.Logger.Info().Msg("Testing WordPress Connection...")
_, err := getWpTitles()
if err != nil {
util.Logger.Error().Err(err).Msg("Error getting WordPress titles")
} else {
util.Logger.Info().Msg("WordPress Connection Successful!")
WordPressStatus = true
}
models.UpsertStatus("WordPress", strconv.FormatBool(WordPressStatus))
LastTestTime = time.Now().Format("Jan 2, 2006 at 3:04pm (MST)")
models.UpsertStatus("LastTestTime", LastTestTime)
}
func testOpenAI() {
OpenAiStatus = false
//Test OpenAI Connection
util.Logger.Info().Msg("Testing OpenAI Connection...")
aiTestResp, err := openai.GenerateTestGreeting(Settings["OPENAI_API_KEY"], false, "You are running your start-up diagnostics, compose some humorous fake startup sequence events and a greeting as a sort of boot-up log and return them. This response should be formatted an <ul> in HTML to be inserted into a status page. Class \"font-monospace\" should be used on the text to give it a robotic feel. The page already exists, we just need to drop in the HTML greeting inside the existing HTML page we have, so it should not include a body or head or close or open html tags, just the markup for the text itself within the page.", "You are Blog-o-Tron a sophisticated, AI-powered blogging robot.")
if err != nil {
util.Logger.Error().Err(err).Msg("Error testing OpenAI API")
} else {
util.Logger.Info().Msg("OpenAI Connection Successful!")
OpenAiStatus = true
Greeting = aiTestResp
}
models.UpsertStatus("OpenAI", strconv.FormatBool(OpenAiStatus))
LastTestTime = time.Now().Format("Jan 2, 2006 at 3:04pm (MST)")
models.UpsertStatus("LastTestTime", LastTestTime)
models.UpsertStatus("Greeting", Greeting)
}
func testUnsplash() {
UnsplashStatus = false
//Test Unsplash Connection
util.Logger.Info().Msg("Testing Unsplash Connection...")
_, err := unsplash.GetImageBySearch(Settings["UNSPLASH_ACCESS_KEY"], "robot")
if err != nil {
util.Logger.Error().Err(err).Msg("Error testing Unsplash API")
} else {
util.Logger.Info().Msg("Unsplash Connection Successful!")
UnsplashStatus = true
}
models.UpsertStatus("Unsplash", strconv.FormatBool(UnsplashStatus))
LastTestTime = time.Now().Format("Jan 2, 2006 at 3:04pm (MST)")
models.UpsertStatus("LastTestTime", LastTestTime)
}
func testStableDiffusion() {
SdStatus = false
//Test StableDiffusion Connection
if Settings["IMG_MODE"] == "sd" {
util.Logger.Info().Msg("Testing StableDiffusion Connection...")
imgResp, err := generateSizedImage("An selfie image of Blog-o-Tron the blog-writing robot sitting in front of a computer in a futuristic lab waving at the camera. Centered and in focus. Photo-realistic, Hyper-realistic, Portrait, Well Lit", 512, 512)
if err != nil {
util.Logger.Error().Err(err).Msg("Error testing StableDiffusion API")
} else {
util.Logger.Info().Msg("StableDiffusion Connection Successful!")
SdStatus = true
Selfie = imgResp
}
} else {
util.Logger.Error().Msg("StableDiffusion is not enabled")
}
models.UpsertStatus("StableDiffusion", strconv.FormatBool(SdStatus))
LastTestTime = time.Now().Format("Jan 2, 2006 at 3:04pm (MST)")
models.UpsertStatus("LastTestTime", LastTestTime)