wallhaven_dl/cmd/set.go

172 lines
3.4 KiB
Go
Raw Normal View History

2023-10-22 21:40:26 +00:00
package cmd
import (
"fmt"
"math/rand"
"os"
"os/exec"
"path"
"time"
"github.com/spf13/cobra"
"git.asdf.cafe/abs3nt/wallhaven_dl/src/wallhaven"
)
func init() {
rootCmd.AddCommand(setCmd)
setCmd.PersistentFlags().StringVarP(
&setRange,
"range",
"r",
"1y",
2023-10-22 22:24:07 +00:00
"range for setList search.",
2023-10-22 21:40:26 +00:00
)
setCmd.PersistentFlags().StringVarP(
&setPurity,
"purity",
"p",
"110",
2023-10-22 22:24:07 +00:00
"purity for the setList search.",
2023-10-22 21:40:26 +00:00
)
2023-10-24 18:59:32 +00:00
setCmd.PersistentFlags().StringVarP(
&setCategories,
"categories",
"c",
"010",
"categories for the setList search.",
)
setCmd.PersistentFlags().StringVarP(
&setSorting,
"sort",
"s",
"toplist",
"sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist.",
)
setCmd.PersistentFlags().StringVarP(
&setOrder,
"order",
"o",
"desc",
"sort order for results, valid sorts: asc desc.",
)
setCmd.PersistentFlags().IntVarP(
&setPage,
"maxPage",
"m",
5,
"number of pages to randomly choose wallpaper from.",
)
setCmd.PersistentFlags().StringSliceVar(
&setRatios,
"ratios",
[]string{"16x9", "16x10"},
"ratios to search for.",
)
setCmd.PersistentFlags().StringVar(
&setAtLeast,
"at-least",
"2560x1440",
"minimum resolution for results.",
)
2023-10-24 18:45:46 +00:00
setCmd.PersistentFlags().StringVarP(
&setScript,
"script",
"t",
"",
"script to run after downloading the wallpaper",
)
setCmd.PersistentFlags().StringVarP(
&setPath,
"download-path",
"d",
"",
"script to run after downloading the wallpaper",
)
2023-10-22 21:40:26 +00:00
}
var (
setRange string
setPurity string
setCategories string
setSorting string
setOrder string
2023-10-22 22:24:07 +00:00
setAtLeast string
2023-10-24 18:45:46 +00:00
setScript string
setPath string
2023-10-22 22:24:07 +00:00
setRatios []string
setPage int
2023-10-22 21:40:26 +00:00
setCmd = &cobra.Command{
Use: "set",
Aliases: []string{"s"},
Args: cobra.RangeArgs(0, 1),
2023-10-24 18:59:32 +00:00
Short: "Wallhaven downloader with the option to run a script after the image has been downloaded",
2023-10-22 21:40:26 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return set(args)
},
}
)
func set(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,
2023-10-22 22:24:07 +00:00
AtLeast: setAtLeast,
Ratios: setRatios,
Page: r.Intn(setPage) + 1,
2023-10-22 21:40:26 +00:00
}
if len(args) > 0 {
s.Query = wallhaven.Q{
Tags: []string{args[0]},
}
}
results, err := wallhaven.SearchWallpapers(s)
if err != nil {
return err
}
2023-10-24 18:45:46 +00:00
resultPath, err := getOrDownload(results, r)
2023-10-22 21:40:26 +00:00
if err != nil {
return err
}
2023-10-24 18:45:46 +00:00
if setScript != "" {
err = runScript(resultPath, setScript)
if err != nil {
return err
}
2023-10-22 21:40:26 +00:00
}
return nil
}
2023-10-24 18:45:46 +00:00
func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (string, error) {
2023-10-22 21:40:26 +00:00
if len(results.Data) == 0 {
2023-10-24 18:45:46 +00:00
return "", fmt.Errorf("no wallpapers found")
2023-10-22 21:40:26 +00:00
}
homedir, _ := os.UserHomeDir()
2023-10-24 18:45:46 +00:00
downloadPath := path.Join(homedir, "Pictures/Wallpapers")
if setPath != "" {
downloadPath = setPath
}
2023-10-22 21:40:26 +00:00
result := results.Data[r.Intn(len(results.Data))]
2023-10-24 18:45:46 +00:00
fullPath := path.Join(downloadPath, path.Base(result.Path))
if _, err := os.Stat(fullPath); err != nil {
err = result.Download(path.Join(downloadPath))
2023-10-22 21:40:26 +00:00
if err != nil {
2023-10-24 18:45:46 +00:00
return "", err
2023-10-22 21:40:26 +00:00
}
}
2023-10-24 18:45:46 +00:00
return fullPath, nil
2023-10-22 21:40:26 +00:00
}
2023-10-24 18:45:46 +00:00
func runScript(imgPath, script string) error {
_, err := exec.Command(script, imgPath).Output()
2023-10-22 21:40:26 +00:00
if err != nil {
return err
}
return nil
}