owl-blogs/plugings/instagram.go

97 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-08-10 19:56:06 +00:00
package plugings
import (
"bytes"
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
entrytypes "owl-blogs/entry_types"
2024-02-21 19:03:18 +00:00
"owl-blogs/render"
2023-08-10 19:56:06 +00:00
"github.com/Davincible/goinsta/v3"
)
type Instagram struct {
configRepo repository.ConfigRepository
binService *app.BinaryService
}
type InstagramConfig struct {
2024-02-21 19:04:22 +00:00
User string
Password string
2023-08-10 19:56:06 +00:00
}
2024-02-21 19:03:18 +00:00
// Form implements app.AppConfig.
func (cfg *InstagramConfig) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/InstagramConfig", cfg)
return f
}
// ParseFormData implements app.AppConfig.
2024-02-21 19:24:18 +00:00
func (cfg *InstagramConfig) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
cfg.User = data.FormValue("User")
cfg.Password = data.FormValue("Password")
return nil
2024-02-21 19:03:18 +00:00
}
2023-08-10 19:56:06 +00:00
func RegisterInstagram(
configRepo repository.ConfigRepository,
configRegister *app.ConfigRegister,
binService *app.BinaryService,
2023-08-11 13:14:38 +00:00
bus *app.EventBus,
2023-08-10 19:56:06 +00:00
) *Instagram {
configRegister.Register("instagram", &InstagramConfig{})
insta := &Instagram{
configRepo: configRepo,
binService: binService,
}
bus.Subscribe(insta)
return insta
}
2023-08-11 13:14:38 +00:00
// NotifyEntryCreated implements app.EntryCreationSubscriber.
func (i *Instagram) NotifyEntryCreated(entry model.Entry) {
2023-08-10 19:56:06 +00:00
image, ok := entry.(*entrytypes.Image)
if !ok {
println("not an image")
return
}
config := &InstagramConfig{}
err := i.configRepo.Get("instagram", config)
if err != nil {
println("no instagram config")
return
}
client := goinsta.New(config.User, config.Password)
err = client.Login()
if err != nil {
println("login failed")
return
}
meta := image.MetaData().(*entrytypes.ImageMetaData)
bin, err := i.binService.FindById(meta.ImageId)
if err != nil {
println("image data not found")
return
}
_, err = client.Upload(
&goinsta.UploadOptions{
File: bytes.NewReader(bin.Data),
Caption: image.Title(),
},
)
if err != nil {
println("upload failed")
return
}
}