refactor: shorten function names in allanime

This commit is contained in:
2026-06-23 17:38:13 +02:00
committed by Milas Holsting
parent 496aea9d0d
commit d55a9087eb
5 changed files with 45 additions and 41 deletions

View File

@@ -28,8 +28,8 @@ func (c *AllAnimeProvider) GetEpisodeAvailabilityByProviderID(ctx context.Contex
return domain.EpisodeAvailability{}, err return domain.EpisodeAvailability{}, err
} }
sub := parseEpisodeNumbers(append(available.Sub, available.Raw...)) sub := episodeNums(append(available.Sub, available.Raw...))
dub := parseEpisodeNumbers(available.Dub) dub := episodeNums(available.Dub)
return domain.EpisodeAvailability{Sub: sub, Dub: dub}, nil return domain.EpisodeAvailability{Sub: sub, Dub: dub}, nil
} }
@@ -62,13 +62,14 @@ func (c *AllAnimeProvider) GetAvailableEpisodes(ctx context.Context, showID stri
} }
return AvailableEpisodes{ return AvailableEpisodes{
Sub: stringSliceFromAny(detail["sub"]), Sub: stringsFrom(detail["sub"]),
Dub: stringSliceFromAny(detail["dub"]), Dub: stringsFrom(detail["dub"]),
Raw: stringSliceFromAny(detail["raw"]), Raw: stringsFrom(detail["raw"]),
}, nil }, nil
} }
func parseEpisodeNumbers(raw []string) []int { // episode ids
func episodeNums(raw []string) []int {
seen := make(map[int]bool, len(raw)) seen := make(map[int]bool, len(raw))
out := make([]int, 0, len(raw)) out := make([]int, 0, len(raw))
for _, value := range raw { for _, value := range raw {
@@ -82,7 +83,8 @@ func parseEpisodeNumbers(raw []string) []int {
return out return out
} }
func stringSliceFromAny(value any) []string { // graphql list
func stringsFrom(value any) []string {
items, ok := value.([]any) items, ok := value.([]any)
if !ok { if !ok {
return nil return nil

View File

@@ -6,10 +6,10 @@ import (
) )
func TestParseEpisodeNumbersKeepsOnlyPositiveIntegers(t *testing.T) { func TestParseEpisodeNumbersKeepsOnlyPositiveIntegers(t *testing.T) {
got := parseEpisodeNumbers([]string{"1", " 2 ", "2", "0", "-1", "12.5", "SP1", "6"}) got := episodeNums([]string{"1", " 2 ", "2", "0", "-1", "12.5", "SP1", "6"})
want := []int{1, 2, 6} want := []int{1, 2, 6}
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Fatalf("parseEpisodeNumbers() = %v, want %v", got, want) t.Fatalf("episodeNums() = %v, want %v", got, want)
} }
} }

View File

@@ -59,7 +59,7 @@ func runSourceReferenceTests(t *testing.T, tests []sourceReferencesTestCase) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel() t.Parallel()
got := buildSourceReferences(tt.rawURLs) got := sourceRefs(tt.rawURLs)
if len(got) != len(tt.wantRefs) { if len(got) != len(tt.wantRefs) {
t.Errorf("got %d refs, want %d", len(got), len(tt.wantRefs)) t.Errorf("got %d refs, want %d", len(got), len(tt.wantRefs))
return return
@@ -327,7 +327,7 @@ func TestBuildSourceReferencesOrder(t *testing.T) {
map[string]any{"sourceUrl": "https://yt.com/v.mp4", "sourceName": "yt-mp4"}, map[string]any{"sourceUrl": "https://yt.com/v.mp4", "sourceName": "yt-mp4"},
} }
got := buildSourceReferences(rawURLs) got := sourceRefs(rawURLs)
wantOrder := []string{"default", "yt-mp4", "s-mp4", "luf-mp4"} wantOrder := []string{"default", "yt-mp4", "s-mp4", "luf-mp4"}
if len(got) != len(wantOrder) { if len(got) != len(wantOrder) {

View File

@@ -108,23 +108,24 @@ func (e *providerExtractor) parseProviderResponse(ctx context.Context, response
return []StreamSource{} return []StreamSource{}
} }
data := collectProviderResponseData(root, e.referer) data := collectData(root, e.referer)
sources := buildProviderLinkSources(data.links, data.referer) sources := linkSources(data.links, data.referer)
sources = append(sources, e.buildProviderHLSSources(ctx, data.hls, data.referer)...) sources = append(sources, e.hlsSources(ctx, data.hls, data.referer)...)
attachSubtitles(sources, data.subtitles) attachSubtitles(sources, data.subtitles)
return sources return sources
} }
func collectProviderResponseData(root any, fallbackReferer string) providerResponseData { // provider payload
func collectData(root any, fallbackReferer string) providerResponseData {
data := providerResponseData{referer: fallbackReferer} data := providerResponseData{referer: fallbackReferer}
var walk func(v any) var walk func(v any)
walk = func(v any) { walk = func(v any) {
switch x := v.(type) { switch x := v.(type) {
case map[string]any: case map[string]any:
collectProviderMapData(x, &data) collectMapData(x, &data)
for _, child := range x { for _, child := range x {
walk(child) walk(child)
} }
@@ -143,7 +144,7 @@ func collectProviderResponseData(root any, fallbackReferer string) providerRespo
return data return data
} }
func collectProviderMapData(node map[string]any, data *providerResponseData) { func collectMapData(node map[string]any, data *providerResponseData) {
if ref, ok := node["Referer"].(string); ok { if ref, ok := node["Referer"].(string); ok {
if trimmedRef := strings.TrimSpace(ref); trimmedRef != "" { if trimmedRef := strings.TrimSpace(ref); trimmedRef != "" {
data.referer = trimmedRef data.referer = trimmedRef
@@ -163,11 +164,11 @@ func collectProviderMapData(node map[string]any, data *providerResponseData) {
} }
if subs, ok := node["subtitles"].([]any); ok { if subs, ok := node["subtitles"].([]any); ok {
data.subtitles = append(data.subtitles, parseProviderSubtitles(subs)...) data.subtitles = append(data.subtitles, parseSubtitles(subs)...)
} }
} }
func parseProviderSubtitles(items []any) []Subtitle { func parseSubtitles(items []any) []Subtitle {
subtitles := make([]Subtitle, 0, len(items)) subtitles := make([]Subtitle, 0, len(items))
for _, item := range items { for _, item := range items {
node, ok := item.(map[string]any) node, ok := item.(map[string]any)
@@ -195,7 +196,7 @@ func parseProviderSubtitles(items []any) []Subtitle {
return subtitles return subtitles
} }
func buildProviderLinkSources(items []providerLinkItem, referer string) []StreamSource { func linkSources(items []providerLinkItem, referer string) []StreamSource {
sources := make([]StreamSource, 0, len(items)) sources := make([]StreamSource, 0, len(items))
for _, item := range items { for _, item := range items {
link := strings.TrimSpace(item.link) link := strings.TrimSpace(item.link)
@@ -207,7 +208,7 @@ func buildProviderLinkSources(items []providerLinkItem, referer string) []Stream
URL: link, URL: link,
Quality: strings.TrimSpace(item.resolutionStr), Quality: strings.TrimSpace(item.resolutionStr),
Provider: "wixmp", Provider: "wixmp",
Type: detectProviderSourceType(link), Type: sourceType(link),
Referer: referer, Referer: referer,
}) })
} }
@@ -215,19 +216,19 @@ func buildProviderLinkSources(items []providerLinkItem, referer string) []Stream
return sources return sources
} }
func detectProviderSourceType(link string) string { func sourceType(link string) string {
sourceType := detectStreamType(link) typ := detectStreamType(link)
if sourceType != "unknown" { if typ != "unknown" {
return sourceType return typ
} }
return detectEmbedType(link) return detectEmbedType(link)
} }
func (e *providerExtractor) buildProviderHLSSources(ctx context.Context, items []providerHLSItem, referer string) []StreamSource { func (e *providerExtractor) hlsSources(ctx context.Context, items []providerHLSItem, referer string) []StreamSource {
sources := make([]StreamSource, 0, len(items)) sources := make([]StreamSource, 0, len(items))
for _, item := range items { for _, item := range items {
playlistURL, ok := providerPlaylistURL(item) playlistURL, ok := playlistURL(item)
if !ok { if !ok {
continue continue
} }
@@ -252,7 +253,7 @@ func (e *providerExtractor) buildProviderHLSSources(ctx context.Context, items [
return sources return sources
} }
func providerPlaylistURL(item providerHLSItem) (string, bool) { func playlistURL(item providerHLSItem) (string, bool) {
playlistURL := strings.TrimSpace(item.url) playlistURL := strings.TrimSpace(item.url)
if playlistURL == "" || item.hardsubLang != "en-US" { if playlistURL == "" || item.hardsubLang != "en-US" {
return "", false return "", false
@@ -298,7 +299,7 @@ func parseM3U8Sources(body string, masterURL string, referer string) []StreamSou
for _, line := range lines { for _, line := range lines {
trimmed := strings.TrimSpace(line) trimmed := strings.TrimSpace(line)
if bandwidth, ok := parseStreamBandwidth(trimmed, bwPattern); ok { if bandwidth, ok := streamBandwidth(trimmed, bwPattern); ok {
bw = bandwidth bw = bandwidth
continue continue
} }
@@ -313,7 +314,7 @@ func parseM3U8Sources(body string, masterURL string, referer string) []StreamSou
sources = append(sources, StreamSource{ sources = append(sources, StreamSource{
URL: streamURL, URL: streamURL,
Quality: qualityFromBandwidth(bw), Quality: quality(bw),
Provider: "hls", Provider: "hls",
Type: "m3u8", Type: "m3u8",
Referer: referer, Referer: referer,
@@ -331,7 +332,7 @@ func playlistBaseURL(masterURL string) string {
return masterURL return masterURL
} }
func parseStreamBandwidth(line string, bwPattern *regexp.Regexp) (int, bool) { func streamBandwidth(line string, bwPattern *regexp.Regexp) (int, bool) {
if !strings.HasPrefix(line, "#EXT-X-STREAM-INF") { if !strings.HasPrefix(line, "#EXT-X-STREAM-INF") {
return 0, false return 0, false
} }
@@ -349,7 +350,7 @@ func parseStreamBandwidth(line string, bwPattern *regexp.Regexp) (int, bool) {
return value, true return value, true
} }
func qualityFromBandwidth(bandwidth int) string { func quality(bandwidth int) string {
kbps := bandwidth / 1000 kbps := bandwidth / 1000
switch { switch {
@@ -414,7 +415,7 @@ func parseMP4UploadSources(body string, referer string) []StreamSource {
return []StreamSource{{ return []StreamSource{{
URL: mediaURL, URL: mediaURL,
Provider: "mp4upload", Provider: "mp4upload",
Type: detectProviderSourceType(mediaURL), Type: sourceType(mediaURL),
Referer: referer, Referer: referer,
}} }}
} }

View File

@@ -55,12 +55,12 @@ func (c *AllAnimeProvider) GetEpisodeSources(ctx context.Context, showID string,
return nil, errors.New("no source urls") return nil, errors.New("no source urls")
} }
references := buildSourceReferences(sourceURLs) references := sourceRefs(sourceURLs)
if len(references) == 0 { if len(references) == 0 {
return nil, errors.New("no source references") return nil, errors.New("no source references")
} }
out := c.resolveSourceReferences(ctx, references) out := c.resolveRefs(ctx, references)
if len(out) == 0 { if len(out) == 0 {
return nil, errors.New("no playable sources extracted") return nil, errors.New("no playable sources extracted")
@@ -80,15 +80,15 @@ func (c *AllAnimeProvider) extractSourceURLsFromData(ctx context.Context, data m
return nil return nil
} }
references := buildSourceReferences(sourceURLs) references := sourceRefs(sourceURLs)
if len(references) == 0 { if len(references) == 0 {
return nil return nil
} }
return c.resolveSourceReferences(ctx, references) return c.resolveRefs(ctx, references)
} }
func (c *AllAnimeProvider) resolveSourceReferences(ctx context.Context, references []sourceReference) []StreamSource { func (c *AllAnimeProvider) resolveRefs(ctx context.Context, references []sourceReference) []StreamSource {
out := make([]StreamSource, 0, len(references)) out := make([]StreamSource, 0, len(references))
for _, ref := range references { for _, ref := range references {
if source, ok := resolveDirectSource(ref); ok { if source, ok := resolveDirectSource(ref); ok {
@@ -96,7 +96,7 @@ func (c *AllAnimeProvider) resolveSourceReferences(ctx context.Context, referenc
return out return out
} }
extracted := c.resolveExtractedSources(ctx, ref) extracted := c.resolveExtracted(ctx, ref)
if len(extracted) > 0 { if len(extracted) > 0 {
out = append(out, extracted...) out = append(out, extracted...)
return out return out
@@ -131,7 +131,7 @@ func resolveDirectSource(ref sourceReference) (StreamSource, bool) {
return buildStreamSource(decoded, detectSourceType(decoded), ref.Name), true return buildStreamSource(decoded, detectSourceType(decoded), ref.Name), true
} }
func (c *AllAnimeProvider) resolveExtractedSources(ctx context.Context, ref sourceReference) []StreamSource { func (c *AllAnimeProvider) resolveExtracted(ctx context.Context, ref sourceReference) []StreamSource {
rawURL := strings.TrimSpace(ref.URL) rawURL := strings.TrimSpace(ref.URL)
decoded := decodeSourceURL(rawURL) decoded := decodeSourceURL(rawURL)
if decoded == "" { if decoded == "" {
@@ -180,7 +180,8 @@ func buildStreamSource(url, sourceType, provider string) StreamSource {
} }
} }
func buildSourceReferences(rawSourceURLs []any) []sourceReference { // source priority
func sourceRefs(rawSourceURLs []any) []sourceReference {
priorityOrder := []string{"default", "yt-mp4", "s-mp4", "luf-mp4"} priorityOrder := []string{"default", "yt-mp4", "s-mp4", "luf-mp4"}
prioritySet := map[string]struct{}{"default": {}, "yt-mp4": {}, "s-mp4": {}, "luf-mp4": {}} prioritySet := map[string]struct{}{"default": {}, "yt-mp4": {}, "s-mp4": {}, "luf-mp4": {}}