Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2f8c49

Browse files
committed
支持抓取 go 官方下载包信息
1 parent 760686f commit e2f8c49

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/http/controller/download.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type DownloadController struct{}
2727
func (self DownloadController) RegisterRoute(g *echo.Group) {
2828
g.Get("/dl", self.GoDl)
2929
g.Get("/dl/golang/:filename", self.FetchGoInstallPackage)
30+
g.Get("/dl/add_new_version", self.AddNewDownload)
3031
}
3132

3233
// GoDl Go 语言安装包下载
@@ -117,6 +118,26 @@ func (self DownloadController) FetchGoInstallPackage(ctx echo.Context) error {
117118
return ctx.Redirect(http.StatusSeeOther, "/static/"+filePath)
118119
}
119120

121+
func (DownloadController) AddNewDownload(ctx echo.Context) error {
122+
version := ctx.QueryParam("version")
123+
selector := ctx.QueryParam("selector")
124+
125+
if version == "" {
126+
return fail(ctx, 1, "version is empty")
127+
}
128+
129+
if selector == "" {
130+
selector = "toggleVisible"
131+
}
132+
133+
err := logic.DefaultDownload.AddNewDownload(ctx, version, selector)
134+
if err != nil {
135+
return fail(ctx, 1, err.Error())
136+
}
137+
138+
return success(ctx, nil)
139+
}
140+
120141
func (DownloadController) headWithTimeout(dlUrl string) (*http.Response, error) {
121142
client := http.Client{
122143
Timeout: 2 * time.Second,

src/logic/download.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
package logic
88

99
import (
10+
"net/http"
11+
"strings"
12+
1013
"model"
1114

1215
"golang.org/x/net/context"
1316

1417
. "db"
1518

19+
"github.com/PuerkitoBio/goquery"
20+
"github.com/polaris1119/goutils"
1621
"github.com/polaris1119/logger"
1722
)
1823

@@ -35,3 +40,71 @@ func (DownloadLogic) RecordDLTimes(ctx context.Context, filename string) error {
3540

3641
return nil
3742
}
43+
44+
func (DownloadLogic) AddNewDownload(ctx context.Context, version, selector string) error {
45+
objLog := GetLogger(ctx)
46+
47+
resp, err := http.Get("https://golang.google.cn/dl/")
48+
if err != nil {
49+
return err
50+
}
51+
defer resp.Body.Close()
52+
53+
doc, err := goquery.NewDocumentFromResponse(resp)
54+
if err != nil {
55+
return err
56+
}
57+
58+
doc.Find(selector).Each(func(i int, versionSel *goquery.Selection) {
59+
idVal, exists := versionSel.Attr("id")
60+
if !exists {
61+
return
62+
}
63+
64+
if idVal != version {
65+
return
66+
}
67+
68+
versionSel.Find("table tbody tr").Each(func(j int, dlSel *goquery.Selection) {
69+
download := &model.Download{
70+
Version: version,
71+
}
72+
73+
if dlSel.HasClass("highlight") {
74+
download.IsRecommend = true
75+
}
76+
77+
dlSel.Find("td").Each(func(k int, fieldSel *goquery.Selection) {
78+
val := fieldSel.Text()
79+
switch k {
80+
case 0:
81+
download.Filename = val
82+
case 1:
83+
download.Kind = val
84+
case 2:
85+
download.OS = val
86+
case 3:
87+
download.Arch = val
88+
case 4:
89+
download.Size = goutils.MustInt(strings.TrimRight(val, "MB"))
90+
case 5:
91+
download.Checksum = val
92+
}
93+
})
94+
95+
has, err := MasterDB.Where("filename=?", download.Filename).Exist(new(model.Download))
96+
if err != nil || has {
97+
return
98+
}
99+
100+
_, err = MasterDB.Insert(download)
101+
if err != nil {
102+
objLog.Errorln("insert download error:", err, "version:", version)
103+
}
104+
})
105+
106+
MasterDB.Exec("UPDATE download SET seq=id WHERE seq=0")
107+
})
108+
109+
return nil
110+
}

0 commit comments

Comments
 (0)