fix: improve video title matching and segments null error
This commit is contained in:
@@ -136,17 +136,35 @@ func (c *AllAnimeProvider) Search(ctx context.Context, query string, mode string
|
|||||||
|
|
||||||
func (c *AllAnimeProvider) GetStreams(ctx context.Context, animeID int, titleCandidates []string, episode string, mode string) (*domain.StreamResult, error) {
|
func (c *AllAnimeProvider) GetStreams(ctx context.Context, animeID int, titleCandidates []string, episode string, mode string) (*domain.StreamResult, error) {
|
||||||
// 1. Search for the show to get its AllAnime ID
|
// 1. Search for the show to get its AllAnime ID
|
||||||
// Try each title candidate until we find a match, matching main's approach
|
// Try each title candidate, preferring results with matching malId
|
||||||
|
targetMalIDStr := strconv.Itoa(animeID)
|
||||||
var showID string
|
var showID string
|
||||||
|
var firstAvailableShowID string
|
||||||
|
|
||||||
for _, title := range titleCandidates {
|
for _, title := range titleCandidates {
|
||||||
searchResults, err := c.Search(ctx, title, mode)
|
searchResults, err := c.Search(ctx, title, mode)
|
||||||
if err != nil || len(searchResults) == 0 {
|
if err != nil || len(searchResults) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
showID = searchResults[0].ID
|
|
||||||
|
for _, res := range searchResults {
|
||||||
|
if res.MalID == targetMalIDStr {
|
||||||
|
showID = res.ID
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if showID != "" {
|
if showID != "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if firstAvailableShowID == "" {
|
||||||
|
firstAvailableShowID = searchResults[0].ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if showID == "" {
|
||||||
|
showID = firstAvailableShowID
|
||||||
}
|
}
|
||||||
|
|
||||||
if showID == "" {
|
if showID == "" {
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ func (s *playbackService) BuildWatchData(ctx context.Context, animeID int, title
|
|||||||
if anime.TitleJapanese != "" {
|
if anime.TitleJapanese != "" {
|
||||||
searchTitles = append(searchTitles, anime.TitleJapanese)
|
searchTitles = append(searchTitles, anime.TitleJapanese)
|
||||||
}
|
}
|
||||||
|
for _, syn := range anime.TitleSynonyms {
|
||||||
|
if syn != "" && syn != anime.Title && syn != anime.TitleEnglish && syn != anime.TitleJapanese {
|
||||||
|
searchTitles = append(searchTitles, syn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var result *domain.StreamResult
|
var result *domain.StreamResult
|
||||||
for _, p := range s.providers {
|
for _, p := range s.providers {
|
||||||
@@ -299,29 +304,29 @@ func (s *playbackService) SaveProgress(ctx context.Context, userID string, anime
|
|||||||
|
|
||||||
func (s *playbackService) fetchSkipSegments(ctx context.Context, malID int, episode string) []SkipSegment {
|
func (s *playbackService) fetchSkipSegments(ctx context.Context, malID int, episode string) []SkipSegment {
|
||||||
if malID <= 0 || strings.TrimSpace(episode) == "" {
|
if malID <= 0 || strings.TrimSpace(episode) == "" {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint := fmt.Sprintf("https://api.aniskip.com/v1/skip-times/%s/%s?types=op&types=ed", url.PathEscape(strconv.Itoa(malID)), url.PathEscape(episode))
|
endpoint := fmt.Sprintf("https://api.aniskip.com/v1/skip-times/%s/%s?types=op&types=ed", url.PathEscape(strconv.Itoa(malID)), url.PathEscape(episode))
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Mozilla/5.0")
|
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||||
|
|
||||||
resp, err := s.httpClient.Do(req)
|
resp, err := s.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
defer func() { _ = resp.Body.Close() }()
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024))
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultItem struct {
|
type resultItem struct {
|
||||||
@@ -338,11 +343,11 @@ func (s *playbackService) fetchSkipSegments(ctx context.Context, malID int, epis
|
|||||||
|
|
||||||
var parsed apiResponse
|
var parsed apiResponse
|
||||||
if err := json.Unmarshal(body, &parsed); err != nil {
|
if err := json.Unmarshal(body, &parsed); err != nil {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !parsed.Found || len(parsed.Result) == 0 {
|
if !parsed.Found || len(parsed.Result) == 0 {
|
||||||
return nil
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
segments := make([]SkipSegment, 0, len(parsed.Result))
|
segments := make([]SkipSegment, 0, len(parsed.Result))
|
||||||
|
|||||||
Reference in New Issue
Block a user