Skip to content

Commit a23c91b

Browse files
committed
Fix several issues
- Handle / in titles -> _ in generated file paths - better cross-platform editor support (Windows)
1 parent f97eb0a commit a23c91b

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

grabbit.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"runtime"
1314
"strings"
1415

1516
"github.com/mitchellh/go-homedir"
@@ -70,20 +71,6 @@ func downloadFile(URL string, fileName string, force bool) error {
7071
return nil
7172
}
7273

73-
func fileNameFromUrl(fullUrl string) (string, error) {
74-
// https://www.golangprograms.com/golang-download-image-from-given-url.html
75-
fileUrl, err := url.Parse(fullUrl)
76-
if err != nil {
77-
return "", err
78-
}
79-
80-
path := fileUrl.Path
81-
segments := strings.Split(path, "/")
82-
83-
fileName := segments[len(segments)-1]
84-
return fileName, nil
85-
}
86-
8774
func getTopPosts(client *reddit.Client, ctx context.Context, subredditName string, postLimit int, timeframe string) ([]*reddit.Post, error) {
8875
posts, _, err := client.Subreddit.TopPosts(ctx, subredditName, &reddit.ListPostOptions{
8976
ListOptions: reddit.ListOptions{
@@ -97,6 +84,30 @@ func getTopPosts(client *reddit.Client, ctx context.Context, subredditName strin
9784
return posts, nil
9885
}
9986

87+
func genFilePath(destinationDir string, title string, fullUrl string) (string, error) {
88+
// https://www.golangprograms.com/golang-download-image-from-given-url.html
89+
fileUrl, err := url.Parse(fullUrl)
90+
if err != nil {
91+
return "", err
92+
}
93+
94+
path := fileUrl.Path
95+
segments := strings.Split(path, "/")
96+
97+
fileName := segments[len(segments)-1]
98+
99+
if err != nil {
100+
return "", err
101+
}
102+
103+
for _, s := range []string{" ", "/", "\\", "\n", "\r", "\x00"} {
104+
title = strings.ReplaceAll(title, s, "_")
105+
}
106+
fileName = title + "_" + fileName
107+
fileName = filepath.Join(destinationDir, fileName)
108+
return fileName, nil
109+
}
110+
100111
func grab(configPath string) error {
101112
configPath, err := homedir.Expand(configPath)
102113
if err != nil {
@@ -144,14 +155,11 @@ func grab(configPath string) error {
144155
for _, post := range posts {
145156
if strings.HasSuffix(post.URL, ".jpg") {
146157

147-
urlFileName, err := fileNameFromUrl(post.URL)
158+
filePath, err := genFilePath(subreddit.Destination, post.Title, post.URL)
148159
if err != nil {
149-
log.Printf("fileNameFromUrl: %v: %v: %v\n", subreddit.Name, post.URL, err)
160+
log.Printf("genFilePath: %v: %v: %v\n", subreddit.Name, post.URL, err)
150161
}
151-
fileName := strings.Replace(post.Title, " ", "_", -1) + "_" + urlFileName
152-
fileName = filepath.Join(subreddit.Destination, fileName)
153-
154-
err = downloadFile(post.URL, fileName, false)
162+
err = downloadFile(post.URL, filePath, false)
155163
if err != nil {
156164
log.Printf("downloadFile: %v: %v: %v\n", subreddit.Name, post.URL, err)
157165
}
@@ -187,7 +195,15 @@ func editFile(path string, defaultContent []byte, rm bool) error {
187195

188196
editor := os.Getenv("EDITOR")
189197
if editor == "" {
190-
editor = "vim"
198+
if runtime.GOOS == "windows" {
199+
editor = "notepad"
200+
} else if runtime.GOOS == "darwin" {
201+
editor = "open"
202+
} else if runtime.GOOS == "linux" {
203+
editor = "xdg-open"
204+
} else {
205+
editor = "vim"
206+
}
191207
}
192208
executable, err := exec.LookPath(editor)
193209
if err != nil {
@@ -232,7 +248,7 @@ func run() error {
232248
app := kingpin.New("grabbit", "Get top images from subreddits").UsageTemplate(kingpin.DefaultUsageTemplate)
233249
app.HelpFlag.Short('h')
234250

235-
editConfigCmd := app.Command("edit-config", "Edit or create configuration file. Uses $EDITOR or vim")
251+
editConfigCmd := app.Command("edit-config", "Edit or create configuration file. Uses $EDITOR or the OS's default editor for yaml files")
236252
editConfigCmdConfigPathFlag := editConfigCmd.Flag("config-path", "config filepath").Short('p').Default(defaultConfigPath).String()
237253

238254
grabCmd := app.Command("grab", "Grab images. Use `edit-config` first to create a config")

0 commit comments

Comments
 (0)