package controllers
|
|
import (
|
"fmt"
|
"github.com/gin-gonic/gin"
|
"github.com/gocolly/colly"
|
"log"
|
"reptile/code"
|
"reptile/util"
|
)
|
|
type ReptileControllers struct{}
|
|
type FieldSelectorInfo struct {
|
Name string `json:"name"`
|
Selector string `json:"selector"`
|
}
|
|
type FieldResult struct {
|
Name string
|
Value string
|
}
|
|
type ReptileInfo struct {
|
Url string `json:"url"`
|
UserName string `json:"userName"`
|
UserPassWD string `json:"userPassWD"`
|
TitleSelectorStr string `json:"titleSelectorStr"`
|
ListSelectorStr string `json:"listSelectorStr"`
|
ListRequest bool `json:"listRequest"`
|
FspArray []FieldSelectorInfo `json:"fspArray"`
|
}
|
|
func (rc *ReptileControllers) Reptile(c *gin.Context) {
|
var rpif ReptileInfo
|
c.BindJSON(&rpif)
|
url := rpif.Url
|
titleSelectorStr := rpif.TitleSelectorStr
|
listSelectorStr := rpif.ListSelectorStr
|
fspArray := rpif.FspArray
|
listRequest := rpif.ListRequest
|
if listRequest == true {
|
if url != "" && titleSelectorStr != "" && listSelectorStr != "" && fspArray != nil && len(fspArray) > 0 {
|
res := ReptileList(url, titleSelectorStr, listSelectorStr, fspArray)
|
util.ResponseFormat(c, code.Success, res)
|
return
|
} else {
|
util.ResponseFormat(c, code.RequestParamError, "请求参数有误")
|
return
|
}
|
} else {
|
if url != "" && titleSelectorStr != "" && fspArray != nil && len(fspArray) > 0 {
|
res := ReptileNoList(url, titleSelectorStr, fspArray)
|
util.ResponseFormat(c, code.Success, res)
|
return
|
} else {
|
util.ResponseFormat(c, code.RequestParamError, "请求参数有误")
|
return
|
}
|
}
|
return
|
}
|
|
func ReptileList(url string, titleSelectorStr string, listSelectorStr string, fspArray []FieldSelectorInfo) interface{} {
|
cy := colly.NewCollector()
|
rfr := make([]interface{}, 0)
|
cy.OnHTML(titleSelectorStr, func(e *colly.HTMLElement) {
|
fmt.Println("+++++++++++++++++++++++++++")
|
e.ForEach(listSelectorStr, func(i int, ele *colly.HTMLElement) {
|
fr := []FieldResult{}
|
for _, v := range fspArray {
|
fr = append(fr, FieldResult{
|
Name: v.Name,
|
Value: ele.ChildText(v.Selector),
|
})
|
}
|
rfr = append(rfr, fr)
|
})
|
})
|
err := cy.Request("GET", url, nil, nil, nil)
|
if err != nil {
|
log.Fatal("connect error: ", err)
|
}
|
return rfr
|
}
|
|
func ReptileNoList(url string, titleSelectorStr string, fspArray []FieldSelectorInfo) []interface{} {
|
c := colly.NewCollector()
|
rfr := make([]interface{}, 0)
|
titleSelectorStr = "body"
|
c.OnHTML(titleSelectorStr, func(e *colly.HTMLElement) {
|
fmt.Println("+++++++++++++++++++++++++++")
|
fr := []FieldResult{}
|
for _, v := range fspArray {
|
fmt.Println("v.Selector: ", v.Selector)
|
fr = append(fr, FieldResult{
|
Name: v.Name,
|
Value: e.DOM.Find(v.Selector).Eq(0).Text(),
|
})
|
}
|
rfr = append(rfr, fr)
|
})
|
err := c.Request("GET", url, nil, nil, nil)
|
if err != nil {
|
log.Fatal("connect error: ", err)
|
}
|
return rfr
|
}
|