-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing.go
80 lines (76 loc) · 1.67 KB
/
bing.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
package main
import (
"github.com/asticode/go-astilectron"
"github.com/asticode/go-astilectron-bootstrap"
"encoding/json"
"time"
"github.com/tidwall/gjson"
"strings"
"net/http"
"io/ioutil"
)
type BingUI struct {
Img string `json:"img"`
Idx string `json:"idx"`
Ti string `json:"ti"`
}
func bingHandleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "bing":
// Unmarshal payload
var path string
if len(m.Payload) > 0 {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &path); err != nil {
payload = err.Error()
return
}
}
// Explore
if payload, err = bingExplore(path); err != nil {
payload = err.Error()
return
}
case "Save":
}
return
}
func bingExplore(path string) (e BingUI, err error) {
//astilog.Info(path)
a,b:=Next(path)
e.Ti=a
e.Img=b
e.Idx=path
return
}
func Next(idx string)(string,string){
var htt="http://cn.bing.com"
var url="http://cn.bing.com/HPImageArchive.aspx?format=js&idx=" + idx + "&n=1&nc=" + string(time.Now().Unix()) + "&video=1";
json:=httpGet(url)
img := gjson.Get(json, "images.#.url")
title := gjson.Get(json, "images.#.copyright")
var title2 string
var img1 string
for _, name := range title.Array() {
title2=name.String()
}
for _, name := range img.Array() {
img1=name.String()
}
//截取字符串
str1:=strings.Index(title2, "©")
var s= string([]byte(title2)[:str1-1])
return s,htt+img1
}
func httpGet(url string) string{
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
return string(body)
}