-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (94 loc) · 2.2 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
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"time"
"fmt"
"log"
)
func main() {
var forceUpdate bool
flag.BoolVar(&forceUpdate, "f", false, "Force update wallpaper")
flag.Parse()
var config Config
installDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
configFile := installDir + "/config.yml"
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Println("Using default config")
config = Default()
config.Save(configFile)
} else {
log.Println("Using config: " + configFile)
config.Load(configFile)
}
wallpaperDir := config.WallpaperDir + "/wallpapers/"
fsStorage := &FileSystemStorage{Dir: wallpaperDir}
if !forceUpdate {
lastUpdate := config.LastUpdate
lastUpdateTime := time.Unix(lastUpdate, 0)
if lastUpdateTime.Day() == time.Now().Day() {
setWallPaper(wallpaperDir)
log.Println("Wallpaper is already the latest, exiting")
os.Exit(0)
}
}
var ig, iid string
var wallpaper *WallPaper
hasErr := true
for i := 0; i < 10; i++ {
bp := NewBingPage()
imgURL := bp.ImageURL()
wallpaper = FromURL(imgURL)
if wallpaper == nil {
continue
}
fileName := imgURL[strings.LastIndex(imgURL, "/")+1:]
wallpaper.SaveToFile(fsStorage, fileName, 100)
ig = bp.IG()
if ig == "" {
log.Println("Unable to get IG, retrying")
continue
}
iid = bp.IID()
if iid == "" {
log.Println("Unable to get IID, retrying")
continue
}
hasErr = false
break
}
if hasErr {
panic("Failed after 10 retries")
}
for i := 0; i < 10; i++ {
bip := NewBingInfoPage(iid, ig)
title := bip.Title()
location := bip.Location()
_, _, article := bip.Article()
fmt.Println(title)
fmt.Println(location)
fmt.Println(article)
if config.TextDrawerEnabled {
wallpaper.SetTextDrawer(&WordWrappingTextDrawer{
Config: config.TextDrawerConfig,
Text: title + ", " + location + "\n" + article,
})
}
wallpaper.SaveToFile(fsStorage, "wp_out.jpg", 100)
setWallPaper(wallpaperDir)
config.LastUpdate = time.Now().Unix()
config.Save(configFile)
log.Println("Done")
break
}
os.Exit(0)
}
func setWallPaper(dir string) {
absWallpaperPath, err := filepath.Abs(dir + "/wp_out.jpg")
if err != nil {
panic(err)
}
setWindowsWallPaper(absWallpaperPath)
}