Browse Source

完成初版

Signed-off-by: 716 <[email protected]>
716 4 years ago
parent
commit
6b74a0599f
9 changed files with 106 additions and 7 deletions
  1. 1 0
      .gitignore
  2. 9 0
      common/config.go
  3. 15 2
      config.go
  4. 2 3
      config.yaml.sample
  5. 1 1
      downloader.go
  6. 1 1
      downloader_test.go
  7. 1 0
      go.mod
  8. 2 0
      go.sum
  9. 74 0
      main.go

+ 1 - 0
.gitignore

@@ -7,3 +7,4 @@
 /tmpthings
 /model/result.png
 /sub_supplier/xunlei/Logs/*.log
+/config.yaml

+ 9 - 0
common/config.go

@@ -0,0 +1,9 @@
+package common
+
+type Config struct {
+	UseProxy bool
+	HttpProxy string
+	EveryTime string
+	DebugMode bool
+	MovieFolder string
+}

+ 15 - 2
config.go

@@ -1,8 +1,12 @@
-package ChineseSubFinder
+package main
 
-import "github.com/spf13/viper"
+import (
+	"github.com/allanpk716/ChineseSubFinder/common"
+	"github.com/spf13/viper"
+)
 import "errors"
 
+// InitConfigure 初始化配置文件实例
 func InitConfigure() (*viper.Viper, error) {
 	v := viper.New()
 	v.SetConfigName("config") // 设置文件名称(无后缀)
@@ -15,4 +19,13 @@ func InitConfigure() (*viper.Viper, error) {
 	}
 
 	return v, nil
+}
+// ReadConfig 读取配置文件
+func ReadConfig(viper *viper.Viper) (*common.Config, error) {
+	conf := &common.Config{}
+	err := viper.Unmarshal(conf)
+	if err != nil {
+		return nil, err
+	}
+	return conf, nil
 }

+ 2 - 3
config.yaml → config.yaml.sample

@@ -1,6 +1,5 @@
 UseProxy: false
 HttpProxy: http://127.0.0.1:10809
-EveryTime: 30m
+EveryTime: 6h
 DebugMode: false
-Movie:
-  - X:\电影\The Devil All the Time (2020)
+MovieFolder: X:\电影

+ 1 - 1
downloader.go

@@ -1,4 +1,4 @@
-package ChineseSubFinder
+package main
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/common"

+ 1 - 1
downloader_test.go

@@ -1,4 +1,4 @@
-package ChineseSubFinder
+package main
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/common"

+ 1 - 0
go.mod

@@ -17,6 +17,7 @@ require (
 	github.com/middelink/go-parse-torrent-name v0.0.0-20190301154245-3ff4efacd4c4
 	github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
 	github.com/pkg/errors v0.9.1 // indirect
+	github.com/robfig/cron/v3 v3.0.0 // indirect
 	github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca
 	github.com/sirupsen/logrus v1.8.1
 	github.com/spf13/viper v1.7.1

+ 2 - 0
go.sum

@@ -191,6 +191,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
 github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
 github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
+github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
+github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
 github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
 github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=

+ 74 - 0
main.go

@@ -0,0 +1,74 @@
+package main
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/common"
+	"github.com/allanpk716/ChineseSubFinder/model"
+	"github.com/robfig/cron/v3"
+	"github.com/sirupsen/logrus"
+	"github.com/spf13/viper"
+)
+
+func init() {
+	var err error
+	log = model.GetLogger()
+	configViper, err = InitConfigure()
+	if err != nil {
+		log.Errorln("InitConfigure", err)
+		return 
+	}
+	config, err = ReadConfig(configViper)
+	if err != nil {
+		log.Errorln("ReadConfig", err)
+		return 
+	}
+}
+
+func main() {
+	if log == nil {
+		panic("log init error")
+	}
+	if configViper == nil {
+		panic("init viper error")
+	}
+	if config == nil {
+		panic("read config error")
+	}
+	downloader := NewDownloader()
+
+	//任务还没执行完,下一次执行时间到来,下一次执行就跳过不执行
+	c := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger)))
+	// 定时器
+	entryID, err := c.AddFunc("@every " + config.EveryTime, func() {
+		// 开始下载
+		err := downloader.DownloadSub(config.MovieFolder)
+		if err != nil {
+			log.Errorln("DownloadSub", err)
+			return
+		}
+	})
+	if err != nil {
+		log.Errorln("cron entryID:", entryID, "Error:", err)
+		return
+	}
+	log.Infoln("First Time Download Start")
+	// 立即触发第一次的更新
+	// 开始下载
+	err = downloader.DownloadSub(config.MovieFolder)
+	if err != nil {
+		log.Errorln("DownloadSub", err)
+		return
+	}
+	log.Infoln("First Time Download End")
+
+	c.Start()
+
+	log.Infoln("Download Timer Started")
+	// 阻塞
+	select {}
+}
+
+var(
+	log         *logrus.Logger
+	configViper *viper.Viper
+	config      *common.Config
+)