feat: fetch episode titles from allanime provider
This commit is contained in:
@@ -12,6 +12,7 @@ type AvailableEpisodes struct {
|
||||
Sub []string
|
||||
Dub []string
|
||||
Raw []string
|
||||
Titles map[int]string
|
||||
}
|
||||
|
||||
func (c *AllAnimeProvider) GetEpisodeAvailability(ctx context.Context, animeID int, titleCandidates []string) (domain.EpisodeAvailability, error) {
|
||||
@@ -30,18 +31,25 @@ func (c *AllAnimeProvider) GetEpisodeAvailabilityByProviderID(ctx context.Contex
|
||||
|
||||
sub := episodeNums(append(available.Sub, available.Raw...))
|
||||
dub := episodeNums(available.Dub)
|
||||
return domain.EpisodeAvailability{Sub: sub, Dub: dub}, nil
|
||||
return domain.EpisodeAvailability{Sub: sub, Dub: dub, Titles: available.Titles}, nil
|
||||
}
|
||||
|
||||
func (c *AllAnimeProvider) GetAvailableEpisodes(ctx context.Context, showID string) (AvailableEpisodes, error) {
|
||||
graphqlQuery := `query($showId: String!) {
|
||||
graphqlQuery := `query($showId: String!, $start: Float!, $end: Float!) {
|
||||
show(_id: $showId) {
|
||||
availableEpisodesDetail
|
||||
lastEpisodeInfo
|
||||
}
|
||||
episodeInfos(showId: $showId, episodeNumStart: $start, episodeNumEnd: $end) {
|
||||
episodeIdNum
|
||||
notes
|
||||
}
|
||||
}`
|
||||
|
||||
result, err := c.graphqlRequest(ctx, graphqlQuery, map[string]any{"showId": showID})
|
||||
result, err := c.graphqlRequest(ctx, graphqlQuery, map[string]any{
|
||||
"showId": showID,
|
||||
"start": 1,
|
||||
"end": 100000,
|
||||
})
|
||||
if err != nil {
|
||||
return AvailableEpisodes{}, err
|
||||
}
|
||||
@@ -61,10 +69,25 @@ func (c *AllAnimeProvider) GetAvailableEpisodes(ctx context.Context, showID stri
|
||||
return AvailableEpisodes{}, errors.New("invalid detail")
|
||||
}
|
||||
|
||||
titles := map[int]string{}
|
||||
infos, _ := data["episodeInfos"].([]any)
|
||||
for _, value := range infos {
|
||||
info, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
number := numericInt(info["episodeIdNum"])
|
||||
title := plainText(stringValue(info["notes"]))
|
||||
if number > 0 && title != "" {
|
||||
titles[number] = title
|
||||
}
|
||||
}
|
||||
|
||||
return AvailableEpisodes{
|
||||
Sub: stringsFrom(detail["sub"]),
|
||||
Dub: stringsFrom(detail["dub"]),
|
||||
Raw: stringsFrom(detail["raw"]),
|
||||
Titles: titles,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -347,13 +347,15 @@ func TestGetAvailableEpisodes(t *testing.T) {
|
||||
body string
|
||||
wantSub int
|
||||
wantDub int
|
||||
wantTitle string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "sub and dub available",
|
||||
body: `{"data":{"show":{"availableEpisodesDetail":{"sub":["1","2","3"],"dub":["1"]},"lastEpisodeInfo":{}}}}`,
|
||||
body: `{"data":{"show":{"availableEpisodesDetail":{"sub":["1","2","3"],"dub":["1"]}},"episodeInfos":[{"episodeIdNum":1,"notes":"The Beginning"}]}}`,
|
||||
wantSub: 3,
|
||||
wantDub: 1,
|
||||
wantTitle: "The Beginning",
|
||||
},
|
||||
{
|
||||
name: "sub only",
|
||||
@@ -394,10 +396,18 @@ func TestGetAvailableEpisodes(t *testing.T) {
|
||||
if len(available.Dub) != tt.wantDub {
|
||||
t.Errorf("Dub count = %d, want %d", len(available.Dub), tt.wantDub)
|
||||
}
|
||||
assertEpisodeTitle(t, available, tt.wantTitle)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertEpisodeTitle(t *testing.T, available AvailableEpisodes, want string) {
|
||||
t.Helper()
|
||||
if want != "" && available.Titles[1] != want {
|
||||
t.Errorf("episode 1 title = %q, want %q", available.Titles[1], want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user