Skip to content

Commit

Permalink
File name format using go templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hegde committed Aug 1, 2023
1 parent db05789 commit a1e01a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ rrip --template-filter='{{not .over_18}}' --max-files=20 r/AMOLEDBackgrounds
## Example: Log links to a file with author, upvote ratio, and quoted title.
## Use dry run (-d) to skip download
rrip -d --data-output-file=amoled.txt --data-output-format='{{.upvote_ratio}} {{.author}} {{.quoted_title}}' r/AMOLEDBackgrounds

## Example: Change file name format using Go templates.
rrip --filename-format='{{.author}} {{.title}} {{.score}}' r/AMOLEDBackgrounds
```

## Caveats
Expand Down
32 changes: 22 additions & 10 deletions rrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
UserAgent = "rrip / Go CLI Tool"
DefaultLimit = 100
defaultDataOutputFormat = "{{.final_url}}"
defaultFileNameFormat = "{{.title}}"
)

var terminalColumns = getTerminalSize()
Expand Down Expand Up @@ -187,13 +188,13 @@ func Traverse(path string, handler PostHandler) {
query := url.Values{}

unsuffixedPath := strings.TrimSuffix(path, "/")
target := "https://www.reddit.com/" + unsuffixedPath

// front page URL
if unsuffixedPath == "" && options.Search == "" && options.Sort == "" {
options.Sort = "hot"
if options.Search == "" && unsuffixedPath == "" {
fatal("Please provide a search string or subreddit")
}

target := "https://www.reddit.com/" + unsuffixedPath

after := options.After

// Handle sort options
Expand Down Expand Up @@ -366,8 +367,8 @@ func DownloadPost(post PostData, postDataMap map[string]any) {
return
}

filename := title + " [" + strings.TrimPrefix(post.Name, "t3_") +
"]" + extension
filenameRaw := formatTemplate(options.FilenameFormat, postDataMap)
filename := fmt.Sprintf("%s [%s]%s", filenameRaw, post.Id, extension)
filename = sanitizeFileName(filename, options.AllowSpecialChars)
log("URL: ", url, " | Score:", post.Score)
if imageUrl != url {
Expand Down Expand Up @@ -536,7 +537,7 @@ func main() {
var titleContains, titleNotContains string
var flairContains, flairNotContains string
var linkContains, linkNotContains string
var dataOutputFormat, templateFilter string
var dataOutputFormat, templateFilter, filenameFormat string

// option parsing
flag.BoolVarP(&options.Debug, "verbose", "v", false, "Enable verbose output (devel)")
Expand All @@ -554,6 +555,7 @@ func main() {
flag.StringVarP(&dataOutputFileName, "data-output-file", "O", "", "Log media links to given file")
flag.StringVarP(&dataOutputFormat, "data-output-format", "f", defaultDataOutputFormat, "Template for saving post data")
flag.StringVar(&templateFilter, "template-filter", "", "Posts will be ignored if this template evaluates to \"false\", \"0\" or empty string")
flag.StringVarP(&filenameFormat, "filename-format", "t", defaultFileNameFormat, "Template for naming files. (Post ID is always appended)")

flag.StringVar(&options.OgType, "og-type", "", "Look Up for a media link in page's og:property"+
" if link itself is not image/video (experimental). supported values: video, image, any")
Expand Down Expand Up @@ -586,7 +588,7 @@ func main() {

flag.Parse()
args := flag.Args()
if len(args) != 1 || help {
if (len(args) != 1 && options.Search == "") || help {
eprintf("Usage: %s <options> <r/subreddit>\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
Expand All @@ -603,7 +605,12 @@ func main() {
defer options.DataOutputFile.Close()
}

path := strings.TrimSuffix(args[0], "/")
var path string = ""

if len(args) > 0 {
// TODO: Join multireddits
path = strings.TrimSuffix(args[0], "/")
}

// validate some arguments
toCheck := map[string]int64{
Expand Down Expand Up @@ -682,6 +689,7 @@ func main() {
}{
{"data-output-format", &options.DataOutputFormat, dataOutputFormat},
{"template-filter", &options.TemplateFilter, templateFilter},
{"filename-format", &options.FilenameFormat, filenameFormat},
}

for _, tv := range templateVals {
Expand All @@ -691,8 +699,12 @@ func main() {
}

// Create folder
folderPath := "rrip-downloads"
if path != "" {
folderPath = path
}
options.Folder = coalesce(options.Folder,
strings.TrimPrefix(strings.ReplaceAll(path, "/", "."), "r."))
strings.TrimPrefix(strings.ReplaceAll(folderPath, "/", "."), "r."))
_, err = os.Stat(options.Folder)

// Note: not creating folder anew if dry run
Expand Down
13 changes: 7 additions & 6 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Options struct {
DataOutputFile io.WriteCloser
DataOutputFormat *template.Template
TemplateFilter *template.Template
FilenameFormat *template.Template
PrintPostData bool
TitleContains, TitleNotContains *regexp.Regexp
FlairContains, FlairNotContains *regexp.Regexp
Expand All @@ -42,12 +43,12 @@ type ImagePreview struct {
}

type PostData struct {
Url, Name, Title string
Score int
Subreddit, Author string
LinkFlairText string
CreatedUtc int64
Preview struct {
Url, Name, Title, Id string
Score int
Subreddit, Author string
LinkFlairText string
CreatedUtc int64
Preview struct {
Images []ImagePreview
}
}
Expand Down

0 comments on commit a1e01a2

Please sign in to comment.