-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (116 loc) · 2.99 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
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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"regexp"
"strconv"
"sync"
"time"
)
var (
//<a.....path="images/201807/thumb_img/2223_thumb_G_1531956513301.jpg" title="11111">
ReImg = `<img[\s\S]+?src="(images[\s\S]+?)"`
ReImgName = `<a.+?path="(.+?)">`
ReTitle = `title="(.+)`
chSem = make(chan int,5)
downloadWG sync.WaitGroup
randomMT sync.Mutex
chImgMaps chan map[string]string
)
func GetPageImgurls(url string) []string {
//i := "1"
//url := "https://www.yuebing.com/category-0-b0-min0-max0-attr0-" + i + "-sort_order-ASC.html"
html := GetHtml(url)
re := regexp.MustCompile(ReImgName)
rets := re.FindAllStringSubmatch(html, -1)
imgUrls := make([]string, 0)
for _, ret := range rets {
imgUrl := "https://www.yuebing.com/"+ret[1]
imgUrls = append(imgUrls, imgUrl)
}
return imgUrls
}
func main() {
for i:=1;i<=15;i++{
j := strconv.Itoa(i)
url := "https://www.yuebing.com/category-0-b0-min0-max0-attr0-" + j + "-sort_order-ASC.html"
imginfos := GetPageImginfos(url)
for _,imgInfoMap := range imginfos{
DownloadImgAsync(imgInfoMap["url"],imgInfoMap["filename"],&downloadWG)
time.Sleep(500 * time.Millisecond)
}
}
downloadWG.Wait()
}
func DownloadImg(url string,file_name string) {
fmt.Println("downloading")
resp, _ := http.Get(url)
defer resp.Body.Close()
filename := `路径` + file_name + ".jpg"
imgBytes, _ := ioutil.ReadAll(resp.Body)
err := ioutil.WriteFile(filename, imgBytes, 0644)
if err == nil{
fmt.Println(filename+"下载成功!")
}else{
fmt.Println(filename+"下载失败!")
}
}
func DownloadImgAsync(url ,filename string,wg *sync.WaitGroup) {
wg.Add(1)
go func() {
chSem <- 1
DownloadImg(url,filename)
<-chSem
downloadWG.Done()
}()
}
func GetHtml(url string) string {
resp, _ := http.Get(url)
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
html := string(bytes)
return html
}
func GetRandomInt(start,end int) int {
randomMT.Lock()
<- time.After(1 * time.Nanosecond)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ret := start + r.Intn(end - start)
randomMT.Unlock()
return ret
}
/*
生成时间戳_随机数的文件名
*/
func GetRandomName() string {
timestamp := strconv.Itoa(int(time.Now().UnixNano()))
randomNum := strconv.Itoa(GetRandomInt(100, 10000))
return timestamp + "-" + randomNum
}
func GetImgNameTag(imgTag string) string {
re := regexp.MustCompile(ReTitle)
rets := re.FindAllStringSubmatch(imgTag, -1)
//fmt.Println(rets)
if len(rets) > 0{
return rets[0][1]
}else {
return GetRandomName()
}
}
func GetPageImginfos(url string) []map[string] string {
html := GetHtml(url)
re := regexp.MustCompile(ReImgName)
rets := re.FindAllStringSubmatch(html, -1)
imgInfos := make([]map[string] string,0)
for _,ret := range rets {
imgInfo := make(map[string] string)
imgUrl := "https://www.yuebing.com/"+ret[1]
imgInfo["url"] = imgUrl[0:78]
imgInfo["filename"]=GetImgNameTag(ret[1])
//fmt.Println(imgInfo["filename"])
imgInfos = append(imgInfos, imgInfo)
}
return imgInfos
}