summary refs log tree commit diff stats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cetus-wallhaven/cetus-wallhaven.go151
1 files changed, 151 insertions, 0 deletions
diff --git a/cmd/cetus-wallhaven/cetus-wallhaven.go b/cmd/cetus-wallhaven/cetus-wallhaven.go
new file mode 100644
index 0000000..5314db9
--- /dev/null
+++ b/cmd/cetus-wallhaven/cetus-wallhaven.go
@@ -0,0 +1,151 @@
+package main
+
+import (
+	"encoding/json"
+	"flag"
+	"fmt"
+	"math/rand"
+	"time"
+
+	"framagit.org/andinus/cetus/pkg/background"
+	"framagit.org/andinus/cetus/pkg/cetus"
+	"framagit.org/andinus/cetus/pkg/wallhaven"
+)
+
+type wh struct {
+	Photos []photo `json:"data"`
+	// MetaI  []meta  `json:"meta"`
+}
+
+// type meta struct {
+// 	CurrentPage int64  `json:"current_page"`
+// 	LastPage    int64  `json:"last_page"`
+// 	PerPage     int64  `json:"per_page"`
+// 	Total       int64  `json:"total"`
+// 	Query       string `json:"query"`
+// 	Seed        string `json:"seed"`
+// }
+
+type photo struct {
+	Id       string `json:"id"`
+	Url      string `json:"url"`
+	ShortUrl string `json:"short_url"`
+	// Views      int      `json:"views"`
+	// Favorites  int      `json:"favorites"`
+	Source   string `json:"source"`
+	Purity   string `json:"purity"`
+	Category string `json:"general"`
+	// DimensionX int      `json:"dimension_x"`
+	// DimensionY int      `json:"dimension_y"`
+	Resolution string `json:"resolution"`
+	Ratio      string `json:"ratio"`
+	// FileSize   int      `json:"file_size"`
+	FileType  string   `json:"file_type"`
+	CreatedAt string   `json:"created_at"`
+	Path      string   `json:"path"`
+	Colors    []string `json:"colors"`
+	// Thumbs     []thumb  `json:"thumbs"`
+}
+
+// type thumb struct {
+// 	Small    string `json:"small"`
+// 	Original string `json:"original"`
+// 	Large    string `json:"large"`
+// }
+
+var (
+	t         time.Duration
+	api       string
+	apiKey    string
+	dump      bool
+	quiet     bool
+	random    bool
+	version   bool
+	pathOnly  bool
+	fetchOnly bool
+)
+
+func main() {
+	parseFlags()
+
+	if version {
+		cetus.Version()
+		return
+	}
+	rand.Seed(time.Now().Unix())
+
+	// Convert timeout to seconds
+	t = t * time.Second
+
+	body, err := whBody()
+	if dump {
+		fmt.Println(body)
+		return
+	}
+
+	wh := wh{}
+	err = json.Unmarshal([]byte(body), &wh)
+	cetus.ErrChk("body unmarshal failed", err)
+
+	// if random was set then wh holds list of multiple responses,
+	// choose a random response from the list
+	i := rand.Intn(len(wh.Photos))
+	whPhoto := wh.Photos[i]
+
+	printDetails(whPhoto)
+
+	// if fetchOnly is true then don't set background
+	if fetchOnly {
+		return
+	}
+
+	err = background.Set(whPhoto.Path)
+	cetus.ErrChk("setting background failed", err)
+}
+
+func parseFlags() {
+	flag.BoolVar(&quiet, "quiet", false, "No output")
+	flag.BoolVar(&version, "version", false, "Cetus version")
+	flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info")
+	flag.BoolVar(&dump, "dump", false, "Only dump received response")
+	flag.BoolVar(&random, "random", true, "Choose a random image")
+	flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image")
+
+	flag.StringVar(&api, "api", "https://wallhaven.cc/api/v1/search", "Wallhaven Search API URL")
+	flag.StringVar(&apiKey, "api-key", "", "Wallhaven API Key")
+
+	flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds")
+	flag.Parse()
+
+}
+
+func printDetails(whPhoto photo) {
+	if quiet {
+		return
+	}
+	if pathOnly {
+		cetus.PrintPath(whPhoto.Path)
+		return
+	}
+	fmt.Printf("Id: %s\n", whPhoto.Id)
+	fmt.Printf("URL: %s\n", whPhoto.Url)
+	fmt.Printf("Short URL: %s\n", whPhoto.ShortUrl)
+	fmt.Printf("Source: %s\n", whPhoto.Source)
+	fmt.Printf("Date: %s\n\n", whPhoto.CreatedAt)
+	fmt.Printf("Resolution: %s\n", whPhoto.Resolution)
+	fmt.Printf("Ratio: %s\n", whPhoto.Ratio)
+	// fmt.Printf("Views: %s\n", whPhoto.Views)
+	// fmt.Printf("Favorites: %s\n", whPhoto.Favorites)
+	fmt.Printf("Category: %s\n", whPhoto.Category)
+}
+
+func whBody() (string, error) {
+	reqInfo := make(map[string]string)
+	reqInfo["api"] = api
+	if random {
+		reqInfo["random"] = "true"
+	}
+
+	body, err := wallhaven.GetWhJson(reqInfo, t)
+	return body, err
+}
ram <vc@akkartik.com> 2015-04-22 22:34:34 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2015-04-22 22:40:46 -0700 1146 - yet another out-of-bounds access' href='/akkartik/mu/commit/cpp/035call?h=main&id=f1e953d0cf7f5669a55b1337a87e646c1262d139'>f1e953d0 ^
ec926027 ^




363be37f ^
b24eb476 ^
ca01193d ^
363be37f ^
50eab110 ^



1fa53058 ^


ec926027 ^
31401373 ^
ec926027 ^



ca01193d ^
363be37f ^
69e14325 ^
5eb49929 ^
e2240eb4 ^

363be37f ^
77cdc6d0 ^

dd2e01e4 ^
77cdc6d0 ^

31401373 ^
d72f3799 ^
0b0cfb6f ^
7284d503 ^


6424e199 ^













f89378d5 ^
2142ccfc ^

f3760b0f ^
2b250717 ^
2142ccfc ^

f1a6f323 ^
9cf71627 ^
59874868 ^






f1c8df7d ^


59874868 ^


f1c8df7d ^
513bfed8 ^
af085cf6 ^


3eeea0a2 ^

9cf71627 ^
5f98a10c ^

1ead3562 ^
2cb36cd0 ^

2b37bbea ^
2cb36cd0 ^

5f98a10c ^
1ead3562 ^
192d59d3 ^
2cb36cd0 ^
2b37bbea ^
2cb36cd0 ^
7284d503 ^
64cf0a59 ^
d41955c1 ^

69e14325 ^
df8bb4c3 ^
f6d47435 ^
d41955c1 ^

31401373 ^
795f5244 ^
5eb49929 ^

317c0a34 ^
ec926027 ^

ac0e9db5 ^
2e8c5d39 ^
77cdc6d0 ^
dd2e01e4 ^
77cdc6d0 ^


31401373 ^
d8c6265d ^
9fdda88b ^
5f98a10c ^
8eff7919 ^
f6d47435 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158