-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (88 loc) · 2.18 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path"
"sync"
"github.com/duke-git/lancet/v2/netutil"
"github.com/o98k-ok/lazy/v2/alfred"
)
var (
HOST = "bing.com"
)
type BingImg struct {
Startdate string `json:"startdate"`
Fullstartdate string `json:"fullstartdate"`
Enddate string `json:"enddate"`
URL string `json:"url"`
Urlbase string `json:"urlbase"`
Copyright string `json:"copyright"`
Copyrightlink string `json:"copyrightlink"`
Title string `json:"title"`
Quiz string `json:"quiz"`
Wp bool `json:"wp"`
Hsh string `json:"hsh"`
Drk int `json:"drk"`
Top int `json:"top"`
Bot int `json:"bot"`
Hs []any `json:"hs"`
}
type BingResponse struct {
Images []BingImg `json:"images"`
}
func main() {
var dir string
var count int
flag.StringVar(&dir, "dir", "", "wallpaper dir")
flag.IntVar(&count, "count", 9, "wallpaper count")
flag.Parse()
if len(dir) == 0 {
pwd, _ := os.Getwd()
dir = path.Join(pwd, "wallpaper")
}
{
os.MkdirAll(dir, 0755)
}
{
url := fmt.Sprintf("https://%s/HPImageArchive.aspx?format=js&idx=0&n=%d&mkt=en-US", HOST, count)
resp, err := netutil.HttpGet(url)
if err != nil {
alfred.Log("get %s err %v", url, err)
return
}
var bing BingResponse
if err = json.NewDecoder(resp.Body).Decode(&bing); err != nil {
alfred.Log("decode %s response err %v", url, err)
return
}
items := alfred.NewItems()
items.Items = make([]*alfred.Item, len(bing.Images))
groups := sync.WaitGroup{}
for i, v := range bing.Images {
groups.Add(1)
go func(idx int, val BingImg) {
defer groups.Done()
filename := path.Join(dir, fmt.Sprintf("%s_%s.jpg", val.Startdate, val.Hsh))
item := alfred.NewItem(val.Title, "", filename)
item.Icon = &alfred.Icon{
Path: filename,
}
items.Items[idx] = item
{
_, err := os.Stat(filename)
if err == nil {
return
}
}
err = netutil.DownloadFile(filename, fmt.Sprintf("https://%s/%s", HOST, val.URL))
if err != nil {
alfred.Log("download %s err %v", val.URL, err)
}
}(i, v)
}
groups.Wait()
items.Show()
}
}