forked from avinashbot/satellite
/
main.go
78 lines (66 loc) · 1.69 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
package main
import (
"flag"
"log"
"time"
"github.com/avinashbot/satellite/background"
"github.com/avinashbot/satellite/download"
)
var (
satellite string
depth int
every time.Duration
)
func init() {
flag.StringVar(&satellite, "use", "himawari", `The satellite to use: "himawari" or "dscovr".`)
flag.IntVar(&depth, "depth", 4, "Resolution of the Himawari image. One of 4, 8, 16, 20.")
flag.DurationVar(&every, "every", 0, "Time to wait between each rerun.")
}
func main() {
flag.Parse()
// Set the satellite.
var dl download.Downloader
switch satellite {
case "himawari":
dl = download.Himawari{Depth: depth}
case "dscovr":
dl = download.Dscovr{}
default:
log.Fatalln("Satellite not recognized. Exiting.")
}
// Start off with a zero time.
lastTime := time.Time{}
for ; ; time.Sleep(every) {
// Get the filename to the latest image.
filename, err := dl.ModifiedSince(lastTime)
if err != nil {
log.Println(err)
continue
}
if filename == "" {
log.Println("No changes since last time. Trying again later...")
continue
}
// There is new image out. Download it.
log.Println("Starting download...")
benchmarkTime := time.Now()
img, err := dl.Download(filename)
if err != nil {
log.Println(err)
continue
}
log.Printf("Done! Download took %s.\n", time.Now().Sub(benchmarkTime))
// Set the image as the background.
// This one's a serious error, so break if it happens.
log.Println("Setting image as background...")
if err := background.Set(img); err != nil {
log.Fatalln(err)
}
// Success. Replace lastTime with the current time.
lastTime = time.Now()
// If we're only doing this once, quit.
if every == 0 {
break
}
}
}