summary refs log tree commit diff stats
path: root/cmd/cetus-bing/cetus-bing.go
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-15 17:13:28 +0530
committerAndinus <andinus@inventati.org>2020-03-15 17:13:28 +0530
commit16bcb7f173399c17b73469ac26d20e0d29c59e0d (patch)
treee27a1b0b3c648bd26d8aa0079f4cce060d5fda58 /cmd/cetus-bing/cetus-bing.go
parent88f7ad2ae17b9be5b55b03dd014901ed3b1bea85 (diff)
downloadcetus-16bcb7f173399c17b73469ac26d20e0d29c59e0d.tar.gz
Support Bing Photo of the Day
Diffstat (limited to 'cmd/cetus-bing/cetus-bing.go')
-rw-r--r--cmd/cetus-bing/cetus-bing.go121
1 files changed, 121 insertions, 0 deletions
diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go
new file mode 100644
index 0000000..e4aae89
--- /dev/null
+++ b/cmd/cetus-bing/cetus-bing.go
@@ -0,0 +1,121 @@
+// Copyright (c) 2020, Andinus <andinus@inventati.org>
+
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"math/rand"
+	"time"
+
+	"framagit.org/andinus/cetus/pkg/background"
+	"framagit.org/andinus/cetus/pkg/bing"
+	"framagit.org/andinus/cetus/pkg/cetus"
+)
+
+var (
+	quiet     bool
+	version   bool
+	fetchOnly bool
+	pathOnly  bool
+
+	api     string
+	random  bool
+	timeout time.Duration
+
+	err       error
+	bpodRes   bing.BPOD
+	bpodPhoto bing.Photo
+)
+
+func main() {
+	parseFlags()
+
+	if version {
+		cetus.Version()
+		return
+	}
+	rand.Seed(time.Now().Unix())
+
+	// Convert timeout to seconds
+	timeout = timeout * time.Second
+
+	// get response from api
+	bpodRes, err = getBPODRes()
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// if random was set then bpodRes holds list of multiple
+	// responses, choose a random response from the list
+	var i int = rand.Intn(len(bpodRes.Photos))
+	bpodPhoto = bpodRes.Photos[i]
+	bpodPhoto.URL = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.URL)
+	printDetails(bpodPhoto)
+
+	// if fetchOnly is true then don't set background
+	if fetchOnly {
+		return
+	}
+
+	// if media type is an image then set background
+	err = background.Set(bpodPhoto.URL)
+	if err != nil {
+		log.Fatal(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(&random, "random", false, "Choose a random image (from 7 images)")
+	flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image")
+
+	flag.StringVar(&api, "api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
+
+	flag.DurationVar(&timeout, "timeout", 32*time.Second, "Timeout for http client in seconds")
+	flag.Parse()
+
+}
+
+func printDetails(bpodPhoto bing.Photo) {
+	if quiet {
+		return
+	}
+	if pathOnly {
+		cetus.PrintPath(bpodPhoto.URL)
+		return
+	}
+	fmt.Printf("Title: %s\n\n", bpodPhoto.Title)
+	fmt.Printf("Copyright: %s\n", bpodPhoto.Copyright)
+	fmt.Printf("Copyright Link: %s\n", bpodPhoto.CopyrightLink)
+	fmt.Printf("Date: %s\n\n", bpodPhoto.StartDate)
+	fmt.Printf("URL: %s\n\n", bpodPhoto.URL)
+}
+
+func getBPODRes() (bing.BPOD, error) {
+	var bpodInfo map[string]string
+	bpodInfo = make(map[string]string)
+	bpodInfo["api"] = api
+	if random {
+		bpodInfo["random"] = "true"
+	}
+	bpodRes, err = bing.BPODPath(bpodInfo, timeout)
+
+	return bpodRes, err
+}
evision' href='/danisanti/profani-tty/blame/tests/functionaltests/test_carbons.c?id=6a8656a06b209e7e181ad8670fe50b7e6fe89572'>^
bdcc0ddb ^


7f98e013 ^
bdcc0ddb ^





7f98e013 ^
bdcc0ddb ^

7f98e013 ^



bdcc0ddb ^








fca59a31 ^





























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