package main
import (
“bufio”
“bytes”
“fmt”
_ “github.com/joho/godotenv/autoload”
“io”
“net/http”
“net/url”
“os”
“path”
“strings”
“study/config/db”
“sync”
“time”
)
type Pinyin struct {
Id uint `gorm:”column:id” json:”id”`
Zw string `gorm:”column:zw” json:”zw”`
Py string `gorm:”column:py” json:”py”`
Url string `gorm:”column:url” json:”url”`
}
func Get(url string) string {
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var buffer [512]byte
result := bytes.NewBuffer(nil)
for {
n, err := resp.Body.Read(buffer[0:])
result.Write(buffer[0:n])
if err != nil && err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
return result.String()
}
func getPic(setUrl string) {
imgPath := `d:\pic\`
imgUrl := setUrl
fileName := path.Base(imgUrl)
res, err := http.Get(imgUrl)
if err != nil {
fmt.Println(“A error occurred!”)
return
}
defer res.Body.Close()
reader := bufio.NewReaderSize(res.Body, 32*1024)
file, err := os.Create(imgPath + fileName)
if err != nil {
panic(err)
}
writer := bufio.NewWriter(file)
_, _ = io.Copy(writer, reader)
}
func downloadPic() {
var wd []Pinyin
Db := db.DB
Db.Find(&wd)
mytest := make([]string, 0)
i:=0
if len(wd) > 0 {
for _, val := range wd {
if len(val.Url) == 0 {
continue
}
if strings.ToLower(path.Ext(val.Url)) == “.gif” {
mytest = append(mytest, strings.Trim(val.Url,” “))
i++
}
}
}
goTd(mytest)
}
func getCallUrl(urlStr string) string {
escapeUrl := url.QueryEscape(urlStr)
return fmt.Sprintf(“url?word=%v”, escapeUrl)
}
func getUrl(word string) string {
urlStr := getCallUrl(word)
data := Get(urlStr)
if len(data) > 0 {
s := strings.Split(data, “?t=”)
return s[0]
} else {
return “”
}
}
func updateUrl(wd Pinyin, urlStr string) {
Db := db.DB
wd.Url = urlStr
Db.Save(&wd)
}
func getWord() {
var wd []Pinyin
Db := db.DB
Db.Where(“id >= ?”, 5978).Find(&wd)
fmt.Println(len(wd))
if len(wd) > 0 {
for _, val := range wd {
if len(val.Url) > 0 {
continue
}
str := getUrl(val.Zw)
if len(str) > 0 {
updateUrl(val, str)
}
}
}
}
type td struct {
id int
name string
}
func testGO(urls []string, wg *sync.WaitGroup) {
for _, val := range urls {
getPic(val)
}
wg.Done()
}
func goTdTest(test []string) {
splitNumber := 5
length := len(test)
k := length / splitNumber
if length%splitNumber != 0 {
k += 1
}
var wg sync.WaitGroup
wg.Add(splitNumber)
m, n, last := 0, 0, k
for i := 0; i < splitNumber; i++ {
if i+1 == splitNumber {
go testGO(test[m:], &wg)
} else {
go testGO(test[m:last], &wg)
}
n = i + 1
m = n * k
last = m + k
}
wg.Wait()
}
func goTd(test []string) {
goTdTest(test)
}
func main() {
downloadPic()
getWord()
}