From 86073de9420226d8c81e14a5b1365280647b8262 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Tue, 24 Oct 2023 12:52:26 -0700 Subject: [PATCH] refac --- README.md | 16 +++--- cmd/{set.go => search.go} | 105 +++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 61 deletions(-) rename cmd/{set.go => search.go} (57%) diff --git a/README.md b/README.md index 8c4b9c9..c7a9669 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,21 @@ this is a tool for downloading images from wallhaven and then passing the downlo ``` Usage: - wallhaven_dl set [flags] + wallhaven_dl search [flags] Aliases: - set, s + search, s Flags: --at-least string minimum resolution for results. (default "2560x1440") - -c, --categories string categories for the setList search. (default "010") - -d, --download-path string script to run after downloading the wallpaper - -h, --help help for set + -c, --categories string categories for the search. (default "010") + -d, --download-path string directory to download the image too + -h, --help help for search -m, --maxPage int number of pages to randomly choose wallpaper from. (default 5) -o, --order string sort order for results, valid sorts: asc desc. (default "desc") - -p, --purity string purity for the setList search. (default "110") - -r, --range string range for setList search. (default "1y") + -p, --purity string purity for the search. (default "110") + -r, --range string range for search. (default "1y") --ratios strings ratios to search for. (default [16x9,16x10]) -t, --script string script to run after downloading the wallpaper - -s, --sort string sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist. (default "toplist") + -s, --sort string sort by for results, valid sorts: date_added, relevance, random, views, favorites, searchlist. (default "toplist") ``` diff --git a/cmd/set.go b/cmd/search.go similarity index 57% rename from cmd/set.go rename to cmd/search.go index 01a3bda..51912a4 100644 --- a/cmd/set.go +++ b/cmd/search.go @@ -14,111 +14,111 @@ import ( ) func init() { - rootCmd.AddCommand(setCmd) - setCmd.PersistentFlags().StringVarP( - &setRange, + rootCmd.AddCommand(searchCmd) + searchCmd.PersistentFlags().StringVarP( + &searchRange, "range", "r", "1y", - "range for setList search.", + "range for search.", ) - setCmd.PersistentFlags().StringVarP( - &setPurity, + searchCmd.PersistentFlags().StringVarP( + &searchPurity, "purity", "p", "110", - "purity for the setList search.", + "purity for the search.", ) - setCmd.PersistentFlags().StringVarP( - &setCategories, + searchCmd.PersistentFlags().StringVarP( + &searchCategories, "categories", "c", "010", - "categories for the setList search.", + "categories for the search.", ) - setCmd.PersistentFlags().StringVarP( - &setSorting, + searchCmd.PersistentFlags().StringVarP( + &searchSorting, "sort", "s", "toplist", - "sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist.", + "sort by for results, valid sorts: date_added, relevance, random, views, favorites, searchlist.", ) - setCmd.PersistentFlags().StringVarP( - &setOrder, + searchCmd.PersistentFlags().StringVarP( + &searchOrder, "order", "o", "desc", "sort order for results, valid sorts: asc desc.", ) - setCmd.PersistentFlags().IntVarP( - &setPage, + searchCmd.PersistentFlags().IntVarP( + &searchPage, "maxPage", "m", 5, "number of pages to randomly choose wallpaper from.", ) - setCmd.PersistentFlags().StringSliceVar( - &setRatios, + searchCmd.PersistentFlags().StringSliceVar( + &searchRatios, "ratios", []string{"16x9", "16x10"}, "ratios to search for.", ) - setCmd.PersistentFlags().StringVar( - &setAtLeast, + searchCmd.PersistentFlags().StringVar( + &searchAtLeast, "at-least", "2560x1440", "minimum resolution for results.", ) - setCmd.PersistentFlags().StringVarP( - &setScript, + searchCmd.PersistentFlags().StringVarP( + &searchScript, "script", "t", "", "script to run after downloading the wallpaper", ) - setCmd.PersistentFlags().StringVarP( - &setPath, + searchCmd.PersistentFlags().StringVarP( + &downloadPath, "download-path", "d", "", - "script to run after downloading the wallpaper", + "directory to download the image too", ) } var ( - setRange string - setPurity string - setCategories string - setSorting string - setOrder string - setAtLeast string - setScript string - setPath string - setRatios []string - setPage int - setCmd = &cobra.Command{ - Use: "set", + searchRange string + searchPurity string + searchCategories string + searchSorting string + searchOrder string + searchAtLeast string + searchScript string + downloadPath string + searchRatios []string + searchPage int + searchCmd = &cobra.Command{ + Use: "search", Aliases: []string{"s"}, Args: cobra.RangeArgs(0, 1), Short: "Wallhaven downloader with the option to run a script after the image has been downloaded", RunE: func(cmd *cobra.Command, args []string) error { - return set(args) + return search(args) }, } ) -func set(args []string) error { +func search(args []string) error { seed := rand.NewSource(time.Now().UnixNano()) r := rand.New(seed) s := &wallhaven.Search{ - Categories: setCategories, - Purities: setPurity, - Sorting: setSorting, - Order: setOrder, - TopRange: setRange, - AtLeast: setAtLeast, - Ratios: setRatios, - Page: r.Intn(setPage) + 1, + Categories: searchCategories, + Purities: searchPurity, + Sorting: searchSorting, + Order: searchOrder, + TopRange: searchRange, + AtLeast: searchAtLeast, + Ratios: searchRatios, + Page: r.Intn(searchPage) + 1, } if len(args) > 0 { s.Query = wallhaven.Q{ @@ -133,8 +133,8 @@ func set(args []string) error { if err != nil { return err } - if setScript != "" { - err = runScript(resultPath, setScript) + if searchScript != "" { + err = runScript(resultPath, searchScript) if err != nil { return err } @@ -147,9 +147,8 @@ func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (string, erro return "", fmt.Errorf("no wallpapers found") } homedir, _ := os.UserHomeDir() - downloadPath := path.Join(homedir, "Pictures/Wallpapers") - if setPath != "" { - downloadPath = setPath + if downloadPath == "" { + downloadPath = path.Join(homedir, "Pictures/Wallpapers") } result := results.Data[r.Intn(len(results.Data))] fullPath := path.Join(downloadPath, path.Base(result.Path))