MCPcopy
hub / github.com/kingwrcy/moments / GetDoubanMovieInfo

Method GetDoubanMovieInfo

backend/handler/memo.go:580–685  ·  view source on GitHub ↗

GetDoubanMovieInfo godoc @Tags Memo @Summary 获取豆瓣电影详情 @Accept json @Produce json @Param id query int true "豆瓣电影ID" @Param x-api-token header string true "登录TOKEN" @Success 200 {object} vo.DoubanMovie @Router /api/mem

(c echo.Context)

Source from the content-addressed store, hash-verified

578// @Success 200 {object} vo.DoubanMovie
579// @Router /api/memo/getDoubanMovieInfo [post]
580func (m MemoHandler) GetDoubanMovieInfo(c echo.Context) error {
581
582 var (
583 book vo.DoubanMovie
584 sysConfigVo vo.FullSysConfigVO
585 sysConfig db.SysConfig
586 userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
587 )
588
589 if err := m.base.db.First(&sysConfig).Error; errors.Is(err, gorm.ErrRecordNotFound) {
590 return FailRespWithMsg(c, Fail, "系统配置为空")
591 }
592 err := json.Unmarshal([]byte(sysConfig.Content), &sysConfigVo)
593 if err != nil {
594 return FailRespWithMsg(c, Fail, "读取系统配置异常")
595 }
596
597 id := c.QueryParam("id")
598 target := fmt.Sprintf("https://movie.douban.com/subject/%s/", id)
599
600 req, _ := http.NewRequest("GET", target, nil)
601 req.Header.Set("User-Agent", userAgent)
602 start := time.Now()
603 res, err := m.hc.Do(req)
604 m.base.log.Info().Str("豆瓣读书ID", id).Str("URL", target).Str("耗时", fmt.Sprintf("%f秒", time.Since(start).Seconds())).Msgf("获取豆瓣读书")
605 if err != nil {
606 m.base.log.Error().Msgf("获取豆瓣电影异常:%s", err.Error())
607 return FailRespWithMsg(c, Fail, err.Error())
608 }
609 defer res.Body.Close()
610 if res.StatusCode != 200 {
611 m.base.log.Error().Msgf("豆瓣电影API返回码不是200,而是:%d", res.StatusCode)
612 return FailRespWithMsg(c, Fail, fmt.Sprintf("豆瓣读书API返回码不是200,而是:%d,URL:%s", res.StatusCode, target))
613 }
614
615 // Load the HTML document
616 doc, err := goquery.NewDocumentFromReader(res.Body)
617 if err != nil {
618 m.base.log.Error().Msgf("初始化html错误,%s", err.Error())
619 return FailRespWithMsg(c, Fail, fmt.Sprintf("初始化html错误,%s", err.Error()))
620 }
621
622 doc.Find("meta[property]").Each(func(i int, s *goquery.Selection) {
623 if properties, exists := s.Attr("property"); exists {
624 value := s.AttrOr("content", "")
625 if properties == "og:title" {
626 book.Title = value
627 } else if properties == "og:description" {
628 book.Desc = value
629 } else if properties == "og:image" {
630 book.Image = value
631 } else if properties == "video:director" {
632 book.Director = value
633 } else if properties == "video:actor" {
634 book.Actors = value + "/"
635 }
636 }
637 })

Callers

nothing calls this directly

Calls 5

FailRespWithMsgFunction · 0.85
downloadImageFunction · 0.85
SuccessRespFunction · 0.85
InfoMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected