-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
126 lines (101 loc) · 2.73 KB
/
main.go
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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/go-co-op/gocron"
"github.com/reujab/wallpaper"
)
var (
// Command line flags
outputFile string
updateTime string
killFlag bool
)
func init() {
flag.StringVar(&outputFile, "o", "", "output file for logs")
flag.StringVar(&updateTime, "t", "08:00", "24-hour UTC time when wallpaper is updated")
flag.BoolVar(&killFlag, "k", false, "update wallpaper once and exit")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, "USAGE: %s [OPTIONS]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "OPTIONS:\n")
flag.PrintDefaults()
}
func main() {
flag.Parse()
output := io.Writer(os.Stdout)
if outputFile != "" {
f, err := os.OpenFile(outputFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Printf("[ERR] unable to open output file %q: %v", outputFile, err)
os.Exit(1)
}
defer f.Close()
output = f
}
log.SetPrefix("[bingo] ")
log.SetOutput(output)
start()
}
func start() {
// set first time on launch
setBingWallpaper()
if killFlag {
log.Printf("[INF] kill flag provided, exiting")
os.Exit(0)
}
s := gocron.NewScheduler(time.UTC)
// set again daily
if _, err := s.Every(1).Day().At(updateTime).Do(setBingWallpaper); err != nil {
log.Printf("[ERR] failed to create daily update job at %q: %v", updateTime, err)
os.Exit(1)
}
log.Printf("[INF] wallpaper will be updated again daily at %s", updateTime)
s.StartBlocking()
}
// setBingWallpaper sets the wallpaper to Bing's current image of the day,
// if it fails an error is logged.
func setBingWallpaper() {
image, err := bingImageOfTheDay()
if err != nil {
log.Printf("[ERR] unable to retrieve Bing image of the day: %v", err)
return
}
log.Printf("[INF] updating wallpaper, url: %q, copyright: %q", image.URL, image.Copyright)
if err := wallpaper.SetFromURL(image.URL); err != nil {
log.Printf("[ERR] unable to set wallpaper: %v", err)
}
}
var client = http.Client{Timeout: 30 * time.Second}
type image struct {
URL string `json:"url"`
Copyright string `json:"copyright"`
}
// bingImageOfTheDay returns Bing's current image of the day.
func bingImageOfTheDay() (*image, error) {
resp, err := client.Get("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1")
if err != nil {
return nil, fmt.Errorf("http GET: %v", err)
}
defer resp.Body.Close()
root := struct {
Images []image `json:"images"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&root); err != nil {
return nil, fmt.Errorf("decode body: %v", err)
}
if len(root.Images) == 0 {
return nil, errors.New("response does not contain an image")
}
image := root.Images[0]
image.URL = "https://www.bing.com" + image.URL
return &image, nil
}