From a89aa7ee2755a9a03aadd004e9b0c4a00d6d7e08 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Thu, 10 Aug 2023 21:56:06 +0200 Subject: [PATCH] upload images to instagram --- cmd/owl/main.go | 9 +++-- go.mod | 10 ++++++ go.sum | 24 +++++++++++++ plugings/instagram.go | 82 +++++++++++++++++++++++++++++++++++++++++++ web/forms/form.go | 2 ++ web/forms/widget.go | 15 ++++++++ 6 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 plugings/instagram.go diff --git a/cmd/owl/main.go b/cmd/owl/main.go index 3fb2e1b..99a495e 100644 --- a/cmd/owl/main.go +++ b/cmd/owl/main.go @@ -56,9 +56,6 @@ func App(db infra.Database) *web.WebApp { // busses entryCreationBus := app.NewEntryCreationBus() - // plugins - plugings.NewEcho(entryCreationBus) - // Create Services entryService := app.NewEntryService(entryRepo, entryCreationBus) binaryService := app.NewBinaryFileService(binRepo) @@ -67,6 +64,12 @@ func App(db infra.Database) *web.WebApp { interactionRepo, entryRepo, httpClient, ) + // plugins + plugings.NewEcho(entryCreationBus) + plugings.RegisterInstagram( + siteConfigRepo, configRegister, binaryService, entryCreationBus, + ) + // Create WebApp return web.NewWebApp( entryService, entryRegister, binaryService, diff --git a/go.mod b/go.mod index b77c0fd..5d711b9 100644 --- a/go.mod +++ b/go.mod @@ -18,16 +18,26 @@ require ( require ( git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect + github.com/Davincible/goinsta/v3 v3.2.6 // indirect github.com/andybalholm/brotli v1.0.5 // indirect + github.com/chromedp/cdproto v0.0.0-20230808232040-5d0fb3432de3 // indirect + github.com/chromedp/chromedp v0.9.2 // indirect + github.com/chromedp/sysutil v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-ap/errors v0.0.0-20221205040414-01c1adfc98ea // indirect github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.3.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.16.3 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/philhofer/fwd v1.1.2 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect diff --git a/go.sum b/go.sum index d20d35b..d2b7a80 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,16 @@ git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:cliQ4HHsCo6xi2oWZYKWW4bly/Ory9FuTpFPRxj/mAg= git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078/go.mod h1:g/V2Hjas6Z1UHUp4yIx6bATpNzJ7DYtD0FG3+xARWxs= +github.com/Davincible/goinsta/v3 v3.2.6 h1:+lNIWU6NABWd2VSGe83UQypnef+kzWwjmfgGihPbwD8= +github.com/Davincible/goinsta/v3 v3.2.6/go.mod h1:jIDhrWZmttL/gtXj/mkCaZyeNdAAqW3UYjasOUW0YEw= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/chromedp/cdproto v0.0.0-20230802225258-3cf4e6d46a89/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs= +github.com/chromedp/cdproto v0.0.0-20230808232040-5d0fb3432de3 h1:Qhw280TMvghvzNauTPMwEvaHUYWRJSoSlNVMev4lO7M= +github.com/chromedp/cdproto v0.0.0-20230808232040-5d0fb3432de3/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs= +github.com/chromedp/chromedp v0.9.2 h1:dKtNz4kApb06KuSXoTQIyUC2TrA0fhGDwNZf3bcgfKw= +github.com/chromedp/chromedp v0.9.2/go.mod h1:LkSXJKONWTCHAfQasKFUZI+mxqS4tZqhmtGzzhLsnLs= +github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic= +github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -13,6 +22,13 @@ github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 h1:GMKIYXyXPGIp+hYiWO github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73/go.mod h1:jyveZeGw5LaADntW+UEsMjl3IlIwk+DxlYNsbofQkGA= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.2.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= +github.com/gobwas/ws v1.3.0 h1:sbeU3Y4Qzlb+MOzIe6mQGf7QR4Hkv6ZD0qhGkBFL2O0= +github.com/gobwas/ws v1.3.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/gofiber/fiber/v2 v2.47.0 h1:EN5lHVCc+Pyqh5OEsk8fzRiifgwpbrP0rulQ4iNf3fs= github.com/gofiber/fiber/v2 v2.47.0/go.mod h1:mbFMVN1lQuzziTkkakgtKKdjfsXSw9BKR5lmcNksUoU= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -21,10 +37,15 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -35,9 +56,12 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0= github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= diff --git a/plugings/instagram.go b/plugings/instagram.go new file mode 100644 index 0000000..02d34f8 --- /dev/null +++ b/plugings/instagram.go @@ -0,0 +1,82 @@ +package plugings + +import ( + "bytes" + "owl-blogs/app" + "owl-blogs/app/repository" + "owl-blogs/domain/model" + entrytypes "owl-blogs/entry_types" + + "github.com/Davincible/goinsta/v3" +) + +type Instagram struct { + configRepo repository.ConfigRepository + binService *app.BinaryService +} + +type InstagramConfig struct { + User string `owl:"widget=text"` + Password string `owl:"widget=password"` +} + +func RegisterInstagram( + configRepo repository.ConfigRepository, + configRegister *app.ConfigRegister, + binService *app.BinaryService, + bus *app.EntryCreationBus, +) *Instagram { + configRegister.Register("instagram", &InstagramConfig{}) + insta := &Instagram{ + configRepo: configRepo, + binService: binService, + } + + bus.Subscribe(insta) + + return insta +} + +// NotifyEntryCreation implements app.EntryCreationSubscriber. +func (i *Instagram) NotifyEntryCreation(entry model.Entry) { + + 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 + } + +} diff --git a/web/forms/form.go b/web/forms/form.go index 5eae1ba..1deab80 100644 --- a/web/forms/form.go +++ b/web/forms/form.go @@ -61,6 +61,8 @@ func (s *FormField) ToWidget() Widget { return &TextareaWidget{*s} case "textlist": return &TextListWidget{*s} + case "password": + return &PasswordWidget{*s} default: return &TextWidget{*s} } diff --git a/web/forms/widget.go b/web/forms/widget.go index af010e2..0c0c2e6 100644 --- a/web/forms/widget.go +++ b/web/forms/widget.go @@ -26,6 +26,21 @@ func (s *TextWidget) ParseValue(value string, output reflect.Value) error { return nil } +type PasswordWidget struct { + FormField +} + +func (s *PasswordWidget) Html() string { + html := "" + html += fmt.Sprintf("\n", s.Name, s.Value.String()) + return html +} + +func (s *PasswordWidget) ParseValue(value string, output reflect.Value) error { + output.SetString(value) + return nil +} + type TextareaWidget struct { FormField }