-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (80 loc) · 2.27 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"search/spiders"
"strings"
)
type MyDownloader interface {
Run(anHao string) ([]int64, string)
GetOnePage(anHao string, page int, getTotalItem bool, putChan chan int64)
DoCookie(req *http.Request)
Get(url string, toDoCookie bool) (result string)
}
func runSpider(downloader MyDownloader, anHao string) ([]int64, string) {
pidArray, errMsg := downloader.Run(anHao)
return pidArray, errMsg
}
func run(key string, anHao string) ([]int64, string) {
var pidArray []int64
var errMsg string
if key == "jd" {
downloader := new(spiders.JdDownloader)
downloader.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
downloader.Referer = "https://auction.jd.com/sifa_list.html"
pidArray, errMsg = runSpider(downloader, anHao)
} else if key == "tb" {
downloader := new(spiders.TbDownloader)
downloader.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
downloader.Referer = "https://sf.taobao.com/item_list.htm"
pidArray, errMsg = runSpider(downloader, anHao)
} else {
fmt.Println("请传入正确的key")
}
return pidArray, errMsg
}
func prepareResponse(c *gin.Context) {
key := c.Param("key")
anHao := c.DefaultQuery("anhao", "")
if anHao != "" && (key == "jd" || key == "tb") {
pidArray, errMsg := run(key, anHao)
var status int = 0
if strings.Contains(errMsg, "未返回数据") {
status = 20010
}
c.JSON(http.StatusOK, gin.H{
"status": status,
"result": pidArray,
"msg": errMsg,
})
} else if anHao == "" {
c.JSON(http.StatusOK, gin.H{
"status": 10011,
"result": []int64{},
"msg": "请传入案号",
})
} else if key == "jd" || key == "tb" {
c.JSON(http.StatusOK, gin.H{
"status": 10011,
"result": []int64{},
"msg": "请选择平台,支持jd和tb",
})
} else {
c.JSON(http.StatusOK, gin.H{
"status": 40010,
"result": []int64{},
"msg": "未知错误",
})
}
}
func main() {
// anHao := "兴执字第"
// anHao := "连执行字"
// pidArray := getJdPids(anHao)
// pidArray := run("tb", anHao)
// fmt.Println(pidArray)
r := gin.Default()
r.GET("/getAnhao/:key/", prepareResponse)
r.Run("0.0.0.0:5090")
}