2024-05-15 11:15:15 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-resty/resty/v2"
|
2024-06-24 08:40:24 +08:00
|
|
|
"go-wechat/config"
|
2024-07-05 09:32:39 +08:00
|
|
|
"go-wechat/model/dto"
|
2024-05-15 11:15:15 +08:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// News
|
|
|
|
// @description: 新闻
|
|
|
|
type News interface {
|
2024-07-15 14:14:24 +08:00
|
|
|
MorningPost() []string // 早报
|
|
|
|
GetHotTop() []dto.HotTopDataItem // 获取热搜排行榜
|
2024-05-15 11:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type news struct{}
|
|
|
|
|
|
|
|
// NewsUtil
|
|
|
|
// @description: 新闻工具
|
|
|
|
// @param account
|
|
|
|
// @param password
|
|
|
|
// @return LeiGod
|
|
|
|
func NewsUtil() News {
|
|
|
|
return &news{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MorningPost
|
|
|
|
// @description: 早报
|
|
|
|
// @receiver news
|
|
|
|
// @return records
|
|
|
|
func (news) MorningPost() (records []string) {
|
2024-07-05 09:32:39 +08:00
|
|
|
var newsResp dto.MorningPost
|
2024-05-15 11:15:15 +08:00
|
|
|
|
|
|
|
res := resty.New()
|
|
|
|
resp, err := res.R().
|
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
2024-06-24 08:40:24 +08:00
|
|
|
SetQueryParam("token", config.Conf.System.AlApiToken).
|
2024-05-15 11:15:15 +08:00
|
|
|
SetResult(&newsResp).
|
|
|
|
Post("https://v2.alapi.cn/api/zaobao")
|
|
|
|
if err != nil {
|
2024-07-15 14:14:24 +08:00
|
|
|
log.Printf("每日早报获取失败: %s", err.Error())
|
|
|
|
return
|
2024-05-15 11:15:15 +08:00
|
|
|
}
|
|
|
|
log.Printf("每日早报获取结果: %s", unicodeToText(resp.String()))
|
|
|
|
|
|
|
|
records = newsResp.Data.News
|
|
|
|
return
|
|
|
|
}
|
2024-07-15 14:14:24 +08:00
|
|
|
|
|
|
|
// GetHotTop
|
|
|
|
// @description: 获取热搜排行榜
|
|
|
|
// @receiver news
|
|
|
|
// @return records
|
|
|
|
func (news) GetHotTop() (records []dto.HotTopDataItem) {
|
|
|
|
var respData dto.HotTop
|
|
|
|
res := resty.New()
|
|
|
|
resp, err := res.R().
|
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetResult(&respData).
|
|
|
|
Get("https://api.vvhan.com/api/hotlist/baiduRD")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("百度热榜获取失败: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("百度热榜获取结果: %s", unicodeToText(resp.String()))
|
|
|
|
if !respData.Success {
|
|
|
|
log.Println("百度热榜获取失败")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return respData.Data
|
|
|
|
}
|