This commit is contained in:
abs3nt 2023-10-22 14:40:26 -07:00
parent d71eb6a355
commit 76d3221205
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
7 changed files with 241 additions and 201 deletions

View File

@ -17,3 +17,4 @@ uninstall:
install: install:
cp wallhaven_dl /usr/bin cp wallhaven_dl /usr/bin
./wallhaven_dl completion zsh > /usr/share/zsh/site-functions/_wallhaven_dl

21
cmd/root.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "wallhaven_dl",
Short: "A wallpaper downloader and setter",
}
// Execute executes the root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

193
cmd/set.go Normal file
View File

@ -0,0 +1,193 @@
package cmd
import (
"fmt"
"io"
"log"
"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",
"range for setList search (default is 1 Year).",
)
setCmd.PersistentFlags().StringVarP(
&setPurity,
"purity",
"p",
"110",
"purity for the setList search (default is 110).",
)
setCmd.PersistentFlags().
StringVarP(
&setCategories,
"categories",
"c",
"010",
"categories for the setList search (default is 010).",
)
setCmd.PersistentFlags().
StringVarP(
&setSorting,
"sort",
"s",
"toplist",
"sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist (default is toplist)",
)
setCmd.PersistentFlags().
StringVarP(
&setOrder,
"order",
"o",
"desc",
"sort order for results, valid sorts: asc desc (default is desc)",
)
setCmd.PersistentFlags().
BoolVarP(
&localPath,
"localPath",
"l",
false,
"set to true if the argument is to a directory or an image file (default is false)",
)
}
var (
setRange string
setPurity string
setCategories string
setSorting string
setOrder string
localPath bool
setCmd = &cobra.Command{
Use: "set",
Aliases: []string{"s"},
Args: cobra.RangeArgs(0, 1),
Short: "set wallpaper from setlist",
RunE: func(cmd *cobra.Command, args []string) error {
return set(args)
},
}
)
func set(args []string) error {
if localPath {
if len(args) == 0 {
return fmt.Errorf("you must provide a path to an image or directory of images to use this option")
}
filePath := args[0]
fileInfo, err := os.Stat(filePath)
if err != nil {
return err
}
if fileInfo.IsDir() {
files, err := os.ReadDir(filePath)
if err != nil {
return err
}
file := files[rand.Intn(len(files))]
return setWallPaperAndRestartStuff(file.Name())
}
return setWallPaperAndRestartStuff(filePath)
}
seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed)
s := &wallhaven.Search{
Categories: setCategories,
Purities: setPurity,
Sorting: setSorting,
Order: setOrder,
TopRange: setRange,
AtLeast: wallhaven.Resolution{Width: 2560, Height: 1440},
Ratios: []wallhaven.Ratio{
{Horizontal: 16, Vertical: 9},
{Horizontal: 16, Vertical: 10},
},
Page: r.Intn(5) + 1,
}
log.Println(args)
if len(args) > 0 {
s.Query = wallhaven.Q{
Tags: []string{args[0]},
}
}
results, err := wallhaven.SearchWallpapers(s)
if err != nil {
return err
}
result, err := getOrDownload(results, r)
if err != nil {
return err
}
err = setWallPaperAndRestartStuff(result.Path)
if err != nil {
return err
}
return nil
}
func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (wallhaven.Wallpaper, error) {
if len(results.Data) == 0 {
return wallhaven.Wallpaper{}, fmt.Errorf("no wallpapers found")
}
homedir, _ := os.UserHomeDir()
result := results.Data[r.Intn(len(results.Data))]
if _, err := os.Stat(path.Join(homedir, "Pictures/Wallpapers", path.Base(result.Path))); err != nil {
err = result.Download(path.Join(homedir, "Pictures/Wallpapers"))
if err != nil {
return wallhaven.Wallpaper{}, err
}
}
return result, nil
}
func setWallPaperAndRestartStuff(result string) error {
homedir, _ := os.UserHomeDir()
_, err := exec.Command("wal", "--cols16", "-i", path.Join(homedir, "Pictures/Wallpapers", path.Base(result)), "-n", "-a", "85").
Output()
if err != nil {
return err
}
_, err = exec.Command("swww", "img", path.Join(homedir, "/Pictures/Wallpapers", path.Base(result))).
Output()
if err != nil {
return err
}
_, err = exec.Command("restart_dunst").
Output()
if err != nil {
return err
}
_, err = exec.Command("pywalfox", "update").
Output()
if err != nil {
return err
}
source, err := os.Open(path.Join(homedir, ".cache/wal/discord-wal.theme.css"))
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(path.Join(homedir, ".config/Vencord/themes/discord-wal.theme.css"))
if err != nil {
return err
}
_, err = io.Copy(destination, source)
if err != nil {
return err
}
return nil
}

9
go.mod
View File

@ -1,5 +1,10 @@
module main module git.asdf.cafe/abs3nt/wallhaven_dl
go 1.21.3 go 1.21.3
require github.com/alecthomas/kong v0.8.1 require github.com/spf13/cobra v1.7.0
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

18
go.sum
View File

@ -1,8 +1,10 @@
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/alecthomas/kong v0.8.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

186
main.go
View File

@ -1,189 +1,7 @@
package main package main
import ( import "git.asdf.cafe/abs3nt/wallhaven_dl/cmd"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"path"
"time"
"github.com/alecthomas/kong"
"main/wallhaven"
)
var cli struct {
Search struct {
Query string `arg:"" name:"query" help:"what to search for." type:"string"`
} `cmd:"" help:"search for wallpaper"`
Top struct {
Purity string `name:"purity" optional:"" help:"purity of results"`
Range string `name:"range" optional:"" help:"range for toplist"`
} `cmd:"" help:"random toplist wallpaper"`
Img struct {
Path string `arg:"" name:"path" help:"path to image or directory." type:"path"`
} `cmd:"" help:"set from file"`
}
func main() { func main() {
ctx := kong.Parse(&cli) cmd.Execute()
switch ctx.Command() {
case "search <query>":
err := searchAndSet(cli.Search.Query)
if err != nil {
panic(err)
}
case "top":
err := setTop(cli.Top.Purity, cli.Top.Range)
if err != nil {
panic(err)
}
case "img <path>":
err := setFromPath(cli.Img.Path)
if err != nil {
panic(err)
}
default:
panic(ctx.Command())
}
}
func setFromPath(filePath string) error {
fileInfo, err := os.Stat(filePath)
if err != nil {
return err
}
if fileInfo.IsDir() {
files, err := os.ReadDir(filePath)
if err != nil {
return err
}
file := files[rand.Intn(len(files))]
return setWallPaperAndRestartStuff(file.Name())
}
return setWallPaperAndRestartStuff(filePath)
}
func searchAndSet(query string) error {
seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed)
results, err := wallhaven.SearchWallpapers(&wallhaven.Search{
Query: wallhaven.Q{
Tags: []string{query},
},
Categories: "111",
Purities: "110",
Sorting: wallhaven.Relevance,
Order: wallhaven.Desc,
AtLeast: wallhaven.Resolution{Width: 2560, Height: 1400},
Ratios: []wallhaven.Ratio{
{Horizontal: 16, Vertical: 9},
{Horizontal: 16, Vertical: 10},
},
Page: 1,
})
if err != nil {
return err
}
result, err := getOrDownload(results, r)
if err != nil {
return nil
}
err = setWallPaperAndRestartStuff(result.Path)
if err != nil {
return err
}
return nil
}
func setTop(purity, topRange string) error {
seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed)
s := &wallhaven.Search{
Categories: "010",
Purities: "110",
Sorting: wallhaven.Toplist,
Order: wallhaven.Desc,
TopRange: "6m",
AtLeast: wallhaven.Resolution{Width: 2560, Height: 1400},
Ratios: []wallhaven.Ratio{
{Horizontal: 16, Vertical: 9},
{Horizontal: 16, Vertical: 10},
},
Page: r.Intn(5) + 1,
}
if purity != "" {
s.Purities = purity
}
if topRange != "" {
s.TopRange = topRange
}
results, err := wallhaven.SearchWallpapers(s)
if err != nil {
return err
}
result, err := getOrDownload(results, r)
if err != nil {
return err
}
err = setWallPaperAndRestartStuff(result.Path)
if err != nil {
return err
}
return nil
}
func setWallPaperAndRestartStuff(result string) error {
homedir, _ := os.UserHomeDir()
_, err := exec.Command("wal", "--cols16", "-i", path.Join(homedir, "Pictures/Wallpapers", path.Base(result)), "-n", "-a", "85").
Output()
if err != nil {
return err
}
_, err = exec.Command("swww", "img", path.Join(homedir, "/Pictures/Wallpapers", path.Base(result))).
Output()
if err != nil {
return err
}
_, err = exec.Command("restart_dunst").
Output()
if err != nil {
return err
}
_, err = exec.Command("pywalfox", "update").
Output()
if err != nil {
return err
}
source, err := os.Open(path.Join(homedir, ".cache/wal/discord-wal.theme.css"))
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(path.Join(homedir, ".config/Vencord/themes/discord-wal.theme.css"))
if err != nil {
return err
}
_, err = io.Copy(destination, source)
if err != nil {
return err
}
return nil
}
func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (wallhaven.Wallpaper, error) {
if len(results.Data) == 0 {
return wallhaven.Wallpaper{}, fmt.Errorf("no wallpapers found")
}
homedir, _ := os.UserHomeDir()
result := results.Data[r.Intn(len(results.Data))]
if _, err := os.Stat(path.Join(homedir, "Pictures/Wallpapers", path.Base(result.Path))); err != nil {
err = result.Download(path.Join(homedir, "Pictures/Wallpapers"))
if err != nil {
return wallhaven.Wallpaper{}, err
}
}
return result, nil
} }

View File

@ -158,8 +158,8 @@ type Search struct {
Query Q Query Q
Categories string Categories string
Purities string Purities string
Sorting Sort Sorting string
Order Order Order string
TopRange string TopRange string
AtLeast Resolution AtLeast Resolution
Resolutions []Resolution Resolutions []Resolution
@ -176,13 +176,13 @@ func (s Search) toQuery() url.Values {
if s.Purities != "" { if s.Purities != "" {
v.Add("purity", s.Purities) v.Add("purity", s.Purities)
} }
if s.Sorting > 0 { if s.Sorting != "" {
v.Add("sorting", s.Sorting.String()) v.Add("sorting", s.Sorting)
} }
if s.Order > 0 { if s.Order != "" {
v.Add("order", s.Order.String()) v.Add("order", s.Order)
} }
if s.TopRange != "" && s.Sorting == Toplist { if s.TopRange != "" && s.Sorting == "toplist" {
v.Add("topRange", s.TopRange) v.Add("topRange", s.TopRange)
} }
if s.AtLeast.isValid() { if s.AtLeast.isValid() {