Skip to content

Commit

Permalink
refactor: file name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxsen committed Aug 19, 2024
1 parent 0e74c38 commit 3e86a33
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 26 deletions.
15 changes: 7 additions & 8 deletions capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func (c *Capture) resolveFileInfo(fc *model.FileContext, file string) error {
return fmt.Errorf("parse number failed, err:%w", err)
}
fc.Number = info
fc.SaveFileBase = fc.Number.GetNumber()
fc.SaveFileBase = fc.Number.GenerateSuffix(fc.SaveFileBase)
fc.SaveFileBase = fc.Number.GenerateFileName()
return nil
}

Expand Down Expand Up @@ -118,7 +117,7 @@ func (c *Capture) displayNumberInfo(ctx context.Context, fcs []*model.FileContex
logutil.GetLogger(ctx).Info("read movie file succ", zap.Int("count", len(fcs)))
for _, item := range fcs {
logutil.GetLogger(ctx).Info("file info",
zap.String("number", item.Number.GetNumber()),
zap.String("number", item.Number.GetNumberID()),
zap.Bool("multi_cd", item.Number.GetIsMultiCD()),
zap.Int("cd", item.Number.GetMultiCDIndex()), zap.String("file", item.FileName))
}
Expand Down Expand Up @@ -151,7 +150,7 @@ func (c *Capture) resolveSaveDir(fc *model.FileContext) error {
naming = strings.ReplaceAll(naming, NamingReleaseYear, year)
naming = strings.ReplaceAll(naming, NamingReleaseMonth, month)
naming = strings.ReplaceAll(naming, NamingActor, actor)
naming = strings.ReplaceAll(naming, NamingNumber, fc.Number.GetNumber())
naming = strings.ReplaceAll(naming, NamingNumber, fc.Number.GetNumberID())
if len(naming) == 0 {
return fmt.Errorf("invalid naming")
}
Expand All @@ -162,13 +161,13 @@ func (c *Capture) resolveSaveDir(fc *model.FileContext) error {
func (c *Capture) doSearch(ctx context.Context, fc *model.FileContext) error {
meta, ok, err := c.c.Searcher.Search(ctx, fc.Number)
if err != nil {
return fmt.Errorf("search number failed, number:%s, err:%w", fc.Number.GetNumber(), err)
return fmt.Errorf("search number failed, number:%s, err:%w", fc.Number.GetNumberID(), err)
}
if !ok {
return fmt.Errorf("search item not found")
}
if meta.Number != fc.Number.GetNumber() {
logutil.GetLogger(ctx).Warn("number not match, may be re-generated, ignore", zap.String("search", meta.Number), zap.String("file", fc.Number.GetNumber()))
if meta.Number != fc.Number.GetNumberID() {
logutil.GetLogger(ctx).Warn("number not match, may be re-generated, ignore", zap.String("search", meta.Number), zap.String("file", fc.Number.GetNumberID()))
}
fc.Meta = meta
return nil
Expand Down Expand Up @@ -236,7 +235,7 @@ func (c *Capture) doMetaVerify(ctx context.Context, fc *model.FileContext) error
}

func (c *Capture) processOneFile(ctx context.Context, fc *model.FileContext) error {
ctx = trace.WithTraceId(ctx, "TID:N:"+fc.Number.GetNumber())
ctx = trace.WithTraceId(ctx, "TID:N:"+fc.Number.GetNumberID())
steps := []struct {
name string
fn fcProcessFunc
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func defaultConfig() *Config {
"duration_fixer",
"translater",
},
LogConfig: logger.LogConfig{
Level: "info",
Console: true,
},
}
}

Expand Down
6 changes: 5 additions & 1 deletion number/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Number struct {
isLeak bool
}

func (n *Number) GetNumber() string {
func (n *Number) GetNumberID() string {
return n.number
}

Expand Down Expand Up @@ -55,3 +55,7 @@ func (n *Number) GenerateSuffix(base string) string {
}
return base
}

func (n *Number) GenerateFileName() string {
return n.GenerateSuffix(n.GetNumberID())
}
2 changes: 1 addition & 1 deletion number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestNumber(t *testing.T) {
for file, info := range checkList {
rs, err := ParseWithFileName(file)
assert.NoError(t, err)
assert.Equal(t, info.GetNumber(), rs.GetNumber())
assert.Equal(t, info.GetNumberID(), rs.GetNumberID())
assert.Equal(t, info.GetIsChineseSubtitle(), rs.GetIsChineseSubtitle())
assert.Equal(t, info.GetIsMultiCD(), rs.GetIsMultiCD())
assert.Equal(t, info.GetMultiCDIndex(), rs.GetMultiCDIndex())
Expand Down
6 changes: 3 additions & 3 deletions searcher/plugin/18av.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type av18 struct {
}

func (p *av18) OnPrecheckRequest(ctx *PluginContext, n *number.Number) (bool, error) {
return number.IsFc2(n.GetNumber()), nil
return number.IsFc2(n.GetNumberID()), nil
}

func (p *av18) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
uri := fmt.Sprintf("https://18av.me/cn/search.php?kw_type=key&kw=%s", number.GetNumber())
ctx.SetKey("number", number.GetNumber())
uri := fmt.Sprintf("https://18av.me/cn/search.php?kw_type=key&kw=%s", number.GetNumberID())
ctx.SetKey("number", number.GetNumberID())
return http.NewRequest(http.MethodGet, uri, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion searcher/plugin/airav/airav.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type airav struct {
}

func (p *airav) OnMakeHTTPRequest(ctx *plugin.PluginContext, number *number.Number) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://www.airav.wiki/api/video/barcode/%s?lng=zh-TW", number.GetNumber()), nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://www.airav.wiki/api/video/barcode/%s?lng=zh-TW", number.GetNumberID()), nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion searcher/plugin/avsox.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p *avsox) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*h

func (p *avsox) OnHandleHTTPRequest(ctx *PluginContext, invoker HTTPInvoker, _ *http.Request) (*http.Response, error) {
number := ctx.GetKeyOrDefault("number_info", nil).(*number.Number)
num := strings.ToUpper(number.GetNumber())
num := strings.ToUpper(number.GetNumberID())
if strings.Contains(num, "FC2") && !strings.Contains(num, "FC2-PPV") {
num = strings.ReplaceAll(num, "FC2", "FC2-PPV")
}
Expand Down
4 changes: 2 additions & 2 deletions searcher/plugin/caribpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type caribpr struct {
}

func (p *caribpr) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
ctx.SetKey("number", number.GetNumber())
uri := fmt.Sprintf("https://www.caribbeancompr.com/moviepages/%s/index.html", number.GetNumber())
ctx.SetKey("number", number.GetNumberID())
uri := fmt.Sprintf("https://www.caribbeancompr.com/moviepages/%s/index.html", number.GetNumberID())
req, err := http.NewRequest(http.MethodGet, uri, nil)
return req, err
}
Expand Down
4 changes: 2 additions & 2 deletions searcher/plugin/fc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type fc2 struct {
}

func (p *fc2) OnPrecheckRequest(ctx *PluginContext, n *number.Number) (bool, error) {
return number.IsFc2(n.GetNumber()), nil
return number.IsFc2(n.GetNumberID()), nil
}

func (p *fc2) OnMakeHTTPRequest(ctx *PluginContext, n *number.Number) (*http.Request, error) {
number := strings.ToLower(n.GetNumber())
number := strings.ToLower(n.GetNumberID())
res := defaultFc2NumberParser.FindStringSubmatch(number)
if len(res) != 2 {
return nil, fmt.Errorf("unabe to decode number")
Expand Down
4 changes: 2 additions & 2 deletions searcher/plugin/freejavbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type freejavbt struct {
}

func (p *freejavbt) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
uri := "https://freejavbt.com/zh/" + number.GetNumber()
ctx.SetKey("number", number.GetNumber())
uri := "https://freejavbt.com/zh/" + number.GetNumberID()
ctx.SetKey("number", number.GetNumberID())
return http.NewRequest(http.MethodGet, uri, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion searcher/plugin/jav321.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type jav321 struct {

func (p *jav321) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
data := url.Values{}
data.Set("sn", number.GetNumber())
data.Set("sn", number.GetNumberID())
body := data.Encode()
req, err := http.NewRequest(http.MethodPost, "https://www.jav321.com/search", strings.NewReader(body))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion searcher/plugin/javbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type javbus struct {
}

func (p *javbus) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
url := "https://www.javbus.com/" + number.GetNumber()
url := "https://www.javbus.com/" + number.GetNumberID()
return http.NewRequest(http.MethodGet, url, nil)
}

Expand Down
4 changes: 2 additions & 2 deletions searcher/plugin/javdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type javdb struct {
}

func (p *javdb) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
ctx.SetKey("number", number.GetNumber())
link := fmt.Sprintf("https://javdb.com/search?q=%s&f=all", number.GetNumber())
ctx.SetKey("number", number.GetNumberID())
link := fmt.Sprintf("https://javdb.com/search?q=%s&f=all", number.GetNumberID())
return http.NewRequest(http.MethodGet, link, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion searcher/plugin/javhoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type javhoo struct {
}

func (p *javhoo) OnMakeHTTPRequest(ctx *PluginContext, number *number.Number) (*http.Request, error) {
uri := fmt.Sprintf("https://www.javhoo.com/av/%s", number.GetNumber())
uri := fmt.Sprintf("https://www.javhoo.com/av/%s", number.GetNumberID())
return http.NewRequest(http.MethodGet, uri, nil)
}

Expand Down

0 comments on commit 3e86a33

Please sign in to comment.