us-coach-spider/server/spider.go

116 lines
2.8 KiB
Go
Raw Normal View History

package server
import (
2024-09-13 22:47:19 +08:00
"fmt"
"net/url"
2024-06-15 21:10:37 +08:00
"strconv"
2024-12-03 09:24:20 +08:00
"strings"
2024-09-13 22:47:19 +08:00
"gitea.timerzz.com/kedaya_haitao/coach-spider/spider"
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/gofiber/fiber/v3"
)
type SpiderSvc struct {
2024-09-13 22:47:19 +08:00
ctl *spider.Controller
providerId v2.ProviderId
}
2024-09-13 22:47:19 +08:00
func NewSpiderSvc(ctl *spider.Controller, providerId v2.ProviderId) *SpiderSvc {
return &SpiderSvc{
2024-09-13 22:47:19 +08:00
ctl: ctl,
providerId: providerId,
}
}
2024-09-13 22:47:19 +08:00
func (s *SpiderSvc) Registry(r fiber.Router) {
2024-11-30 20:26:37 +08:00
api := r.Group(fmt.Sprintf("/api/v2/provider/%s", s.providerId))
api.Post("pull", s.Pull)
api.Post("price/fetch/:id", s.FetchArticlePrice)
api.Post("detail/fetch/:pid", s.FetchArticleDetail)
api.Post("ats/fetch/:pid", s.FetchArticleAts)
api.Get("ats/:pid", s.GetArticleAts)
}
2024-06-15 21:10:37 +08:00
2024-09-13 22:47:19 +08:00
func (s *SpiderSvc) Pull(ctx fiber.Ctx) error {
// 接收到,就爬取数据
go s.ctl.Crawl()
return ctx.JSON(web.NewResponse("ok", "ok"))
2024-06-15 21:10:37 +08:00
}
2024-09-13 22:47:19 +08:00
func (s *SpiderSvc) FetchArticlePrice(ctx fiber.Ctx) error {
i := ctx.Params("id")
id, _ := strconv.Atoi(i)
if id == 0 {
return fmt.Errorf("id is empty")
2024-06-15 21:10:37 +08:00
}
2024-09-13 22:47:19 +08:00
if err := s.ctl.FetchArticlePrice(ctx.Context(), uint(id)); err != nil {
2024-06-15 21:10:37 +08:00
return err
}
2024-09-13 22:47:19 +08:00
return ctx.JSON(web.NewResponse("ok"))
2024-06-15 21:10:37 +08:00
}
2024-11-21 14:36:31 +08:00
func (s *SpiderSvc) FetchArticleDetail(ctx fiber.Ctx) error {
pid, err := url.QueryUnescape(ctx.Params("pid"))
if err != nil {
return err
2024-11-21 14:36:31 +08:00
}
2024-12-03 09:24:20 +08:00
pid = strings.ReplaceAll(pid, " ", "-")
pid = strings.ReplaceAll(pid, "+", "-")
if err = s.ctl.FetchArticleDetail(ctx.Context(), pid); err != nil {
2024-11-21 14:36:31 +08:00
return err
}
return ctx.JSON(web.NewResponse("ok"))
}
func (s *SpiderSvc) FetchArticleAts(ctx fiber.Ctx) error {
pid, err := url.QueryUnescape(ctx.Params("pid"))
if err != nil {
return err
2024-11-21 14:36:31 +08:00
}
2024-12-03 09:24:20 +08:00
pid = strings.ReplaceAll(pid, " ", "-")
pid = strings.ReplaceAll(pid, "+", "-")
if err = s.ctl.FetchArticleAts(ctx.Context(), pid); err != nil {
2024-11-21 14:36:31 +08:00
return err
}
return ctx.JSON(web.NewResponse("ok"))
}
2024-11-21 17:21:17 +08:00
func (s *SpiderSvc) GetArticleAts(ctx fiber.Ctx) error {
pid, err := url.QueryUnescape(ctx.Params("pid"))
if err != nil {
return err
2024-11-21 17:21:17 +08:00
}
2024-12-03 09:24:20 +08:00
pid = strings.ReplaceAll(pid, " ", "-")
pid = strings.ReplaceAll(pid, "+", "-")
2024-11-21 17:21:17 +08:00
ats, err := s.ctl.GetArticleAts(ctx.Context(), pid)
if err != nil {
return err
}
return ctx.JSON(web.NewResponse(ats))
}
2024-09-13 22:47:19 +08:00
//func (s *SpiderSvc) DelCalculate(ctx fiber.Ctx) error {
// idStr := ctx.Params("id")
// if idStr == "" {
// return fiber.ErrBadRequest
// }
// id, _ := strconv.Atoi(idStr)
// if err := s.ctl.DeleteCalculateProcess(uint(id)); err != nil {
// return err
// }
// return nil
//}
//
//func (s *SpiderSvc) UpsertCalculate(ctx fiber.Ctx) error {
// var cs []productv1.CalculateProcess
// if err := ctx.Bind().JSON(&cs); err != nil {
// return err
// }
// if err := s.ctl.SaveCalculateProcess(cs); err != nil {
// return err
// }
// return nil
//}