refac
This commit is contained in:
parent
fdda342cf8
commit
86073de942
16
README.md
16
README.md
@ -4,21 +4,21 @@ this is a tool for downloading images from wallhaven and then passing the downlo
|
|||||||
|
|
||||||
```
|
```
|
||||||
Usage:
|
Usage:
|
||||||
wallhaven_dl set [flags]
|
wallhaven_dl search [flags]
|
||||||
|
|
||||||
Aliases:
|
Aliases:
|
||||||
set, s
|
search, s
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
--at-least string minimum resolution for results. (default "2560x1440")
|
--at-least string minimum resolution for results. (default "2560x1440")
|
||||||
-c, --categories string categories for the setList search. (default "010")
|
-c, --categories string categories for the search. (default "010")
|
||||||
-d, --download-path string script to run after downloading the wallpaper
|
-d, --download-path string directory to download the image too
|
||||||
-h, --help help for set
|
-h, --help help for search
|
||||||
-m, --maxPage int number of pages to randomly choose wallpaper from. (default 5)
|
-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")
|
-o, --order string sort order for results, valid sorts: asc desc. (default "desc")
|
||||||
-p, --purity string purity for the setList search. (default "110")
|
-p, --purity string purity for the search. (default "110")
|
||||||
-r, --range string range for setList search. (default "1y")
|
-r, --range string range for search. (default "1y")
|
||||||
--ratios strings ratios to search for. (default [16x9,16x10])
|
--ratios strings ratios to search for. (default [16x9,16x10])
|
||||||
-t, --script string script to run after downloading the wallpaper
|
-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")
|
||||||
```
|
```
|
||||||
|
@ -14,111 +14,111 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(setCmd)
|
rootCmd.AddCommand(searchCmd)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setRange,
|
&searchRange,
|
||||||
"range",
|
"range",
|
||||||
"r",
|
"r",
|
||||||
"1y",
|
"1y",
|
||||||
"range for setList search.",
|
"range for search.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setPurity,
|
&searchPurity,
|
||||||
"purity",
|
"purity",
|
||||||
"p",
|
"p",
|
||||||
"110",
|
"110",
|
||||||
"purity for the setList search.",
|
"purity for the search.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setCategories,
|
&searchCategories,
|
||||||
"categories",
|
"categories",
|
||||||
"c",
|
"c",
|
||||||
"010",
|
"010",
|
||||||
"categories for the setList search.",
|
"categories for the search.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setSorting,
|
&searchSorting,
|
||||||
"sort",
|
"sort",
|
||||||
"s",
|
"s",
|
||||||
"toplist",
|
"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(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setOrder,
|
&searchOrder,
|
||||||
"order",
|
"order",
|
||||||
"o",
|
"o",
|
||||||
"desc",
|
"desc",
|
||||||
"sort order for results, valid sorts: asc desc.",
|
"sort order for results, valid sorts: asc desc.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().IntVarP(
|
searchCmd.PersistentFlags().IntVarP(
|
||||||
&setPage,
|
&searchPage,
|
||||||
"maxPage",
|
"maxPage",
|
||||||
"m",
|
"m",
|
||||||
5,
|
5,
|
||||||
"number of pages to randomly choose wallpaper from.",
|
"number of pages to randomly choose wallpaper from.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringSliceVar(
|
searchCmd.PersistentFlags().StringSliceVar(
|
||||||
&setRatios,
|
&searchRatios,
|
||||||
"ratios",
|
"ratios",
|
||||||
[]string{"16x9", "16x10"},
|
[]string{"16x9", "16x10"},
|
||||||
"ratios to search for.",
|
"ratios to search for.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVar(
|
searchCmd.PersistentFlags().StringVar(
|
||||||
&setAtLeast,
|
&searchAtLeast,
|
||||||
"at-least",
|
"at-least",
|
||||||
"2560x1440",
|
"2560x1440",
|
||||||
"minimum resolution for results.",
|
"minimum resolution for results.",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setScript,
|
&searchScript,
|
||||||
"script",
|
"script",
|
||||||
"t",
|
"t",
|
||||||
"",
|
"",
|
||||||
"script to run after downloading the wallpaper",
|
"script to run after downloading the wallpaper",
|
||||||
)
|
)
|
||||||
setCmd.PersistentFlags().StringVarP(
|
searchCmd.PersistentFlags().StringVarP(
|
||||||
&setPath,
|
&downloadPath,
|
||||||
"download-path",
|
"download-path",
|
||||||
"d",
|
"d",
|
||||||
"",
|
"",
|
||||||
"script to run after downloading the wallpaper",
|
"directory to download the image too",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
setRange string
|
searchRange string
|
||||||
setPurity string
|
searchPurity string
|
||||||
setCategories string
|
searchCategories string
|
||||||
setSorting string
|
searchSorting string
|
||||||
setOrder string
|
searchOrder string
|
||||||
setAtLeast string
|
searchAtLeast string
|
||||||
setScript string
|
searchScript string
|
||||||
setPath string
|
downloadPath string
|
||||||
setRatios []string
|
searchRatios []string
|
||||||
setPage int
|
searchPage int
|
||||||
setCmd = &cobra.Command{
|
searchCmd = &cobra.Command{
|
||||||
Use: "set",
|
Use: "search",
|
||||||
Aliases: []string{"s"},
|
Aliases: []string{"s"},
|
||||||
Args: cobra.RangeArgs(0, 1),
|
Args: cobra.RangeArgs(0, 1),
|
||||||
Short: "Wallhaven downloader with the option to run a script after the image has been downloaded",
|
Short: "Wallhaven downloader with the option to run a script after the image has been downloaded",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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())
|
seed := rand.NewSource(time.Now().UnixNano())
|
||||||
r := rand.New(seed)
|
r := rand.New(seed)
|
||||||
s := &wallhaven.Search{
|
s := &wallhaven.Search{
|
||||||
Categories: setCategories,
|
Categories: searchCategories,
|
||||||
Purities: setPurity,
|
Purities: searchPurity,
|
||||||
Sorting: setSorting,
|
Sorting: searchSorting,
|
||||||
Order: setOrder,
|
Order: searchOrder,
|
||||||
TopRange: setRange,
|
TopRange: searchRange,
|
||||||
AtLeast: setAtLeast,
|
AtLeast: searchAtLeast,
|
||||||
Ratios: setRatios,
|
Ratios: searchRatios,
|
||||||
Page: r.Intn(setPage) + 1,
|
Page: r.Intn(searchPage) + 1,
|
||||||
}
|
}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
s.Query = wallhaven.Q{
|
s.Query = wallhaven.Q{
|
||||||
@ -133,8 +133,8 @@ func set(args []string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if setScript != "" {
|
if searchScript != "" {
|
||||||
err = runScript(resultPath, setScript)
|
err = runScript(resultPath, searchScript)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -147,9 +147,8 @@ func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (string, erro
|
|||||||
return "", fmt.Errorf("no wallpapers found")
|
return "", fmt.Errorf("no wallpapers found")
|
||||||
}
|
}
|
||||||
homedir, _ := os.UserHomeDir()
|
homedir, _ := os.UserHomeDir()
|
||||||
downloadPath := path.Join(homedir, "Pictures/Wallpapers")
|
if downloadPath == "" {
|
||||||
if setPath != "" {
|
downloadPath = path.Join(homedir, "Pictures/Wallpapers")
|
||||||
downloadPath = setPath
|
|
||||||
}
|
}
|
||||||
result := results.Data[r.Intn(len(results.Data))]
|
result := results.Data[r.Intn(len(results.Data))]
|
||||||
fullPath := path.Join(downloadPath, path.Base(result.Path))
|
fullPath := path.Join(downloadPath, path.Base(result.Path))
|
Loading…
Reference in New Issue
Block a user