Skip to content

Commit

Permalink
Fix(avbase): support new prefix formatted ID (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu authored Oct 13, 2023
1 parent b702d6c commit 34b677e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
24 changes: 22 additions & 2 deletions provider/avbase/avbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ func New() *AVBase {
}
}

func (ab *AVBase) NormalizeID(id string) string { return strings.ToUpper(id) }
func (ab *AVBase) NormalizeID(id string) string {
if !strings.Contains(id, ":") {
return strings.ToUpper(id)
}
ss := strings.SplitN(id, ":", 2)
prefix, workID := ss[0], ss[1]
return ab.JoinPrefixID(prefix, workID)
}

func (ab *AVBase) GetMovieInfoByID(id string) (info *model.MovieInfo, err error) {
return ab.GetMovieInfoByURL(fmt.Sprintf(movieURL, id))
Expand Down Expand Up @@ -127,6 +134,10 @@ func (ab *AVBase) GetMovieInfoByURL(rawURL string) (info *model.MovieInfo, err e
if len(workInfo.Actors) > 0 {
info.Actors = workInfo.Actors
}
// choose right ID for info.
if len(workInfo.ID) > len(id) && strings.Contains(workInfo.ID, ":") {
id = workInfo.ID
}
}
})

Expand All @@ -147,6 +158,7 @@ func (ab *AVBase) GetMovieInfoByURL(rawURL string) (info *model.MovieInfo, err e

func (ab *AVBase) getMovieInfoFromWork(work Work) (info *model.MovieInfo, err error) {
info = &model.MovieInfo{
ID: ab.JoinPrefixID(work.Prefix, work.WorkID),
Number: work.WorkID,
Actors: []string{},
PreviewImages: []string{},
Expand Down Expand Up @@ -258,7 +270,7 @@ func (ab *AVBase) SearchMovie(keyword string) (results []*model.MovieSearchResul
continue
}
result := &model.MovieSearchResult{
ID: work.WorkID,
ID: ab.JoinPrefixID(work.Prefix, work.WorkID),
Number: work.WorkID,
Title: work.Title,
Provider: ab.Name(),
Expand All @@ -279,6 +291,13 @@ func (ab *AVBase) SearchMovie(keyword string) (results []*model.MovieSearchResul
return
}

func (ab *AVBase) JoinPrefixID(prefix, workID string) string {
if strings.TrimSpace(prefix) == "" {
return workID
}
return fmt.Sprintf("%s:%s", prefix, workID)
}

func (ab *AVBase) GetBuildID() (string, error) {
v, err, _ := ab.single.Do(func() (any, error) {
return ab.getBuildID()
Expand Down Expand Up @@ -315,6 +334,7 @@ func (ab *AVBase) getBuildID() (buildID string, err error) {

type Work struct {
ID int `json:"id"`
Prefix string `json:"prefix"`
WorkID string `json:"work_id"`
Title string `json:"title"`
MinDate string `json:"min_date"`
Expand Down
13 changes: 5 additions & 8 deletions provider/avbase/avbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
func TestAVBase_GetMovieInfoByID(t *testing.T) {
provider := New()
for _, item := range []string{
"ABP-588",
"PGD-919",
"ORECO-062",
"RECEN-012",
"DDH-079",
"prestige:ABP-588",
"tameike:MEYD-856",
"SSIS-354",
} {
info, err := provider.GetMovieInfoByID(item)
data, _ := json.MarshalIndent(info, "", "\t")
Expand All @@ -27,9 +25,8 @@ func TestAVBase_SearchMovie(t *testing.T) {
provider := New()
for _, item := range []string{
"ABP-588",
"ORECO-062",
"AKDL-030",
"SABA-099",
"MEYD-856",
"SSIS-354",
} {
results, err := provider.SearchMovie(provider.NormalizeKeyword(item))
data, _ := json.MarshalIndent(results, "", "\t")
Expand Down

0 comments on commit 34b677e

Please sign in to comment.