Skip to content

Commit

Permalink
v0.10.3
Browse files Browse the repository at this point in the history
- 配置文件版本号为`1.6.1`
- 支持设置Mikan的Cookie
  • Loading branch information
wetor committed Nov 18, 2023
1 parent 9dc72ff commit 7328b34
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 94 deletions.
7 changes: 4 additions & 3 deletions cmd/animego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ func Main() {
},
BangumiCache: bangumiCache,
BangumiCacheLock: &BangumiCacheMutex,
RedirectMikan: config.Advanced.Redirect.Mikan,
RedirectBangumi: config.Advanced.Redirect.Bangumi,
RedirectThemoviedb: config.Advanced.Redirect.Themoviedb,
RedirectMikan: config.Advanced.AniData.Mikan.Redirect,
RedirectBangumi: config.Advanced.AniData.Bangumi.Redirect,
RedirectThemoviedb: config.Advanced.AniData.Themoviedb.Redirect,
MikanCookie: config.Advanced.AniData.Mikan.Cookie,
},
})

Expand Down
9 changes: 9 additions & 0 deletions configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ func TestUpdateConfig_160(t *testing.T) {
EqualFile(t, "data/animego.yaml", test.GetDataPath(testdata, "animego_160.yaml"))
}

func TestUpdateConfig_161(t *testing.T) {
configs.ConfigVersion = "1.6.1"
file, _ := test.GetData(testdata, "animego_160.yaml")
_ = os.WriteFile("data/animego.yaml", file, 0666)
configs.UpdateConfig("data/animego.yaml", false)

EqualFile(t, "data/animego.yaml", test.GetDataPath(testdata, "animego_161.yaml"))
}

func TestInitEnvConfig(t *testing.T) {
_ = os.Setenv("ANIMEGO_QBT_URL", "http://127.0.0.1:18080")
_ = os.Setenv("ANIMEGO_QBT_DOWNLOAD_PATH", "7766/download")
Expand Down
17 changes: 12 additions & 5 deletions configs/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ type Setting struct {
type Advanced struct {
RefreshSecond int `yaml:"refresh_second" json:"refresh_second" attr:"刷新间隔时间" comment_key:"refresh_second_help"`

Redirect struct {
Mikan string `yaml:"mikan" json:"mikan" attr:"默认mikanani.me"`
Bangumi string `yaml:"bangumi" json:"bangumi" attr:"默认api.bgm.tv"`
Themoviedb string `yaml:"themoviedb" json:"themoviedb" attr:"默认api.themoviedb.org"`
} `yaml:"redirect" json:"redirect" attr:"域名重定向"`
AniData struct {
Mikan struct {
Redirect string `yaml:"redirect" json:"redirect" attr:"默认mikanani.me"`
Cookie string `yaml:"cookie" json:"cookie" attr:"mikan的Cookie" comment:"使用登录后的Cookie可以正常下载mikan的被隐藏番剧. 登录状态的Cookie名为'.AspNetCore.Identity.Application'"`
} `yaml:"mikan" json:"mikan"`
Bangumi struct {
Redirect string `yaml:"redirect" json:"redirect" attr:"默认api.bgm.tv"`
} `yaml:"bangumi" json:"bangumi"`
Themoviedb struct {
Redirect string `yaml:"redirect" json:"redirect" attr:"默认api.themoviedb.org"`
} `yaml:"themoviedb" json:"themoviedb"`
} `yaml:"anidata" json:"anidata" attr:"资源网站设置"`

Request struct {
TimeoutSecond int `yaml:"timeout_second" json:"timeout_second" attr:"请求超时时间"`
Expand Down
47 changes: 46 additions & 1 deletion configs/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/jinzhu/copier"
"github.com/wetor/AnimeGo/configs/version/v_160"
"gopkg.in/yaml.v3"

"github.com/wetor/AnimeGo/assets"
Expand Down Expand Up @@ -49,6 +50,7 @@ var (
"1.5.1",
"1.5.2",
"1.6.0",
"1.6.1",
}
ConfigVersion = versions[len(versions)-1] // 当前配置文件版本

Expand Down Expand Up @@ -97,6 +99,11 @@ var (
Desc: "更改字段名,数据库迁移",
UpdateFunc: update_152_160,
},
{
Name: versions[9],
Desc: "更改域名重定向设置,支持设置Mikan的Cookie",
UpdateFunc: update_160_161,
},
}
)

Expand Down Expand Up @@ -485,7 +492,7 @@ func update_152_160(file string) {
log.Fatal("配置文件加载错误:", err)
}

newConfig := DefaultConfig()
newConfig := &v_160.Config{}
err = copier.Copy(newConfig, oldConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
Expand Down Expand Up @@ -563,6 +570,44 @@ func bolt2dirdb(boltPath, savePath string) {
}
}

func update_160_161(file string) {
data, err := os.ReadFile(file)
if err != nil {
log.Fatal("配置文件加载错误:", err)
}
oldConfig := &v_160.Config{}
err = yaml.Unmarshal(data, oldConfig)
if err != nil {
log.Fatal("配置文件加载错误:", err)
}

newConfig := DefaultConfig()
err = copier.Copy(newConfig, oldConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
newConfig.Version = "1.6.1"

log.Println("[变动] 配置项(advanced.redirect.mikan) 变更为 advanced.anidata.mikan.redirect")
log.Println("[新增] 配置项(advanced.anidata.mikan.cookie)")
newConfig.Advanced.AniData.Mikan.Redirect = oldConfig.Advanced.Redirect.Mikan
log.Println("[变动] 配置项(advanced.redirect.bangumi) 变更为 advanced.anidata.bangumi.redirect")
newConfig.Advanced.AniData.Bangumi.Redirect = oldConfig.Advanced.Redirect.Bangumi
log.Println("[变动] 配置项(advanced.redirect.themoviedb) 变更为 advanced.anidata.themoviedb.redirect")
newConfig.Advanced.AniData.Themoviedb.Redirect = oldConfig.Advanced.Redirect.Themoviedb

content, err := encodeConfig(newConfig)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
err = os.WriteFile(file, content, 0644)
if err != nil {
log.Fatal("配置文件升级失败:", err)
}
// 强制写入
assets.WritePlugins(assets.Dir, path.Join(newConfig.DataPath, assets.Dir), false)
}

func write(file string, data any) error {
f := dirdb.NewFile(file)
err := f.Open()
Expand Down
96 changes: 96 additions & 0 deletions configs/version/v_160/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package v_160

type Config struct {
Version string `yaml:"version" json:"version"`
Setting `yaml:"setting" json:"setting"`
Plugin `yaml:"plugin" json:"plugin"`
Advanced `yaml:"advanced" json:"advanced"`
}

type PluginInfo struct {
Enable bool `yaml:"enable" json:"enable"`
Type string `yaml:"type" json:"type"`
File string `yaml:"file" json:"file"`
Args map[string]any `yaml:"args,omitempty" json:"args,omitempty"`
Vars map[string]any `yaml:"vars,omitempty" json:"vars,omitempty"`
}

type Plugin struct {
Feed []PluginInfo `yaml:"feed" json:"feed"`
Parser []PluginInfo `yaml:"parser" json:"parser"`
Filter []PluginInfo `yaml:"filter" json:"filter"`
Schedule []PluginInfo `yaml:"schedule" json:"schedule"`
Rename []PluginInfo `yaml:"rename" json:"rename"`
}

type Setting struct {
Client struct {
QBittorrent struct {
Url string `yaml:"url" json:"url"`
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
DownloadPath string `yaml:"download_path" json:"download_path"`
} `yaml:"qbittorrent" json:"qbittorrent"`
} `yaml:"client" json:"client"`
DownloadPath string `yaml:"download_path" json:"download_path"`
SavePath string `yaml:"save_path" json:"save_path"`
DataPath string `yaml:"data_path" json:"data_path"`
Category string `yaml:"category" json:"category"`
Tag string `yaml:"tag" json:"tag"`
WebApi struct {
AccessKey string `yaml:"access_key" json:"access_key"`
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
} `yaml:"webapi" json:"webapi"`
Proxy struct {
Enable bool `yaml:"enable" json:"enable"`
Url string `yaml:"url" json:"url"`
} `yaml:"proxy" json:"proxy"`
Key struct {
Themoviedb string `yaml:"themoviedb" json:"themoviedb"`
} `yaml:"key" json:"key"`
}

type Advanced struct {
RefreshSecond int `yaml:"refresh_second" json:"refresh_second"`

Redirect struct {
Mikan string `yaml:"mikan" json:"mikan"`
Bangumi string `yaml:"bangumi" json:"bangumi"`
Themoviedb string `yaml:"themoviedb" json:"themoviedb"`
} `yaml:"redirect" json:"redirect"`

Request struct {
TimeoutSecond int `yaml:"timeout_second" json:"timeout_second"`
RetryNum int `yaml:"retry_num" json:"retry_num"`
RetryWaitSecond int `yaml:"retry_wait_second" json:"retry_wait_second"`
} `yaml:"request" json:"request"`

Download struct {
AllowDuplicateDownload bool `yaml:"allow_duplicate_download" json:"allow_duplicate_download"`
SeedingTimeMinute int `yaml:"seeding_time_minute" json:"seeding_time_minute"`
Rename string `yaml:"rename" json:"rename"`
} `yaml:"download" json:"download"`

Feed struct {
DelaySecond int `yaml:"delay_second" json:"delay_second"`
} `yaml:"feed" json:"feed"`

Default struct {
TMDBFailSkip bool `yaml:"tmdb_fail_skip" json:"tmdb_fail_skip"`
TMDBFailUseTitleSeason bool `yaml:"tmdb_fail_use_title_season" json:"tmdb_fail_use_title_season"`
TMDBFailUseFirstSeason bool `yaml:"tmdb_fail_use_first_season" json:"tmdb_fail_use_first_season"`
} `yaml:"default" json:"default"`

Client struct {
ConnectTimeoutSecond int `yaml:"connect_timeout_second" json:"connect_timeout_second"`
RetryConnectNum int `yaml:"retry_connect_num" json:"retry_connect_num"`
CheckTimeSecond int `yaml:"check_time_second" json:"check_time_second"`
} `yaml:"client" json:"client"`

Cache struct {
MikanCacheHour int `yaml:"mikan_cache_hour" json:"mikan_cache_hour"`
BangumiCacheHour int `yaml:"bangumi_cache_hour" json:"bangumi_cache_hour"`
ThemoviedbCacheHour int `yaml:"themoviedb_cache_hour" json:"themoviedb_cache_hour"`
} `yaml:"cache" json:"cache"`
}
6 changes: 6 additions & 0 deletions internal/animego/anidata/anidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var (
RedirectMikan string
RedirectBangumi string
RedirectThemoviedb string

MikanCookie string
)

type Options struct {
Expand All @@ -27,6 +29,8 @@ type Options struct {
RedirectMikan string
RedirectBangumi string
RedirectThemoviedb string

MikanCookie string
}

func Init(opts *Options) {
Expand All @@ -38,4 +42,6 @@ func Init(opts *Options) {
RedirectMikan = opts.RedirectMikan
RedirectBangumi = opts.RedirectBangumi
RedirectThemoviedb = opts.RedirectThemoviedb

MikanCookie = opts.MikanCookie
}
10 changes: 9 additions & 1 deletion internal/animego/anidata/mikan/mikan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
IdXPath = "//a[@class='mikan-rss']" // Mikan番剧id获取XPath
GroupXPath = "//p[@class='bangumi-info']/a[@class='magnet-link-wrap']" // Mikan番剧信息获取group字幕组id和name
BangumiUrlXPath = "//p[@class='bangumi-info']/a[contains(@href, 'bgm.tv')]" // Mikan番剧信息中bangumi id获取XPath

AuthCookie = ".AspNetCore.Identity.Application"
)

var (
Expand Down Expand Up @@ -134,7 +136,13 @@ func (a *Mikan) cacheParseMikanBangumiID(mikanID int) (bangumiID int, err error)

func (a *Mikan) loadHtml(url string) (*html.Node, error) {
buf := bytes.NewBuffer(nil)
err := request.GetWriter(url, buf)
cookie := anidata.MikanCookie
if !strings.Contains(cookie, AuthCookie+"=") {
cookie = AuthCookie + "=" + cookie
}
err := request.GetWriter(url, buf, map[string]string{
"Cookie": cookie,
})
if err != nil {
log.DebugErr(err)
return nil, errors.WithStack(&exceptions.ErrRequest{Name: a.Name()})
Expand Down
32 changes: 24 additions & 8 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,44 @@ func GetString(uri string, args ...interface{}) (string, error) {
return str, nil
}

func Get(uri string, body interface{}) error {
func Get(uri string, body interface{}, args ...interface{}) error {
ReInitWG.Add(1)
defer ReInitWG.Done()
resp, _, errs := request(uri, "GET", nil, nil).EndStruct(body)
var header map[string]string = nil
if len(args) > 0 {
header = args[0].(map[string]string)
}
resp, _, errs := request(uri, "GET", nil, header).EndStruct(body)
err := handleError(resp, errs)
if err != nil {
return err
}
return nil
}

func Post(uri string, req interface{}, body interface{}) error {
func Post(uri string, req interface{}, body interface{}, args ...interface{}) error {
ReInitWG.Add(1)
defer ReInitWG.Done()
resp, _, errs := request(uri, "POST", req, nil).EndStruct(body)
var header map[string]string = nil
if len(args) > 0 {
header = args[0].(map[string]string)
}
resp, _, errs := request(uri, "POST", req, header).EndStruct(body)
err := handleError(resp, errs)
if err != nil {
return err
}
return nil
}

func GetFile(uri string, file string) error {
func GetFile(uri string, file string, args ...interface{}) error {
ReInitWG.Add(1)
defer ReInitWG.Done()
resp, bodyBytes, errs := request(uri, "GET", nil, nil).EndBytes()
var header map[string]string = nil
if len(args) > 0 {
header = args[0].(map[string]string)
}
resp, bodyBytes, errs := request(uri, "GET", nil, header).EndBytes()
err := handleError(resp, errs)
if err != nil {
return err
Expand All @@ -164,10 +176,14 @@ func GetFile(uri string, file string) error {
return nil
}

func GetWriter(uri string, w io.Writer) error {
func GetWriter(uri string, w io.Writer, args ...interface{}) error {
ReInitWG.Add(1)
defer ReInitWG.Done()
resp, bodyBytes, errs := request(uri, "GET", nil, nil).EndBytes()
var header map[string]string = nil
if len(args) > 0 {
header = args[0].(map[string]string)
}
resp, bodyBytes, errs := request(uri, "GET", nil, header).EndBytes()
err := handleError(resp, errs)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 7328b34

Please sign in to comment.