-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing.go
95 lines (83 loc) · 2.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"html"
"log"
"regexp"
)
const (
BaseURL = "https://cn.bing.com"
InfoURL = "https://cn.bing.com/cnhp/life?IID=%s&IG=%s"
)
type BingPage struct {
src string
}
func NewBingPage() *BingPage {
return &BingPage{getPage(BaseURL)}
}
func (b BingPage) IID() string {
re := regexp.MustCompile("data-ajaxiid=\"(\\d+?)\"")
found := re.FindStringSubmatch(b.src)
if len(found) < 1 {
return ""
}
iid := "SERP." + found[1]
log.Println("IID:" + iid)
return iid
}
func (b BingPage) IG() string {
re := regexp.MustCompile("IG:\"(\\w+?\\d+?)\"")
found := re.FindStringSubmatch(b.src)
if len(found) < 1 {
return ""
}
log.Println("IG:" + found[1])
return found[1]
}
func (b BingPage) ImageURL() string {
re := regexp.MustCompile("g_img=\\{url:\\s\"(.+\\.jpg)\"")
m := re.FindStringSubmatch(b.src)
if len(m) < 1 {
return ""
}
imgurl := BaseURL + m[1]
log.Println(imgurl)
return imgurl
}
type BingInfoPage struct {
src string
}
func NewBingInfoPage(iid, ig string) *BingInfoPage {
return &BingInfoPage{getPage(fmt.Sprintf(InfoURL, iid, ig))}
}
func (b BingInfoPage) Title() string {
re := regexp.MustCompile("<div class=\"hplaTtl\">(.+?)</div>")
found := re.FindStringSubmatch(b.src)
if len(found) < 1 {
return ""
}
title := html.UnescapeString(found[1])
return title
}
func (b BingInfoPage) Location() string {
re := regexp.MustCompile("<span class=\"hplaAttr\">(.+?)</span>")
found := re.FindStringSubmatch(b.src)
if len(found) < 1 {
return ""
}
location := html.UnescapeString(found[1])
return location
}
func (b BingInfoPage) Article() (title string, subtitle string, body string) {
re := regexp.MustCompile("<div class=\"hplatt\">(.+?)</div>")
m := re.FindStringSubmatch(b.src)
if len(m) < 1 {
return
}
title = html.UnescapeString(m[1])
re = regexp.MustCompile("<div class=\"hplats\">(.+?)</div>")
subtitle = html.UnescapeString(re.FindStringSubmatch(b.src)[1])
re = regexp.MustCompile("<div id=\"hplaSnippet\">(.+?)</div>")
body = html.UnescapeString(re.FindStringSubmatch(b.src)[1])
return
}