diff options
| author | Paul Buetow <paul@buetow.org> | 2024-11-08 22:48:51 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-11-08 22:48:51 +0200 |
| commit | 7a63e78baf3fe9f769ad4a0569baf8ca71363cfe (patch) | |
| tree | d696caa20f94e8c05a8619240428cffb94fdf85d | |
| parent | e9142b9d7a2faf5c7dc5b13a621d3a216bce8e54 (diff) | |
can download preview image for linkedin
| -rw-r--r-- | cmd/gos/main.go | 3 | ||||
| -rw-r--r-- | internal/config/args.go | 1 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 8 | ||||
| -rw-r--r-- | internal/platforms/linkedin/preview.go | 32 |
4 files changed, 44 insertions, 0 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go index 8e9e4e8..774dd13 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -22,6 +22,8 @@ func main() { browser := flag.String("browser", "firefox", "OAuth2 browser") secretsConfigPath := filepath.Join(os.Getenv("HOME"), ".config/gos/gosec.json") secretsConfigPath = *flag.String("secretsConfig", secretsConfigPath, "Gos' secret config") + cacheDir := filepath.Join(os.Getenv("HOME"), ".config/gos/cache") + cacheDir = *flag.String("cacheDir", cacheDir, "Go's cache dir") platforms := flag.String("platforms", "Mastodon:500,LinkedIn:1000", "Platforms enabled plus their post size limits") target := flag.Int("target", 2, "How many posts per week are the target?") minQueued := flag.Int("minQueued", 4, "Minimum of queued items until printing a warn message!") @@ -45,6 +47,7 @@ func main() { PauseDays: *pauseDays, Lookback: time.Duration(*lookback) * time.Hour * 24, SecretsConfigPath: secretsConfigPath, + CacheDir: cacheDir, Secrets: secrets, OAuth2Browser: *browser, } diff --git a/internal/config/args.go b/internal/config/args.go index 4d65297..77c3850 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -20,6 +20,7 @@ type Args struct { MaxDaysQueued int PauseDays int Lookback time.Duration + CacheDir string SecretsConfigPath string Secrets Secrets OAuth2Browser string diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index a8f7a13..1d57a9e 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -58,6 +58,14 @@ func post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) return err } + var filePath string + if prev.imageURL != "" { + if filePath, err = prev.DownloadImage(args.CacheDir); err != nil { + return err + } + log.Println("Downloaded preview image to ", filePath) + } + question := fmt.Sprintf("Do you want to post this message to Linkedin (%v)?", prev) if err := prompt.FileAction(question, content, en.Path); err != nil { return err diff --git a/internal/platforms/linkedin/preview.go b/internal/platforms/linkedin/preview.go index 765a487..2e40798 100644 --- a/internal/platforms/linkedin/preview.go +++ b/internal/platforms/linkedin/preview.go @@ -8,7 +8,10 @@ import ( "log" "net/http" "net/url" + "os" + "path/filepath" + "codeberg.org/snonux/gos/internal/oi" "golang.org/x/net/html" ) @@ -48,6 +51,35 @@ func (p preview) Empty() bool { return p.url == "" } +func (p preview) DownloadImage(destPath string) (string, error) { + if err := oi.EnsureDir(destPath); err != nil { + return "", err + } + resp, err := http.Get(p.imageURL) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("bad status while trying to download image: %s", resp.Status) + } + + destFile := fmt.Sprintf("%s/%s", destPath, filepath.Base(p.imageURL)) + out, err := os.Create(destFile) + if err != nil { + return destFile, fmt.Errorf("%s: %w", destFile, err) + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + return destFile, err + } + + return destFile, nil +} + func findTitle(n *html.Node) (string, error) { var title string var traverse func(*html.Node) |
