Skip to content

Commit

Permalink
Fix #19
Browse files Browse the repository at this point in the history
Fix downloading S8E10.

Add tolerance for if a subtitle segment URL returns a 502. If that's the case, simply write empty data.
  • Loading branch information
xypwn committed Aug 21, 2024
1 parent d4124b3 commit 182bf80
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/southpark/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"sort"
"strconv"
"strings"

"github.com/xypwn/southpark-downloader-ui/pkg/httputils"
"github.com/xypwn/southpark-downloader-ui/pkg/ioutils"
)

// Processes strings like METHOD=AES-128,URI="https://.../",IV=0xDEADBEEF
Expand Down Expand Up @@ -499,10 +502,33 @@ func DownloadEpisodeStream(
for segmentIndex-segmentOffset < len(stream.Subs.Segments) {
relSegIdx := segmentIndex - segmentOffset
seg := stream.Subs.Segments[relSegIdx]
data, err := httputils.GetBodyWithContext(ctx, seg.URL)
if err != nil {
return fmt.Errorf("download subtitle segment: %w", err)

var data []byte
if err := func() error {
resp, err := httputils.GetWithContext(ctx, seg.URL)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusBadGateway {
// HACK: A 502 happens on S8E10 for a part of the subtitles.
// In that case, just write empty subs.
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP error: %v", resp.Status)
} else {
var err error
data, err = io.ReadAll(ioutils.NewCtxReader(ctx, resp.Body))
if err != nil {
return fmt.Errorf("io.ReadAll: %w", err)
}
}

return nil
}(); err != nil {
return fmt.Errorf("download subtitle segment: get '%v': %w", seg.URL, err)
}

if err := subsCallback(data, relSegIdx); err != nil {
return fmt.Errorf("subsCallback: %w", err)
}
Expand Down

0 comments on commit 182bf80

Please sign in to comment.