Skip to content

Commit

Permalink
feat: retry subtitles multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
tympanix committed Jul 9, 2018
1 parent 65ae58e commit b2c5e8f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 10 additions & 5 deletions app/app_subtitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func (a *Application) DownloadSubtitles(input types.LocalMediaList, lang set.Int
}

if !a.Config().Dry() {
sub, err := a.downloadBestSubtitle(ctx, item, langsubs, c)
rated := langsubs.RateByMedia(item, a.Config().Evaluator())
sub, err := a.downloadBestSubtitle(ctx, item, rated, 3, c)
if err != nil {
if a.Config().Strict() {
return nil, err
Expand All @@ -116,12 +117,11 @@ func (a *Application) DownloadSubtitles(input types.LocalMediaList, lang set.Int
return result, nil
}

func (a *Application) downloadBestSubtitle(ctx notify.Context, m types.Video, l types.SubtitleList, c chan<- *notify.Entry) (types.LocalSubtitle, error) {
rated := l.RateByMedia(m, a.Config().Evaluator())
if rated.Len() == 0 {
func (a *Application) downloadBestSubtitle(ctx notify.Context, m types.Video, l types.RatedSubtitleList, retries int, c chan<- *notify.Entry) (types.LocalSubtitle, error) {
if l.Len() == 0 {
return nil, ctx.Warn("No subtitles satisfied media")
}
sub := rated.Best()
sub := l.Best()
if sub.Score() < (float32(a.Config().Score()) / 100.0) {
return nil, ctx.Warn("Score too low %.0f%%", sub.Score()*100.0)
}
Expand All @@ -130,6 +130,11 @@ func (a *Application) downloadBestSubtitle(ctx notify.Context, m types.Video, l
ctx.Fatal("Subtitle could not be cast to online subtitle")
}
srt, err := onl.Download()
if err != nil && retries > 0 {
c <- ctx.WithError(err).Debug("Retrying subtitle")
p := list.RatedSubtitles(l.List()[1:])
return a.downloadBestSubtitle(ctx, m, p, retries-1, c)
}
if err != nil {
return nil, ctx.Error(err.Error())
}
Expand Down
6 changes: 1 addition & 5 deletions list/subtitle_rated_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ type RatedSubtitles []types.RatedSubtitle

// List returns the list of subtitles as a slice
func (s RatedSubtitles) List() []types.RatedSubtitle {
subs := make([]types.RatedSubtitle, len(s))
for i, v := range s {
subs[i] = v
}
return subs
return s
}

// Best returns the best matching subtitle
Expand Down

0 comments on commit b2c5e8f

Please sign in to comment.