Skip to content

Commit

Permalink
Make yify subs more flexible
Browse files Browse the repository at this point in the history
Use the levenshtein distance to find the subtitle to download.
  • Loading branch information
gregdel authored and PouuleT committed Feb 8, 2024
1 parent f315fa0 commit e30803d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions modules/yifysubtitles/yifysubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"path/filepath"

"github.com/agnivade/levenshtein"
polochon "github.com/odwrtw/polochon/lib"
"github.com/odwrtw/yifysubs"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -91,16 +92,25 @@ func (y *YifySubs) getMovieSubtitle(m *polochon.Movie, lang polochon.Language, l
return nil, err
}

var selected *yifysubs.Subtitle
minScore := 1000

release := filepath.Base(m.PathWithoutExt())
for _, sub := range subs {
for _, subRelease := range sub.Releases {
if release == subRelease {
return sub, nil
dist := levenshtein.ComputeDistance(release, subRelease)
if dist < minScore {
selected = sub
minScore = dist
}
}
}

return nil, polochon.ErrNoSubtitleFound
if selected == nil {
return nil, polochon.ErrNoSubtitleFound
}

return selected, nil
}

// GetMovieSubtitle will get a movie subtitle
Expand Down

0 comments on commit e30803d

Please sign in to comment.