-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinggo.go
188 lines (169 loc) · 3.67 KB
/
binggo.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
"strings"
)
const (
displayAll = 0
)
// Args cli arguments definition
type Args struct {
pictDir string
display int
}
func main() {
os.Exit(run())
}
func run() int {
args, err := parseArgs()
if err != nil {
log.Print(err)
fmt.Printf("usage: binggo --pictdir /path/to/downloads\n")
fmt.Printf("usage: binggo --pictdir /path/to/downloads --display 1\n")
fmt.Printf("usage: --display option is available only on macOS\n")
return 1
}
urls, err := getPictureUrls()
if err != nil {
log.Print(err)
return 2
}
pictDirName := args.pictDir
for _, url := range urls {
err := downloadPicture(url, pictDirName)
if err != nil {
log.Print(err)
return 3
}
}
files, err := getWallpaperFile(pictDirName)
if err != nil {
log.Print(err)
return 4
}
sort.Sort(files)
err = changeWallpaper(args.display, pictDirName, files[0])
if err != nil {
log.Print(err)
return 5
}
return 0
}
func parseArgs() (*Args, error) {
var pictDir string
flag.StringVar(&pictDir, "pictdir", "", "directory path for download")
var display int
flag.IntVar(&display, "display", displayAll, "target display number.")
flag.Parse()
if pictDir == "" {
return nil, errors.New("pictdir is empty")
}
if pictDir[:2] == "~/" {
usr, err := user.Current()
if err != nil {
return nil, err
}
pictDir = strings.Replace(pictDir, "~/", usr.HomeDir+"/", 1)
}
pictDir, err := validatePictDir(pictDir)
if err != nil {
return nil, err
}
args := &Args{
pictDir: pictDir,
display: display,
}
return args, err
}
// validate pict directory path
func validatePictDir(path string) (string, error) {
pictDirPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
f, err := os.Stat(pictDirPath)
if err != nil {
return "", fmt.Errorf("[%s] is not exists.\n%v", pictDirPath, err)
}
if !f.IsDir() {
return "", fmt.Errorf("[%s] is not a directory", pictDirPath)
}
return pictDirPath, nil
}
// get all urls of picture from bing top page
func getPictureUrls() ([]string, error) {
resp, err := http.Get("http://www.bing.com")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
s := string(body)
regex1 := regexp.MustCompile(`url:'\\([^']*)'`)
results := regex1.FindAllStringSubmatch(s, -1)
var urls []string
for _, r := range results {
if len(r) == 2 {
p := strings.Replace(r[1], "\\", "", -1)
urls = append(urls, fmt.Sprintf("http://bing.com%s", p))
}
}
return urls, nil
}
// download bing picture
func downloadPicture(url string, dirName string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
splited := strings.Split(url, "/")
filename := splited[len(splited)-1]
out, err := os.Create(dirName + string(os.PathSeparator) + filename)
if err != nil {
return err
}
defer out.Close()
out.Write(body)
return nil
}
// PictFiles are wallpaper pictures
type PictFiles []os.FileInfo
// Len for sort.Interface
func (files PictFiles) Len() int {
return len(files)
}
// Swap for sort.Interface
func (files PictFiles) Swap(i, j int) {
files[i], files[j] = files[j], files[i]
}
// Less for sort.Interface
func (files PictFiles) Less(i, j int) bool {
return files[i].ModTime().UnixNano() > files[j].ModTime().UnixNano()
}
// getWallpaperFiles collects wallpaper picture file
func getWallpaperFile(pictDir string) (PictFiles, error) {
var files PictFiles
files, err := ioutil.ReadDir(pictDir)
if err != nil {
return nil, err
}
return files, nil
}