mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-02-19 00:54:41 +08:00
cache picuter to server
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"server/config"
|
||||
"server/model"
|
||||
"server/plugin/common/dp"
|
||||
"server/plugin/common/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -66,7 +67,7 @@ func StartSpider() {
|
||||
SearchInfoToMdb()
|
||||
model.AddSearchIndex()
|
||||
log.Println("SearchInfoToMdb 影片检索信息保存完毕")
|
||||
//获取其他站点数据13
|
||||
//获取其他站点数据
|
||||
go MtSiteSpider()
|
||||
log.Println("Spider End , 数据保存执行完成")
|
||||
time.Sleep(time.Second * 10)
|
||||
@@ -75,12 +76,12 @@ func StartSpider() {
|
||||
// CategoryList 获取分类数据
|
||||
func CategoryList() {
|
||||
// 设置请求参数信息
|
||||
r := RequestInfo{Uri: MainSite, Params: url.Values{}}
|
||||
r := util.RequestInfo{Uri: MainSite, Params: url.Values{}}
|
||||
r.Params.Set(`ac`, "list")
|
||||
r.Params.Set(`pg`, "1")
|
||||
r.Params.Set(`t`, "1")
|
||||
// 执行请求, 获取一次list数据
|
||||
ApiGet(&r)
|
||||
util.ApiGet(&r)
|
||||
// 解析resp数据
|
||||
movieListInfo := model.MovieListInfo{}
|
||||
if len(r.Resp) <= 0 {
|
||||
@@ -103,7 +104,7 @@ func CategoryList() {
|
||||
// MainSiteSpider 主站点数据处理
|
||||
func MainSiteSpider() {
|
||||
// 获取分页页数
|
||||
pageCount, err := GetPageCount(RequestInfo{Uri: MainSite, Params: url.Values{}})
|
||||
pageCount, err := GetPageCount(util.RequestInfo{Uri: MainSite, Params: url.Values{}})
|
||||
// 主站点分页出错直接终止程序
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -123,7 +124,7 @@ func MainSiteSpider() {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
list, e := GetMovieDetail(pg, RequestInfo{Uri: MainSite, Params: url.Values{}})
|
||||
list, e := GetMovieDetail(pg, util.RequestInfo{Uri: MainSite, Params: url.Values{}})
|
||||
if e != nil {
|
||||
log.Println("GetMovieDetail Error: ", err)
|
||||
continue
|
||||
@@ -152,7 +153,7 @@ func MtSiteSpider() {
|
||||
// PlayDetailSpider SpiderSimpleInfo 获取单个站点的播放源
|
||||
func PlayDetailSpider(s Site) {
|
||||
// 获取分页页数
|
||||
pageCount, err := GetPageCount(RequestInfo{Uri: s.Uri, Params: url.Values{}})
|
||||
pageCount, err := GetPageCount(util.RequestInfo{Uri: s.Uri, Params: url.Values{}})
|
||||
// 出错直接终止当前站点数据获取
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -174,7 +175,7 @@ func PlayDetailSpider(s Site) {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
list, e := GetMovieDetail(pg, RequestInfo{Uri: s.Uri, Params: url.Values{}})
|
||||
list, e := GetMovieDetail(pg, util.RequestInfo{Uri: s.Uri, Params: url.Values{}})
|
||||
if e != nil || len(list) <= 0 {
|
||||
log.Println("GetMovieDetail Error: ", err)
|
||||
continue
|
||||
@@ -221,7 +222,7 @@ func UpdateMovieDetail() {
|
||||
// UpdateMainDetail 更新主站点的最新影片
|
||||
func UpdateMainDetail() {
|
||||
// 获取分页页数
|
||||
r := RequestInfo{Uri: MainSite, Params: url.Values{}}
|
||||
r := util.RequestInfo{Uri: MainSite, Params: url.Values{}}
|
||||
r.Params.Set("h", config.UpdateInterval)
|
||||
pageCount, err := GetPageCount(r)
|
||||
if err != nil {
|
||||
@@ -256,7 +257,7 @@ func UpdateMainDetail() {
|
||||
func UpdatePlayDetail() {
|
||||
for _, s := range SiteList {
|
||||
// 获取单个站点的分页数
|
||||
r := RequestInfo{Uri: s.Uri, Params: url.Values{}}
|
||||
r := util.RequestInfo{Uri: s.Uri, Params: url.Values{}}
|
||||
r.Params.Set("h", config.UpdateInterval)
|
||||
pageCount, err := GetPageCount(r)
|
||||
if err != nil {
|
||||
@@ -288,11 +289,11 @@ func StartSpiderRe() {
|
||||
// =========================公共方法==============================
|
||||
|
||||
// GetPageCount 获取总页数
|
||||
func GetPageCount(r RequestInfo) (count int, err error) {
|
||||
func GetPageCount(r util.RequestInfo) (count int, err error) {
|
||||
// 发送请求获取pageCount
|
||||
r.Params.Set("ac", "detail")
|
||||
r.Params.Set("pg", "2")
|
||||
ApiGet(&r)
|
||||
util.ApiGet(&r)
|
||||
// 判断请求结果是否为空, 如果为空直接输出错误并终止
|
||||
if len(r.Resp) <= 0 {
|
||||
err = errors.New("response is empty")
|
||||
@@ -309,7 +310,7 @@ func GetPageCount(r RequestInfo) (count int, err error) {
|
||||
}
|
||||
|
||||
// GetMovieDetail 处理详情接口请求返回的数据
|
||||
func GetMovieDetail(pageNumber int, r RequestInfo) (list []model.MovieDetail, err error) {
|
||||
func GetMovieDetail(pageNumber int, r util.RequestInfo) (list []model.MovieDetail, err error) {
|
||||
// 防止json解析异常引发panic
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
@@ -319,7 +320,7 @@ func GetMovieDetail(pageNumber int, r RequestInfo) (list []model.MovieDetail, er
|
||||
// 设置分页请求参数
|
||||
r.Params.Set(`ac`, `detail`)
|
||||
r.Params.Set(`pg`, fmt.Sprint(pageNumber))
|
||||
ApiGet(&r)
|
||||
util.ApiGet(&r)
|
||||
// 影视详情信息
|
||||
details := model.DetailListInfo{}
|
||||
// 如果返回数据为空则直接结束本次循环
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
package spider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gocolly/colly/v2"
|
||||
"github.com/gocolly/colly/v2/extensions"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Client = CreateClient()
|
||||
)
|
||||
|
||||
// RequestInfo 请求参数结构体
|
||||
type RequestInfo struct {
|
||||
Uri string `json:"uri"` // 请求url地址
|
||||
Params url.Values `json:"param"` // 请求参数
|
||||
Header http.Header `json:"header"` // 请求头数据
|
||||
Resp []byte `json:"resp"` // 响应结果数据
|
||||
}
|
||||
|
||||
// RefererUrl 记录上次请求的url
|
||||
var RefererUrl string
|
||||
|
||||
// CreateClient 初始化请求客户端
|
||||
func CreateClient() *colly.Collector {
|
||||
c := colly.NewCollector()
|
||||
|
||||
// 设置请求使用clash的socks5代理
|
||||
//setProxy(c)
|
||||
|
||||
// 设置代理信息
|
||||
//if proxy, err := proxy.RoundRobinProxySwitcher("127.0.0.1:7890"); err != nil {
|
||||
// c.SetProxyFunc(proxy)
|
||||
//}
|
||||
// 设置并发数量控制
|
||||
//c.Async = true
|
||||
// 访问深度
|
||||
c.MaxDepth = 1
|
||||
//可重复访问
|
||||
c.AllowURLRevisit = true
|
||||
// 设置超时时间 默认10s
|
||||
c.SetRequestTimeout(20 * time.Second)
|
||||
// 发起请求之前会调用的方法
|
||||
c.OnRequest(func(request *colly.Request) {
|
||||
// 设置一些请求头信息
|
||||
request.Headers.Set("Content-Type", "application/json;charset=UTF-8")
|
||||
request.Headers.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
|
||||
//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) {
|
||||
log.Printf("请求异常: URL: %s Error: %s\n", response.Request.URL, err)
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
// ApiGet 请求数据的方法
|
||||
func ApiGet(r *RequestInfo) {
|
||||
if r.Header != nil {
|
||||
if t, err := strconv.Atoi(r.Header.Get("timeout")); err != nil && t > 0 {
|
||||
Client.SetRequestTimeout(time.Duration(t) * time.Second)
|
||||
}
|
||||
}
|
||||
// 设置随机请求头
|
||||
extensions.RandomUserAgent(Client)
|
||||
//extensions.Referer(Client)
|
||||
// 请求成功后的响应
|
||||
Client.OnResponse(func(response *colly.Response) {
|
||||
// 将响应结构封装到 RequestInfo.Resp中
|
||||
if len(response.Body) > 0 {
|
||||
r.Resp = response.Body
|
||||
} else {
|
||||
r.Resp = []byte{}
|
||||
}
|
||||
// 将请求url保存到RefererUrl 用于 Header Refer属性
|
||||
RefererUrl = response.Request.URL.String()
|
||||
// 拿到response后输出请求url
|
||||
//log.Println("\n请求成功: ", response.Request.URL)
|
||||
})
|
||||
// 处理请求参数
|
||||
err := Client.Visit(fmt.Sprintf("%s?%s", r.Uri, r.Params.Encode()))
|
||||
if err != nil {
|
||||
log.Println("获取数据失败: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 本地代理测试
|
||||
func setProxy(c *colly.Collector) {
|
||||
proxyUrl, _ := url.Parse("socks5://127.0.0.1:7890")
|
||||
c.WithTransport(&http.Transport{Proxy: http.ProxyURL(proxyUrl)})
|
||||
}
|
||||
Reference in New Issue
Block a user