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
|
|
|
)
|
|
|
|
setCmd.PersistentFlags().
|
|
|
|
StringVarP(
|
|
|
|
&setCategories,
|
|
|
|
"categories",
|
|
|
|
"c",
|
|
|
|
"010",
|
2023-10-22 22:24:07 +00:00
|
|
|
"categories for the setList search.",
|
2023-10-22 21:40:26 +00:00
|
|
|
)
|
|
|
|
setCmd.PersistentFlags().
|
|
|
|
StringVarP(
|
|
|
|
&setSorting,
|
|
|
|
"sort",
|
|
|
|
"s",
|
|
|
|
"toplist",
|
2023-10-22 22:24:07 +00:00
|
|
|
"sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist.",
|
2023-10-22 21:40:26 +00:00
|
|
|
)
|
|
|
|
setCmd.PersistentFlags().
|
|
|
|
StringVarP(
|
|
|
|
&setOrder,
|
|
|
|
"order",
|
|
|
|
"o",
|
|
|
|
"desc",
|
2023-10-22 22:24:07 +00:00
|
|
|
"sort order for results, valid sorts: asc desc.",
|
|
|
|
)
|
|
|
|
setCmd.PersistentFlags().
|
|
|
|
IntVarP(
|
|
|
|
&setPage,
|
|
|
|
"maxPage",
|
|
|
|
"m",
|
|
|
|
5,
|
|
|
|
"number of pages to randomly choose wallpaper from.",
|
2023-10-22 21:40:26 +00:00
|
|
|
)
|
2023-10-22 22:24:07 +00:00
|
|
|
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-22 21:40:26 +00:00
|
|
|
)
|
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-22 22:25:11 +00:00
|
|
|
Short: "query wallhaven with the provided parameters then download the image, set it as wallpaper, and generate colorscheme",
|
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
|
|
|
|
}
|