wallhaven_dl/cmd/search.go

171 lines
3.5 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() {
2023-10-24 19:52:26 +00:00
rootCmd.AddCommand(searchCmd)
searchCmd.PersistentFlags().StringVarP(
&searchRange,
2023-10-22 21:40:26 +00:00
"range",
"r",
"1y",
2023-10-24 19:52:26 +00:00
"range for search.",
2023-10-22 21:40:26 +00:00
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&searchPurity,
2023-10-22 21:40:26 +00:00
"purity",
"p",
"110",
2023-10-24 19:52:26 +00:00
"purity for the search.",
2023-10-22 21:40:26 +00:00
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&searchCategories,
2023-10-24 18:59:32 +00:00
"categories",
"c",
"010",
2023-10-24 19:52:26 +00:00
"categories for the search.",
2023-10-24 18:59:32 +00:00
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&searchSorting,
2023-10-24 18:59:32 +00:00
"sort",
"s",
"toplist",
2023-10-24 19:52:26 +00:00
"sort by for results, valid sorts: date_added, relevance, random, views, favorites, searchlist.",
2023-10-24 18:59:32 +00:00
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&searchOrder,
2023-10-24 18:59:32 +00:00
"order",
"o",
"desc",
"sort order for results, valid sorts: asc desc.",
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().IntVarP(
&searchPage,
2023-10-24 18:59:32 +00:00
"maxPage",
"m",
5,
"number of pages to randomly choose wallpaper from.",
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringSliceVar(
&searchRatios,
2023-10-24 18:59:32 +00:00
"ratios",
[]string{"16x9", "16x10"},
"ratios to search for.",
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVar(
&searchAtLeast,
2023-10-24 18:59:32 +00:00
"at-least",
"2560x1440",
"minimum resolution for results.",
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&searchScript,
2023-10-24 18:45:46 +00:00
"script",
"t",
"",
"script to run after downloading the wallpaper",
)
2023-10-24 19:52:26 +00:00
searchCmd.PersistentFlags().StringVarP(
&downloadPath,
2023-10-24 18:45:46 +00:00
"download-path",
"d",
"",
2023-10-24 19:52:26 +00:00
"directory to download the image too",
2023-10-24 18:45:46 +00:00
)
2023-10-22 21:40:26 +00:00
}
var (
2023-10-24 19:52:26 +00:00
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",
2023-10-22 21:40:26 +00:00
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 {
2023-10-24 19:52:26 +00:00
return search(args)
2023-10-22 21:40:26 +00:00
},
}
)
2023-10-24 19:52:26 +00:00
func search(args []string) error {
2023-10-22 21:40:26 +00:00
seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed)
s := &wallhaven.Search{
2023-10-24 19:52:26 +00:00
Categories: searchCategories,
Purities: searchPurity,
Sorting: searchSorting,
Order: searchOrder,
TopRange: searchRange,
AtLeast: searchAtLeast,
Ratios: searchRatios,
Page: r.Intn(searchPage) + 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 19:52:26 +00:00
if searchScript != "" {
err = runScript(resultPath, searchScript)
2023-10-24 18:45:46 +00:00
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 19:52:26 +00:00
if downloadPath == "" {
downloadPath = path.Join(homedir, "Pictures/Wallpapers")
2023-10-24 18:45:46 +00:00
}
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
}