feat: add description field to allanime provider show
This commit is contained in:
@@ -1,17 +1,22 @@
|
|||||||
package allanime
|
package allanime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
stdhtml "html"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProviderShow struct {
|
type ProviderShow struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
EnglishName string
|
EnglishName string
|
||||||
|
Description string
|
||||||
MalID int
|
MalID int
|
||||||
Status string
|
Status string
|
||||||
Thumbnail string
|
Thumbnail string
|
||||||
@@ -43,7 +48,7 @@ func (c *AllAnimeProvider) DirectSequels(ctx context.Context, show ProviderShow)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *AllAnimeProvider) GetProviderShow(ctx context.Context, showID string) (ProviderShow, error) {
|
func (c *AllAnimeProvider) GetProviderShow(ctx context.Context, showID string) (ProviderShow, error) {
|
||||||
query := `query($showId: String!) { show(_id: $showId) { _id name englishName malId status thumbnail type season episodeCount availableEpisodesDetail } }`
|
query := `query($showId: String!) { show(_id: $showId) { _id name englishName description malId status thumbnail type season episodeCount availableEpisodesDetail } }`
|
||||||
result, err := c.graphqlRequest(ctx, query, map[string]any{"showId": showID})
|
result, err := c.graphqlRequest(ctx, query, map[string]any{"showId": showID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ProviderShow{}, err
|
return ProviderShow{}, err
|
||||||
@@ -58,7 +63,7 @@ func (c *AllAnimeProvider) GetProviderShow(ctx context.Context, showID string) (
|
|||||||
|
|
||||||
func (c *AllAnimeProvider) SeasonalShows(ctx context.Context, season string, year int) ([]ProviderShow, error) {
|
func (c *AllAnimeProvider) SeasonalShows(ctx context.Context, season string, year int) ([]ProviderShow, error) {
|
||||||
const pageSize = 40
|
const pageSize = 40
|
||||||
query := `query($search: SearchInput, $page: Int) { shows(search: $search, limit: 40, page: $page, countryOrigin: ALL) { edges { _id name englishName malId status thumbnail type season episodeCount availableEpisodesDetail } } }`
|
query := `query($search: SearchInput, $page: Int) { shows(search: $search, limit: 40, page: $page, countryOrigin: ALL) { edges { _id name englishName description malId status thumbnail type season episodeCount availableEpisodesDetail } } }`
|
||||||
if season == "" {
|
if season == "" {
|
||||||
return nil, errors.New("allanime: season is required")
|
return nil, errors.New("allanime: season is required")
|
||||||
}
|
}
|
||||||
@@ -103,6 +108,7 @@ func providerShowFrom(raw map[string]any) ProviderShow {
|
|||||||
ID: stringValue(raw["_id"]),
|
ID: stringValue(raw["_id"]),
|
||||||
Name: stringValue(raw["name"]),
|
Name: stringValue(raw["name"]),
|
||||||
EnglishName: stringValue(raw["englishName"]),
|
EnglishName: stringValue(raw["englishName"]),
|
||||||
|
Description: plainText(stringValue(raw["description"])),
|
||||||
MalID: intValue(raw["malId"]),
|
MalID: intValue(raw["malId"]),
|
||||||
Status: stringValue(raw["status"]),
|
Status: stringValue(raw["status"]),
|
||||||
Thumbnail: stringValue(raw["thumbnail"]),
|
Thumbnail: stringValue(raw["thumbnail"]),
|
||||||
@@ -114,6 +120,26 @@ func providerShowFrom(raw map[string]any) ProviderShow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func plainText(value string) string {
|
||||||
|
tokenizer := html.NewTokenizer(bytes.NewBufferString(value))
|
||||||
|
var out strings.Builder
|
||||||
|
for {
|
||||||
|
switch tokenizer.Next() {
|
||||||
|
case html.ErrorToken:
|
||||||
|
return out.String()
|
||||||
|
case html.TextToken:
|
||||||
|
text := strings.Join(strings.Fields(stdhtml.UnescapeString(string(tokenizer.Text()))), " ")
|
||||||
|
if text == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if out.Len() > 0 {
|
||||||
|
out.WriteByte(' ')
|
||||||
|
}
|
||||||
|
out.WriteString(text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func stringValue(value any) string {
|
func stringValue(value any) string {
|
||||||
valueString, _ := value.(string)
|
valueString, _ := value.(string)
|
||||||
return valueString
|
return valueString
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func TestDirectSequelsReturnsOnlyPlayableSequels(t *testing.T) {
|
|||||||
func TestGetProviderShowKeepsOnlyPositiveIntegerEpisodes(t *testing.T) {
|
func TestGetProviderShowKeepsOnlyPositiveIntegerEpisodes(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
_, _ = w.Write([]byte(`{"data":{"show":{"_id":"season-2","name":"Example Season 2","englishName":"Example Season 2","malId":"42","status":"Finished","episodeCount":"2","availableEpisodesDetail":{"sub":["2","1","0","1.5"],"dub":["1"],"raw":[]}}}}`))
|
_, _ = w.Write([]byte(`{"data":{"show":{"_id":"season-2","name":"Example Season 2","englishName":"Example Season 2","description":"A useful<br><i>synopsis</i> & summary.","malId":"42","status":"Finished","episodeCount":"2","availableEpisodesDetail":{"sub":["2","1","0","1.5"],"dub":["1"],"raw":[]}}}}`))
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
@@ -50,6 +50,9 @@ func TestGetProviderShowKeepsOnlyPositiveIntegerEpisodes(t *testing.T) {
|
|||||||
if got.MalID != 42 || len(got.SubEpisodes) != 2 || len(got.DubEpisodes) != 1 {
|
if got.MalID != 42 || len(got.SubEpisodes) != 2 || len(got.DubEpisodes) != 1 {
|
||||||
t.Fatalf("GetProviderShow = %+v", got)
|
t.Fatalf("GetProviderShow = %+v", got)
|
||||||
}
|
}
|
||||||
|
if got.Description != "A useful synopsis & summary." {
|
||||||
|
t.Fatalf("Description = %q", got.Description)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSeasonalShowsReturnsPlayableTVAnime(t *testing.T) {
|
func TestSeasonalShowsReturnsPlayableTVAnime(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user