Преглед изворни кода

像公用数据结构外置

Signed-off-by: allan716 <[email protected]>
allan716 пре 3 година
родитељ
комит
6d7751c790

+ 1 - 0
go.mod

@@ -72,6 +72,7 @@ require (
 	github.com/Microsoft/go-winio v0.4.16 // indirect
 	github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
 	github.com/acomagu/bufpipe v1.0.3 // indirect
+	github.com/allanpk716/ChineseSubModels v0.0.1 // indirect
 	github.com/andybalholm/brotli v1.0.0 // indirect
 	github.com/andybalholm/cascadia v1.2.0 // indirect
 	github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect

+ 2 - 0
go.sum

@@ -50,6 +50,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
+github.com/allanpk716/ChineseSubModels v0.0.1 h1:RP7tw/dhGWB22uurgYZ8dvdsPRA8hZTD0GtoQiJqah8=
+github.com/allanpk716/ChineseSubModels v0.0.1/go.mod h1:4Yd7MJ5jPxCKSHUJkJT8wlrC8wj2Rh5D4yLtXAEp1uE=
 github.com/allanpk716/fake-useragent v0.2.1 h1:5EurHovAMGNwMZNbZ2cFOH8HsLdHwopnnKWCsV3PtNI=
 github.com/allanpk716/fake-useragent v0.2.1/go.mod h1:U3tmrxBYvSPeQKQ/9x59CXJPkHbU4x4eXw3ji5TDhPk=
 github.com/allanpk716/go-parse-torrent-name v0.0.0-20220212145434-430ea25cbba7 h1:5AYgeYsVjtDPG5sUS77lyeEPiRMduIKstLjkBLiVPRc=

+ 2 - 1
internal/dao/init.go

@@ -7,6 +7,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	gModels "github.com/allanpk716/ChineseSubModels/models"
 	"gorm.io/driver/sqlite"
 	"gorm.io/gorm"
 	"os"
@@ -68,7 +69,7 @@ func InitDb() error {
 	}
 	// 迁移 schema
 	err = db.AutoMigrate(&models.HotFix{}, &models.SubFormatRec{},
-		&models.IMDBInfo{}, &models.VideoSubInfo{})
+		&gModels.IMDBInfo{}, &gModels.VideoSubInfo{})
 	if err != nil {
 		return errors.New(fmt.Sprintf("db AutoMigrate error, %s", err.Error()))
 	}

+ 5 - 5
internal/logic/series_helper/seriesHelper.go

@@ -6,7 +6,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/ifaces"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
-	"github.com/allanpk716/ChineseSubFinder/internal/models"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/imdb_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
@@ -17,6 +16,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types/emby"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
+	gModels "github.com/allanpk716/ChineseSubModels/models"
 	"github.com/emirpasic/gods/maps/treemap"
 	"github.com/jinzhu/now"
 	"path/filepath"
@@ -25,7 +25,7 @@ import (
 )
 
 // ReadSeriesInfoFromDir 读取剧集的信息,只有那些 Eps 需要下载字幕的 NeedDlEpsKeyList
-func ReadSeriesInfoFromDir(seriesDir string, imdbInfo *models.IMDBInfo, forcedScanAndDownloadSub bool) (*series.SeriesInfo, error) {
+func ReadSeriesInfoFromDir(seriesDir string, imdbInfo *gModels.IMDBInfo, forcedScanAndDownloadSub bool) (*series.SeriesInfo, error) {
 
 	subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
 
@@ -97,7 +97,7 @@ func ReadSeriesInfoFromDir(seriesDir string, imdbInfo *models.IMDBInfo, forcedSc
 }
 
 // ReadSeriesInfoFromEmby 将 Emby API 读取到的数据进行转换到通用的结构中,需要填充那些剧集需要下载,这样要的是一个连续剧的,不是所有的传入
-func ReadSeriesInfoFromEmby(seriesDir string, imdbInfo *models.IMDBInfo, seriesList []emby.EmbyMixInfo) (*series.SeriesInfo, error) {
+func ReadSeriesInfoFromEmby(seriesDir string, imdbInfo *gModels.IMDBInfo, seriesList []emby.EmbyMixInfo) (*series.SeriesInfo, error) {
 
 	seriesInfo, err := getSeriesInfoFromDir(seriesDir, imdbInfo)
 	if err != nil {
@@ -122,7 +122,7 @@ func ReadSeriesInfoFromEmby(seriesDir string, imdbInfo *models.IMDBInfo, seriesL
 }
 
 // SkipChineseSeries 跳过中文连续剧
-func SkipChineseSeries(seriesRootPath string, _proxySettings ...settings.ProxySettings) (bool, *models.IMDBInfo, error) {
+func SkipChineseSeries(seriesRootPath string, _proxySettings ...settings.ProxySettings) (bool, *gModels.IMDBInfo, error) {
 	var proxySettings settings.ProxySettings
 	if len(_proxySettings) > 0 {
 		proxySettings = _proxySettings[0]
@@ -330,7 +330,7 @@ func whichSeasonEpsNeedDownloadSub(seriesInfo *series.SeriesInfo, forcedScanAndD
 	return needDlSubEpsList, needDlSeasonList
 }
 
-func getSeriesInfoFromDir(seriesDir string, imdbInfo *models.IMDBInfo) (*series.SeriesInfo, error) {
+func getSeriesInfoFromDir(seriesDir string, imdbInfo *gModels.IMDBInfo) (*series.SeriesInfo, error) {
 	seriesInfo := series.SeriesInfo{}
 	// 只考虑 IMDB 去查询,文件名目前发现可能会跟电影重复,导致很麻烦,本来也有前置要求要削刮器处理的
 	videoInfo, err := decode.GetImdbInfo4SeriesDir(seriesDir)

+ 0 - 6
internal/models/imdb_aka.go

@@ -1,6 +0,0 @@
-package models
-
-type IMDBAKA struct {
-	AKA        string
-	IMDBInfoID uint
-}

+ 0 - 15
internal/models/imdb_info.go

@@ -1,15 +0,0 @@
-package models
-
-type IMDBInfo struct {
-	IMDBID        string         `gorm:"primaryKey"` // IMDB ID
-	Name          string         // 视频名称
-	Year          int            `gorm:"default:0"` // 发布的时间
-	Description   string         // 描述
-	Languages     StringList     `gorm:"type:varchar(255);not null"` // 语言
-	AKA           StringList     `gorm:"type:varchar(255);not null"` // 又名 xx xxx
-	VideoSubInfos []VideoSubInfo `gorm:"foreignKey:IMDBInfoID"`      // 视频对应的字幕,外键约束
-}
-
-func NewIMDBInfo(IMDBID string, name string, year int, description string, languages StringList, AKA StringList) *IMDBInfo {
-	return &IMDBInfo{IMDBID: IMDBID, Name: name, Year: year, Description: description, Languages: languages, AKA: AKA, VideoSubInfos: make([]VideoSubInfo, 0)}
-}

+ 0 - 6
internal/models/imdb_language.go

@@ -1,6 +0,0 @@
-package models
-
-type IMDBLanguage struct {
-	Language   string
-	IMDBInfoID uint
-}

+ 0 - 16
internal/models/string_array.go

@@ -1,16 +0,0 @@
-package models
-
-import (
-	"database/sql/driver"
-	"encoding/json"
-)
-
-type StringList []string
-
-func (p StringList) Value() (driver.Value, error) {
-	return json.Marshal(p)
-}
-
-func (p *StringList) Scan(data interface{}) error {
-	return json.Unmarshal(data.([]byte), &p)
-}

+ 0 - 22
internal/models/video_sub_info.go

@@ -1,22 +0,0 @@
-package models
-
-// VideoSubInfo 属于 IMDBInfo,IMDBInfoID 是外键,使用了 GORM 的 HasMany 关联
-type VideoSubInfo struct {
-	Feature      string `gorm:"primaryKey" json:"feature"` // 特征码,这个未必有,比如是蓝光格式,分散成多个视频文件的时候,暂定使用本程序的特征提前方式
-	SubName      string `json:"sub_name"`                  // 字幕的文件名
-	Season       int    `json:"season"`                    // 如果对应的是电影则可能是 0,没有
-	Episode      int    `json:"episode"`                   // 如果对应的是电影则可能是 0,没有
-	LanguageISO  string `json:"language_iso"`              // 字幕的语言,目标语言,就算是双语,中英,也应该是中文。ISO_639-1_codes 标准,见 ISOLanguage.go 文件,这里无法区分简体繁体
-	IsDouble     bool   `json:"is_double"`                 // 是否是双语,上面是主体语言,比如是中文,
-	ChineseISO   string `json:"chinese_iso"`               // 中文语言编码变种,见 ISOLanguage.go 文件,这里区分简体、繁体等,如果语言是非中文则这里是空
-	MyLanguage   string `json:"my_language"`               // 这个是本程序定义的语言类型,见 my_language.go 文件
-	StoreRPath   string `json:"store_r_path"`              // 字幕存在出本地的哪里相对路径上,cache/CSF-ShareSubCache
-	ExtraPreName string `json:"extra_pre_name"`            // 字幕额外的命名信息,指 Emby 字幕命名格式(简英,subhd),的 subhd
-	SHA1         string `json:"sha_1"`                     // 当前文件的 sha1 的值
-	IsSend       bool   `json:"is_send"`                   // 是否已经发送
-	IMDBInfoID   string
-}
-
-func NewVideoSubInfo(feature string, subName string, languageISO string, isDouble bool, chineseISO string, myLanguage string, storeFPath string, extraPreName string, sha1String string) *VideoSubInfo {
-	return &VideoSubInfo{Feature: feature, SubName: subName, LanguageISO: languageISO, IsDouble: isDouble, ChineseISO: chineseISO, MyLanguage: myLanguage, StoreRPath: storeFPath, ExtraPreName: extraPreName, SHA1: sha1String}
-}

+ 7 - 7
internal/pkg/imdb_helper/imdb.go

@@ -4,10 +4,10 @@ import (
 	"crypto/tls"
 	"github.com/StalkR/imdb"
 	"github.com/allanpk716/ChineseSubFinder/internal/dao"
-	"github.com/allanpk716/ChineseSubFinder/internal/models"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
+	gModels "github.com/allanpk716/ChineseSubModels/models"
 	"net/http"
 	"net/url"
 	"strings"
@@ -51,7 +51,7 @@ func GetVideoInfoFromIMDB(imdbID string, _proxySettings ...settings.ProxySetting
 }
 
 // GetVideoIMDBInfoFromLocal 从本地获取 IMDB 信息,如果找不到则去网络获取并写入本地缓存
-func GetVideoIMDBInfoFromLocal(imdbID string, _proxySettings ...settings.ProxySettings) (*models.IMDBInfo, error) {
+func GetVideoIMDBInfoFromLocal(imdbID string, _proxySettings ...settings.ProxySettings) (*gModels.IMDBInfo, error) {
 
 	var proxySettings settings.ProxySettings
 	if len(_proxySettings) > 0 {
@@ -61,9 +61,9 @@ func GetVideoIMDBInfoFromLocal(imdbID string, _proxySettings ...settings.ProxySe
 	log_helper.GetLogger().Debugln("GetVideoIMDBInfoFromLocal", 0)
 
 	// 首先从数据库中查找是否存在这个 IMDB 信息,如果不存在再使用 Web 查找,且写入数据库
-	var imdbInfos []models.IMDBInfo
+	var imdbInfos []gModels.IMDBInfo
 	// 把嵌套关联的 has many 的信息都查询出来
-	dao.GetDb().Preload("VideoSubInfos").Limit(1).Where(&models.IMDBInfo{IMDBID: imdbID}).Find(&imdbInfos)
+	dao.GetDb().Preload("VideoSubInfos").Limit(1).Where(&gModels.IMDBInfo{IMDBID: imdbID}).Find(&imdbInfos)
 
 	log_helper.GetLogger().Debugln("GetVideoIMDBInfoFromLocal", 1)
 
@@ -76,8 +76,8 @@ func GetVideoIMDBInfoFromLocal(imdbID string, _proxySettings ...settings.ProxySe
 		log_helper.GetLogger().Debugln("GetVideoIMDBInfoFromLocal", 2)
 
 		// 存入数据库
-		nowIMDBInfo := models.NewIMDBInfo(imdbID, t.Name, t.Year, t.Description, t.Languages, t.AKA)
-		imdbInfos = make([]models.IMDBInfo, 0)
+		nowIMDBInfo := gModels.NewIMDBInfo(imdbID, t.Name, t.Year, t.Description, t.Languages, t.AKA)
+		imdbInfos = make([]gModels.IMDBInfo, 0)
 		imdbInfos = append(imdbInfos, *nowIMDBInfo)
 		dao.GetDb().Create(nowIMDBInfo)
 
@@ -93,7 +93,7 @@ func GetVideoIMDBInfoFromLocal(imdbID string, _proxySettings ...settings.ProxySe
 }
 
 // IsChineseVideo 从 imdbID 去查询判断是否是中文视频
-func IsChineseVideo(imdbID string, _proxySettings ...settings.ProxySettings) (bool, *models.IMDBInfo, error) {
+func IsChineseVideo(imdbID string, _proxySettings ...settings.ProxySettings) (bool, *gModels.IMDBInfo, error) {
 
 	const chName0 = "chinese"
 	const chName1 = "mandarin"