mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-02-12 21:14:41 +08:00
optimize
This commit is contained in:
@@ -46,10 +46,15 @@ const (
|
||||
SpiderCipher = "Life in a different world from zero"
|
||||
)
|
||||
|
||||
/*API相关redis key*/
|
||||
const (
|
||||
IndexCacheKey = "IndexCache"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
// SearchTableName 存放检索信息的数据表名
|
||||
SearchTableName = "search_mt"
|
||||
SearchTableName = "search"
|
||||
|
||||
//mysql服务配置信息 root:root 设置mysql账户的用户名和密码
|
||||
|
||||
@@ -66,7 +71,7 @@ const (
|
||||
*/
|
||||
RedisAddr = `192.168.20.10:6379`
|
||||
RedisPassword = `root`
|
||||
RedisDBNo = 1
|
||||
RedisDBNo = 0
|
||||
|
||||
// RedisAddr docker compose 环境下运行使用如下配置信息
|
||||
//RedisAddr = `redis:6379`
|
||||
|
||||
@@ -22,11 +22,17 @@ type IndexLogic struct {
|
||||
var IL *IndexLogic
|
||||
|
||||
// IndexPage 首页数据处理
|
||||
func (i *IndexLogic) IndexPage() gin.H {
|
||||
Info := gin.H{}
|
||||
// 首页分类数据处理
|
||||
|
||||
// 1. 导航分类数据处理, 只提供 电影 电视剧 综艺 动漫 四大顶级分类和其子分类
|
||||
func (i *IndexLogic) IndexPage() map[string]interface{} {
|
||||
//声明返回值
|
||||
//Info := make(map[string]interface{})
|
||||
// 首页请求时长较高, 采用redis进行缓存, 在定时任务更新影片时清除对应缓存
|
||||
// 判断是否存在缓存数据, 存在则直接将数据返回
|
||||
Info := model.GetCacheData(config.IndexCacheKey)
|
||||
if Info != nil {
|
||||
return Info
|
||||
}
|
||||
Info = make(map[string]interface{})
|
||||
// 1. 首页分类数据处理 导航分类数据处理, 只提供 电影 电视剧 综艺 动漫 四大顶级分类和其子分类
|
||||
tree := model.CategoryTree{Category: &model.Category{Id: 0, Name: "分类信息"}}
|
||||
sysTree := model.GetCategoryTree()
|
||||
// 由于采集源数据格式不一,因此采用名称匹配
|
||||
@@ -38,15 +44,18 @@ func (i *IndexLogic) IndexPage() gin.H {
|
||||
}
|
||||
Info["category"] = tree
|
||||
// 2. 提供用于首页展示的顶级分类影片信息, 每分类 14条数据
|
||||
var list []gin.H
|
||||
var list []map[string]interface{}
|
||||
for _, c := range tree.Children {
|
||||
page := model.Page{PageSize: 14, Current: 1}
|
||||
movies := model.GetMovieListByPid(c.Id, &page)
|
||||
item := gin.H{"nav": c, "movies": movies}
|
||||
// 获取当前分类的本月热门影片
|
||||
HotMovies := model.GetHotMovieByPid(c.Id, &page)
|
||||
item := map[string]interface{}{"nav": c, "movies": movies, "hot": HotMovies}
|
||||
list = append(list, item)
|
||||
}
|
||||
Info["content"] = list
|
||||
|
||||
// 不存在首页数据缓存时将查询数据缓存到redis中
|
||||
model.DataCache(config.IndexCacheKey, Info)
|
||||
return Info
|
||||
}
|
||||
|
||||
@@ -141,23 +150,33 @@ func (i *IndexLogic) RelateMovie(detail model.MovieDetail, page *model.Page) []m
|
||||
return model.GetRelateMovieBasicInfo(search, page)
|
||||
}
|
||||
|
||||
// 将多个站点的对应影视播放源追加到主站点播放列表中
|
||||
/*
|
||||
将多个站点的对应影视播放源追加到主站点播放列表中
|
||||
1. 将主站点影片的name 和 subtitle 进行处理添加到用于匹配对应播放源的map中
|
||||
2. 仅对主站点影片name进行映射关系处理并将结果添加到map中
|
||||
例如: xxx第一季 xxx
|
||||
*/
|
||||
func multipleSource(detail *model.MovieDetail) {
|
||||
// 整合多播放源, 处理部分站点中影片名称的空格
|
||||
names := map[string]int{model.HashKey(detail.Name): 0}
|
||||
// 不同站点影片别名匹配
|
||||
re := regexp.MustCompile(`第一季$`)
|
||||
alias := strings.TrimSpace(re.ReplaceAllString(detail.Name, ""))
|
||||
names[model.HashKey(alias)] = 0
|
||||
// 将多个影片别名进行切分,放入names中
|
||||
// 整合多播放源, 初始化存储key map
|
||||
names := make(map[string]int)
|
||||
// 1. 判断detail的dbId是否存在, 存在则添加到names中作为匹配条件
|
||||
if detail.DbId > 0 {
|
||||
names[model.GenerateHashKey(detail.DbId)] = 0
|
||||
}
|
||||
// 2. 对name进行去除特殊格式处理
|
||||
names[model.GenerateHashKey(detail.Name)] = 0
|
||||
// 3. 对包含第一季的name进行处理
|
||||
names[model.GenerateHashKey(regexp.MustCompile(`第一季$`).ReplaceAllString(detail.Name, ""))] = 0
|
||||
|
||||
// 4. 将subtitle进行切分,放入names中
|
||||
if len(detail.SubTitle) > 0 && strings.Contains(detail.SubTitle, ",") {
|
||||
for _, v := range strings.Split(detail.SubTitle, ",") {
|
||||
names[model.HashKey(v)] = 0
|
||||
names[model.GenerateHashKey(v)] = 0
|
||||
}
|
||||
}
|
||||
if len(detail.SubTitle) > 0 && strings.Contains(detail.SubTitle, "/") {
|
||||
for _, v := range strings.Split(detail.SubTitle, "/") {
|
||||
names[model.HashKey(v)] = 0
|
||||
names[model.GenerateHashKey(v)] = 0
|
||||
}
|
||||
}
|
||||
// 遍历站点列表
|
||||
|
||||
@@ -5,12 +5,11 @@ import (
|
||||
"server/plugin/db"
|
||||
"server/plugin/spider"
|
||||
"server/router"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 执行初始化前等待20s , 让mysql服务完成初始化指令
|
||||
time.Sleep(time.Second * 20)
|
||||
//time.Sleep(time.Second * 20)
|
||||
//初始化redis客户端
|
||||
err := db.InitRedisConn()
|
||||
if err != nil {
|
||||
@@ -24,6 +23,7 @@ func init() {
|
||||
}
|
||||
func main() {
|
||||
start()
|
||||
//spider.MtSiteSpider()
|
||||
}
|
||||
|
||||
func start() {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"hash/fnv"
|
||||
"regexp"
|
||||
"server/config"
|
||||
"server/plugin/db"
|
||||
"strconv"
|
||||
@@ -45,6 +46,7 @@ type MovieDescriptor struct {
|
||||
AddTime int64 `json:"addTime"` //资源添加时间戳
|
||||
DbId int64 `json:"dbId"` //豆瓣id
|
||||
DbScore string `json:"dbScore"` // 豆瓣评分
|
||||
Hits int64 `json:"hits"` //影片热度
|
||||
Content string `json:"content"` //内容简介
|
||||
}
|
||||
|
||||
@@ -168,11 +170,22 @@ func SaveSitePlayList(siteName string, list []MovieDetail) (err error) {
|
||||
for _, d := range list {
|
||||
if len(d.PlayList) > 0 {
|
||||
data, _ := json.Marshal(d.PlayList[0])
|
||||
res[HashKey(d.Name)] = string(data)
|
||||
// 不保存电影解说类
|
||||
if strings.Contains(d.CName, "解说") {
|
||||
continue
|
||||
}
|
||||
// 如果DbId不为0, 则以dbID作为key进行hash额外存储一次
|
||||
if d.DbId > 0 {
|
||||
res[GenerateHashKey(d.DbId)] = string(data)
|
||||
}
|
||||
res[GenerateHashKey(d.Name)] = string(data)
|
||||
}
|
||||
}
|
||||
// 保存形式 key: MultipleSource:siteName Hash[hash(movieName)]list
|
||||
err = db.Rdb.HMSet(db.Cxt, fmt.Sprintf(config.MultipleSiteDetail, siteName), res).Err()
|
||||
// 如果结果不为空,则将数据保存到redis中
|
||||
if len(res) > 0 {
|
||||
// 保存形式 key: MultipleSource:siteName Hash[hash(movieName)]list
|
||||
err = db.Rdb.HMSet(db.Cxt, fmt.Sprintf(config.MultipleSiteDetail, siteName), res).Err()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -181,9 +194,9 @@ func AddSearchInfo(searchInfo SearchInfo) (err error) {
|
||||
// 片名 Name 分类 CName 类别标签 classTag 地区 Area 语言 Language 年份 Year 首字母 Initial, 排序
|
||||
data, _ := json.Marshal(searchInfo)
|
||||
// 时间排序 score -->时间戳 DbId 排序 --> 热度, 评分排序 DbScore
|
||||
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf("%s:Pid%d", config.SearchTimeListKey, searchInfo.Pid), redis.Z{Score: float64(searchInfo.Time), Member: data}).Err()
|
||||
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf("%s:Pid%d", config.SearchTimeListKey, searchInfo.Pid), redis.Z{Score: float64(searchInfo.UpdateStamp), Member: data}).Err()
|
||||
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf("%s:Pid%d", config.SearchScoreListKey, searchInfo.Pid), redis.Z{Score: searchInfo.Score, Member: data}).Err()
|
||||
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf("%s:Pid%d", config.SearchHeatListKey, searchInfo.Pid), redis.Z{Score: float64(searchInfo.Rank), Member: data}).Err()
|
||||
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf("%s:Pid%d", config.SearchHeatListKey, searchInfo.Pid), redis.Z{Score: float64(searchInfo.Hits), Member: data}).Err()
|
||||
// 添加搜索关键字信息
|
||||
SearchKeyword(searchInfo)
|
||||
return
|
||||
@@ -298,27 +311,28 @@ func BatchSaveSearchInfo(list []MovieDetail) {
|
||||
func ConvertSearchInfo(detail MovieDetail) SearchInfo {
|
||||
score, _ := strconv.ParseFloat(detail.DbScore, 64)
|
||||
stamp, _ := time.ParseInLocation(time.DateTime, detail.UpdateTime, time.Local)
|
||||
year, err := strconv.ParseInt(detail.Year, 10, 64)
|
||||
// detail中的年份信息并不准确, 因此采用 ReleaseDate中的年份
|
||||
year, err := strconv.ParseInt(regexp.MustCompile(`[1-9][0-9]{3}`).FindString(detail.ReleaseDate), 10, 64)
|
||||
if err != nil {
|
||||
year = 0
|
||||
}
|
||||
return SearchInfo{
|
||||
Mid: detail.Id,
|
||||
Cid: detail.Cid,
|
||||
Pid: detail.Pid,
|
||||
Name: detail.Name,
|
||||
SubTitle: detail.SubTitle,
|
||||
CName: detail.CName,
|
||||
ClassTag: detail.ClassTag,
|
||||
Area: detail.Area,
|
||||
Language: detail.Language,
|
||||
Year: year,
|
||||
Initial: detail.Initial,
|
||||
Score: score,
|
||||
Rank: detail.DbId,
|
||||
Time: stamp.Unix(),
|
||||
State: detail.State,
|
||||
Remarks: detail.Remarks,
|
||||
Mid: detail.Id,
|
||||
Cid: detail.Cid,
|
||||
Pid: detail.Pid,
|
||||
Name: detail.Name,
|
||||
SubTitle: detail.SubTitle,
|
||||
CName: detail.CName,
|
||||
ClassTag: detail.ClassTag,
|
||||
Area: detail.Area,
|
||||
Language: detail.Language,
|
||||
Year: year,
|
||||
Initial: detail.Initial,
|
||||
Score: score,
|
||||
Hits: detail.Hits,
|
||||
UpdateStamp: stamp.Unix(),
|
||||
State: detail.State,
|
||||
Remarks: detail.Remarks,
|
||||
// releaseDate 部分影片缺失该参数, 所以使用添加时间作为上映时间排序
|
||||
ReleaseDate: detail.AddTime,
|
||||
}
|
||||
@@ -342,10 +356,28 @@ func GetDetailByKey(key string) MovieDetail {
|
||||
return detail
|
||||
}
|
||||
|
||||
// HashKey 将字符串转化为hash值
|
||||
func HashKey(str string) string {
|
||||
/*
|
||||
对附属播放源入库时的name|dbID进行处理,保证唯一性
|
||||
1. 去除name中的所有空格
|
||||
2. 去除name中含有的别名~.*~
|
||||
3. 去除name首尾的标点符号
|
||||
4. 将处理完成后的name转化为hash值作为存储时的key
|
||||
*/
|
||||
// GenerateHashKey 存储播放源信息时对影片名称进行处理, 提高各站点间同一影片的匹配度
|
||||
func GenerateHashKey[K string | ~int | int64](key K) string {
|
||||
mName := fmt.Sprint(key)
|
||||
//1. 去除name中的所有空格
|
||||
mName = regexp.MustCompile(`\s`).ReplaceAllString(mName, "")
|
||||
//2. 去除name中含有的别名~.*~
|
||||
mName = regexp.MustCompile(`~.*~$`).ReplaceAllString(mName, "")
|
||||
//3. 去除name首尾的标点符号
|
||||
mName = regexp.MustCompile(`^[[:punct:]]+|[[:punct:]]+$`).ReplaceAllString(mName, "")
|
||||
// 部分站点包含 动画版, 特殊别名 等字符, 需进行删除
|
||||
//mName = regexp.MustCompile(`动画版`).ReplaceAllString(mName, "")
|
||||
mName = regexp.MustCompile(`季.*`).ReplaceAllString(mName, "季")
|
||||
//4. 将处理完成后的name转化为hash值作为存储时的key
|
||||
h := fnv.New32a()
|
||||
_, err := h.Write([]byte(str))
|
||||
_, err := h.Write([]byte(mName))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ type MovieDetailInfo struct {
|
||||
AddTime int64 `json:"vod_time_add"` //资源添加时间戳
|
||||
DbId int64 `json:"vod_douban_id"` //豆瓣id
|
||||
DbScore string `json:"vod_douban_score"` // 豆瓣评分
|
||||
Hits int64 `json:"vod_hits"` // 总热度
|
||||
Content string `json:"vod_content"` //内容简介
|
||||
PlayFrom string `json:"vod_play_from"` // 播放来源
|
||||
PlaySeparator string `json:"vod_play_note"` // 播放信息分隔符
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"server/config"
|
||||
"server/plugin/db"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SearchInfo 存储用于检索的信息
|
||||
@@ -28,8 +29,8 @@ type SearchInfo struct {
|
||||
Year int64 `json:"year"` // 年份
|
||||
Initial string `json:"initial"` // 首字母
|
||||
Score float64 `json:"score"` //评分
|
||||
Time int64 `json:"time"` // 更新时间
|
||||
Rank int64 `json:"rank"` // 热度排行id
|
||||
UpdateStamp int64 `json:"updateStamp"` // 更新时间
|
||||
Hits int64 `json:"hits"` // 热度排行
|
||||
State string `json:"state"` //状态 正片|预告
|
||||
Remarks string `json:"remarks"` // 完结 | 更新至x集
|
||||
ReleaseDate int64 `json:"releaseDate"` //上映时间 时间戳
|
||||
@@ -144,7 +145,7 @@ func BatchSaveOrUpdate(list []SearchInfo) {
|
||||
// 如果存在对应数据则进行更新, 否则进行删除
|
||||
if count > 0 {
|
||||
// 记录已经存在则执行更新部分内容
|
||||
err := tx.Model(&SearchInfo{}).Where("mid", info.Mid).Updates(SearchInfo{Time: info.Time, Rank: info.Rank, State: info.State,
|
||||
err := tx.Model(&SearchInfo{}).Where("mid", info.Mid).Updates(SearchInfo{UpdateStamp: info.UpdateStamp, Hits: info.Hits, State: info.State,
|
||||
Remarks: info.Remarks, Score: info.Score, ReleaseDate: info.ReleaseDate}).Error
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -198,7 +199,7 @@ func GetMovieListByPid(pid int64, page *Page) []MovieBasicInfo {
|
||||
page.PageCount = int((page.Total + page.PageSize - 1) / page.PageSize)
|
||||
// 进行具体的信息查询
|
||||
var s []SearchInfo
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("pid", pid).Order("year DESC, time DESC").Find(&s).Error; err != nil {
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("pid", pid).Order("year DESC, update_stamp DESC").Find(&s).Error; err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
@@ -211,6 +212,24 @@ func GetMovieListByPid(pid int64, page *Page) []MovieBasicInfo {
|
||||
return list
|
||||
}
|
||||
|
||||
// GetHotMovieByPid 获取指定类别的热门影片
|
||||
func GetHotMovieByPid(pid int64, page *Page) []SearchInfo {
|
||||
// 返回分页参数
|
||||
var count int64
|
||||
db.Mdb.Model(&SearchInfo{}).Where("pid", pid).Count(&count)
|
||||
page.Total = int(count)
|
||||
page.PageCount = int((page.Total + page.PageSize - 1) / page.PageSize)
|
||||
// 进行具体的信息查询
|
||||
var s []SearchInfo
|
||||
// 当前时间偏移一个月
|
||||
t := time.Now().AddDate(0, -1, 0).Unix()
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("pid=? AND update_stamp > ?", pid, t).Order(" year DESC, hits DESC").Find(&s).Error; err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SearchFilmKeyword 通过关键字搜索库存中满足条件的影片名
|
||||
func SearchFilmKeyword(keyword string, page *Page) []SearchInfo {
|
||||
var searchList []SearchInfo
|
||||
@@ -221,7 +240,7 @@ func SearchFilmKeyword(keyword string, page *Page) []SearchInfo {
|
||||
page.PageCount = int((page.Total + page.PageSize - 1) / page.PageSize)
|
||||
// 2. 获取满足条件的数据
|
||||
db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).
|
||||
Where("name LIKE ?", fmt.Sprint(`%`, keyword, `%`)).Or("sub_title LIKE ?", fmt.Sprint(`%`, keyword, `%`)).Order("year DESC, time DESC").Find(&searchList)
|
||||
Where("name LIKE ?", fmt.Sprint(`%`, keyword, `%`)).Or("sub_title LIKE ?", fmt.Sprint(`%`, keyword, `%`)).Order("year DESC, update_stamp DESC").Find(&searchList)
|
||||
return searchList
|
||||
}
|
||||
|
||||
@@ -234,7 +253,7 @@ func GetMovieListByCid(cid int64, page *Page) []MovieBasicInfo {
|
||||
page.PageCount = int((page.Total + page.PageSize - 1) / page.PageSize)
|
||||
// 进行具体的信息查询
|
||||
var s []SearchInfo
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("cid", cid).Order("year DESC, time DESC").Find(&s).Error; err != nil {
|
||||
if err := db.Mdb.Limit(page.PageSize).Offset((page.Current-1)*page.PageSize).Where("cid", cid).Order("year DESC, update_stamp DESC").Find(&s).Error; err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
@@ -302,3 +321,25 @@ func GetMultiplePlay(siteName, key string) []MovieUrlInfo {
|
||||
_ = json.Unmarshal([]byte(data), &playList)
|
||||
return playList
|
||||
}
|
||||
|
||||
// DataCache API请求 数据缓存
|
||||
func DataCache(key string, data map[string]interface{}) {
|
||||
val, _ := json.Marshal(data)
|
||||
db.Rdb.Set(db.Cxt, key, val, config.CategoryTreeExpired)
|
||||
}
|
||||
|
||||
// GetCacheData 获取API接口的缓存数据
|
||||
func GetCacheData(key string) map[string]interface{} {
|
||||
data := make(map[string]interface{})
|
||||
val, err := db.Rdb.Get(db.Cxt, key).Result()
|
||||
if err != nil || len(val) <= 0 {
|
||||
return nil
|
||||
}
|
||||
_ = json.Unmarshal([]byte(val), &data)
|
||||
return data
|
||||
}
|
||||
|
||||
// RemoveCache 删除数据缓存
|
||||
func RemoveCache(key string) {
|
||||
db.Rdb.Del(db.Cxt, key)
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ func ProcessMovieDetail(detail model.MovieDetailInfo) model.MovieDetail {
|
||||
AddTime: detail.AddTime,
|
||||
DbId: detail.DbId,
|
||||
DbScore: detail.DbScore,
|
||||
Hits: detail.Hits,
|
||||
Content: detail.Content,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ type Site struct {
|
||||
var SiteList = []Site{
|
||||
//{"tk", "https://api.tiankongapi.com/api.php/provide/vod"},
|
||||
//{"yh", "https://m3u8.apiyhzy.com/api.php/provide/vod/"},
|
||||
//{"zk", "https://api.1080zyku.com/inc/apijson.php"}, 数据格式不规范,不采用
|
||||
//{"fs", "https://www.feisuzyapi.com/api.php/provide/vod/"},
|
||||
|
||||
{"lz", "https://cj.lziapi.com/api.php/provide/vod/"},
|
||||
@@ -251,7 +252,7 @@ func UpdateMainDetail() {
|
||||
func UpdatePlayDetail() {
|
||||
for _, s := range SiteList {
|
||||
// 获取单个站点的分页数
|
||||
r := RequestInfo{Uri: s.Name, Params: url.Values{}}
|
||||
r := RequestInfo{Uri: s.Uri, Params: url.Values{}}
|
||||
r.Params.Set("h", config.UpdateInterval)
|
||||
pageCount, err := GetPageCount(r)
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"log"
|
||||
"server/config"
|
||||
"server/model"
|
||||
)
|
||||
|
||||
// RegularUpdateMovie 定时更新, 每半小时获取一次站点的最近x小时数据
|
||||
@@ -14,6 +15,8 @@ func RegularUpdateMovie() {
|
||||
// 执行更新最近x小时影片的Spider
|
||||
log.Println("执行一次影片更新任务...")
|
||||
UpdateMovieDetail()
|
||||
// 执行更新任务后清理redis中的相关API接口数据缓存
|
||||
clearCache()
|
||||
})
|
||||
|
||||
// 开启定时任务每月最后一天凌晨两点, 执行一次清库重取数据
|
||||
@@ -27,3 +30,8 @@ func RegularUpdateMovie() {
|
||||
|
||||
c.Start()
|
||||
}
|
||||
|
||||
// 清理API接口数据缓存
|
||||
func clearCache() {
|
||||
model.RemoveCache(config.IndexCacheKey)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,9 @@ type RequestInfo struct {
|
||||
Resp []byte `json:"resp"` // 响应结果数据
|
||||
}
|
||||
|
||||
// RefererUrl 记录上次请求的url
|
||||
var RefererUrl string
|
||||
|
||||
// CreateClient 初始化请求客户端
|
||||
func CreateClient() *colly.Collector {
|
||||
c := colly.NewCollector()
|
||||
@@ -43,6 +47,11 @@ func CreateClient() *colly.Collector {
|
||||
// 设置一些请求头信息
|
||||
request.Headers.Set("Content-Type", "application/json;charset=UTF-8")
|
||||
//request.Headers.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
|
||||
// 请求完成后设置请求头Referer
|
||||
if len(RefererUrl) <= 0 || !strings.Contains(RefererUrl, request.URL.Host) {
|
||||
RefererUrl = ""
|
||||
}
|
||||
request.Headers.Set("Referer", RefererUrl)
|
||||
})
|
||||
// 请求期间报错的回调
|
||||
c.OnError(func(response *colly.Response, err error) {
|
||||
@@ -69,6 +78,8 @@ func ApiGet(r *RequestInfo) {
|
||||
} else {
|
||||
r.Resp = []byte{}
|
||||
}
|
||||
// 将请求url保存到RefererUrl 用于 Header Refer属性
|
||||
RefererUrl = response.Request.URL.String()
|
||||
// 拿到response后输出请求url
|
||||
//log.Println("\n请求成功: ", response.Request.URL)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user