database intro

This commit is contained in:
mubai
2023-06-06 10:50:23 +08:00
parent 04c93f0a55
commit 0591c6faa2
6 changed files with 134 additions and 54 deletions

View File

@@ -81,3 +81,70 @@ server
1. 修改 /server/plugin/db 目录下的 mysql.go 和 redis.go 中的连接地址和用户名密码
2. 在 server 目录下执行 `go run main.go`
## 数据库信息简介
#### 1.Mysql
> 连接信息(以docker compose部署为例) :
```yaml
mysql:
ip: 部署的服务器IP
port: 3610
username: root
password: root
database: FilmSite
```
> 数据库结构
- 数据库: FilmSite
- 数据表 search
> search 表 (用于记录影片的相关检索信息, 主要用于影片的 搜索, 分类, 排序 等)
| 字段名称 | 类型 | 字段释义 |
| ------------ | -------- | ---------------- |
| id | bigint | 自增主键 |
| created_at | datetime | 记录创建时间 |
| updated_at | datetime | 记录更新时间 |
| deleted_at | datetime | 逻辑删除字段 |
| mid | bigint | 影片ID |
| cid | bigint | 二级分类ID |
| pid | bigint | 一级分类ID |
| name | varchar | 影片名称 |
| sub_title | varchar | 子标题(影片别名) |
| c_name | varchar | 分类名称 |
| class_tag | varchar | 剧情标签 |
| area | varchar | 地区 |
| language | varchar | 语言 |
| year | bigint | 上映年份 |
| initial | varchar | 首字母 |
| score | double | 豆瓣评分 |
| update_stamp | bigint | 影片更新时间戳 |
| hits | bigint | 热度(播放次数) |
| state | varchar | 状态(正片) |
| remarks | varchar | 更新状态(完结 |
| release_data | bigint | 上映时间戳 |
#### 2.Redis
> 连接信息(以docker compose部署为例) :
```yaml
## 部署时默认使用如下信息
redis:
ip: 部署的服务器IP
port: 3620
password: root
DB: 0 ##使用的redis数据库为0号库
```

View File

@@ -47,6 +47,7 @@ const (
)
/*API相关redis key*/
const (
IndexCacheKey = "IndexCache"
)

View File

@@ -89,28 +89,6 @@ type MovieDetail struct {
MovieDescriptor `json:"descriptor"` //影片描述信息
}
// SaveMoves 保存影片分页请求list
func SaveMoves(list []Movie) (err error) {
// 整合数据
for _, m := range list {
//score, _ := time.ParseInLocation(time.DateTime, m.Time, time.Local)
movie, _ := json.Marshal(m)
// 以Cid为目录为集合进行存储, 便于后续搜索, 以影片id为分值进行存储 例 MovieList:Cid%d
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf(config.MovieListInfoKey, m.Cid), redis.Z{Score: float64(m.Id), Member: movie}).Err()
}
return err
}
// AllMovieInfoKey 获取redis中所有的影视列表信息key MovieList:Cid
func AllMovieInfoKey() []string {
return db.Rdb.Keys(db.Cxt, fmt.Sprint("MovieList:Cid*")).Val()
}
// GetMovieListByKey 获取指定分类的影片列表数据
func GetMovieListByKey(key string) []string {
return db.Rdb.ZRange(db.Cxt, key, 0, -1).Val()
}
// SaveDetails 保存影片详情信息到redis中 格式: MovieDetail:Cid?:Id?
func SaveDetails(list []MovieDetail) (err error) {
// 遍历list中的信息
@@ -383,3 +361,29 @@ func GenerateHashKey[K string | ~int | int64](key K) string {
}
return fmt.Sprint(h.Sum32())
}
// 处理分类方法0
// ============================采集方案.v1 遗留==================================================
// SaveMoves 保存影片分页请求list
func SaveMoves(list []Movie) (err error) {
// 整合数据
for _, m := range list {
//score, _ := time.ParseInLocation(time.DateTime, m.Time, time.Local)
movie, _ := json.Marshal(m)
// 以Cid为目录为集合进行存储, 便于后续搜索, 以影片id为分值进行存储 例 MovieList:Cid%d
err = db.Rdb.ZAdd(db.Cxt, fmt.Sprintf(config.MovieListInfoKey, m.Cid), redis.Z{Score: float64(m.Id), Member: movie}).Err()
}
return err
}
// AllMovieInfoKey 获取redis中所有的影视列表信息key MovieList:Cid
func AllMovieInfoKey() []string {
return db.Rdb.Keys(db.Cxt, fmt.Sprint("MovieList:Cid*")).Val()
}
// GetMovieListByKey 获取指定分类的影片列表数据
func GetMovieListByKey(key string) []string {
return db.Rdb.ZRange(db.Cxt, key, 0, -1).Val()
}

View File

@@ -133,12 +133,6 @@ func BatchSave(list []SearchInfo) {
// BatchSaveOrUpdate 判断数据库中是否存在对应mid的数据, 如果存在则更新, 否则插入
func BatchSaveOrUpdate(list []SearchInfo) {
tx := db.Mdb.Begin()
// 失败则回滚事务
//defer func() {
// if r := recover(); r != nil {
// tx.Rollback()
// }
//}()
for _, info := range list {
var count int64
// 通过当前影片id 对应的记录数
@@ -213,6 +207,28 @@ func GetMovieListByPid(pid int64, page *Page) []MovieBasicInfo {
return list
}
// GetMovieListByCid 通过Cid查找对应的影片分页数据, 不适合GetMovieListByPid 糅合
func GetMovieListByCid(cid int64, page *Page) []MovieBasicInfo {
// 返回分页参数
var count int64
db.Mdb.Model(&SearchInfo{}).Where("cid", cid).Count(&count)
page.Total = int(count)
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, update_stamp DESC").Find(&s).Error; err != nil {
log.Println(err)
return nil
}
// 通过影片ID去redis中获取id对应数据信息
var list []MovieBasicInfo
for _, v := range s {
// 通过key搜索指定的影片信息 , MovieDetail:Cid6:Id15441
list = append(list, GetBasicInfoByKey(fmt.Sprintf(config.MovieBasicInfoKey, v.Cid, v.Mid)))
}
return list
}
// GetHotMovieByPid 获取指定类别的热门影片
func GetHotMovieByPid(pid int64, page *Page) []SearchInfo {
// 返回分页参数
@@ -245,28 +261,6 @@ func SearchFilmKeyword(keyword string, page *Page) []SearchInfo {
return searchList
}
// GetMovieListByCid 通过Cid查找对应的影片分页数据, 不适合GetMovieListByPid 糅合
func GetMovieListByCid(cid int64, page *Page) []MovieBasicInfo {
// 返回分页参数
var count int64
db.Mdb.Model(&SearchInfo{}).Where("cid", cid).Count(&count)
page.Total = int(count)
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, update_stamp DESC").Find(&s).Error; err != nil {
log.Println(err)
return nil
}
// 通过影片ID去redis中获取id对应数据信息
var list []MovieBasicInfo
for _, v := range s {
// 通过key搜索指定的影片信息 , MovieDetail:Cid6:Id15441
list = append(list, GetBasicInfoByKey(fmt.Sprintf(config.MovieBasicInfoKey, v.Cid, v.Mid)))
}
return list
}
// GetRelateMovieBasicInfo GetRelateMovie 根据SearchInfo获取相关影片
func GetRelateMovieBasicInfo(search SearchInfo, page *Page) []MovieBasicInfo {
/*

View File

@@ -29,7 +29,7 @@ import (
*/
const (
MainSite = "https://www.feisuzyapi.com/api.php/provide/vod/"
MainSite = "https://cj.lziapi.com/api.php/provide/vod/"
)
type Site struct {
@@ -43,10 +43,13 @@ var SiteList = []Site{
//{"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/"},
//{"su", "https://subocaiji.com/api.php/provide/vod/at/json"},
{"lz", "https://cj.lziapi.com/api.php/provide/vod/"},
{"ff", "https://cj.ffzyapi.com/api.php/provide/vod/"},
{"su", "https://subocaiji.com/api.php/provide/vod/at/json"},
//{"lz", "https://cj.lziapi.com/api.php/provide/vod/"},
//{"ff", "https://cj.ffzyapi.com/api.php/provide/vod/"},
//{"fs", "https://www.feisuzyapi.com/api.php/provide/vod/"},
//{"bf", "https://bfzyapi.com/api.php/provide/vod/"},
{"test", "https://bfzyapi.com/api.php/provide/vod/"},
}
// StartSpider 执行多源spider

View File

@@ -30,6 +30,10 @@ 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)
@@ -46,6 +50,7 @@ func CreateClient() *colly.Collector {
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) {
@@ -89,3 +94,9 @@ func ApiGet(r *RequestInfo) {
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)})
}