refactor: shorten function names in allanime
This commit is contained in:
@@ -28,8 +28,8 @@ func (c *AllAnimeProvider) GetEpisodeAvailabilityByProviderID(ctx context.Contex
|
||||
return domain.EpisodeAvailability{}, err
|
||||
}
|
||||
|
||||
sub := parseEpisodeNumbers(append(available.Sub, available.Raw...))
|
||||
dub := parseEpisodeNumbers(available.Dub)
|
||||
sub := episodeNums(append(available.Sub, available.Raw...))
|
||||
dub := episodeNums(available.Dub)
|
||||
return domain.EpisodeAvailability{Sub: sub, Dub: dub}, nil
|
||||
}
|
||||
|
||||
@@ -62,13 +62,14 @@ func (c *AllAnimeProvider) GetAvailableEpisodes(ctx context.Context, showID stri
|
||||
}
|
||||
|
||||
return AvailableEpisodes{
|
||||
Sub: stringSliceFromAny(detail["sub"]),
|
||||
Dub: stringSliceFromAny(detail["dub"]),
|
||||
Raw: stringSliceFromAny(detail["raw"]),
|
||||
Sub: stringsFrom(detail["sub"]),
|
||||
Dub: stringsFrom(detail["dub"]),
|
||||
Raw: stringsFrom(detail["raw"]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseEpisodeNumbers(raw []string) []int {
|
||||
// episode ids
|
||||
func episodeNums(raw []string) []int {
|
||||
seen := make(map[int]bool, len(raw))
|
||||
out := make([]int, 0, len(raw))
|
||||
for _, value := range raw {
|
||||
@@ -82,7 +83,8 @@ func parseEpisodeNumbers(raw []string) []int {
|
||||
return out
|
||||
}
|
||||
|
||||
func stringSliceFromAny(value any) []string {
|
||||
// graphql list
|
||||
func stringsFrom(value any) []string {
|
||||
items, ok := value.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
)
|
||||
|
||||
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}
|
||||
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("parseEpisodeNumbers() = %v, want %v", got, want)
|
||||
t.Fatalf("episodeNums() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func runSourceReferenceTests(t *testing.T, tests []sourceReferencesTestCase) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := buildSourceReferences(tt.rawURLs)
|
||||
got := sourceRefs(tt.rawURLs)
|
||||
if len(got) != len(tt.wantRefs) {
|
||||
t.Errorf("got %d refs, want %d", len(got), len(tt.wantRefs))
|
||||
return
|
||||
@@ -327,7 +327,7 @@ func TestBuildSourceReferencesOrder(t *testing.T) {
|
||||
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"}
|
||||
if len(got) != len(wantOrder) {
|
||||
|
||||
@@ -108,23 +108,24 @@ func (e *providerExtractor) parseProviderResponse(ctx context.Context, response
|
||||
return []StreamSource{}
|
||||
}
|
||||
|
||||
data := collectProviderResponseData(root, e.referer)
|
||||
sources := buildProviderLinkSources(data.links, data.referer)
|
||||
sources = append(sources, e.buildProviderHLSSources(ctx, data.hls, data.referer)...)
|
||||
data := collectData(root, e.referer)
|
||||
sources := linkSources(data.links, data.referer)
|
||||
sources = append(sources, e.hlsSources(ctx, data.hls, data.referer)...)
|
||||
|
||||
attachSubtitles(sources, data.subtitles)
|
||||
|
||||
return sources
|
||||
}
|
||||
|
||||
func collectProviderResponseData(root any, fallbackReferer string) providerResponseData {
|
||||
// provider payload
|
||||
func collectData(root any, fallbackReferer string) providerResponseData {
|
||||
data := providerResponseData{referer: fallbackReferer}
|
||||
|
||||
var walk func(v any)
|
||||
walk = func(v any) {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
collectProviderMapData(x, &data)
|
||||
collectMapData(x, &data)
|
||||
for _, child := range x {
|
||||
walk(child)
|
||||
}
|
||||
@@ -143,7 +144,7 @@ func collectProviderResponseData(root any, fallbackReferer string) providerRespo
|
||||
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 trimmedRef := strings.TrimSpace(ref); trimmedRef != "" {
|
||||
data.referer = trimmedRef
|
||||
@@ -163,11 +164,11 @@ func collectProviderMapData(node map[string]any, data *providerResponseData) {
|
||||
}
|
||||
|
||||
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))
|
||||
for _, item := range items {
|
||||
node, ok := item.(map[string]any)
|
||||
@@ -195,7 +196,7 @@ func parseProviderSubtitles(items []any) []Subtitle {
|
||||
return subtitles
|
||||
}
|
||||
|
||||
func buildProviderLinkSources(items []providerLinkItem, referer string) []StreamSource {
|
||||
func linkSources(items []providerLinkItem, referer string) []StreamSource {
|
||||
sources := make([]StreamSource, 0, len(items))
|
||||
for _, item := range items {
|
||||
link := strings.TrimSpace(item.link)
|
||||
@@ -207,7 +208,7 @@ func buildProviderLinkSources(items []providerLinkItem, referer string) []Stream
|
||||
URL: link,
|
||||
Quality: strings.TrimSpace(item.resolutionStr),
|
||||
Provider: "wixmp",
|
||||
Type: detectProviderSourceType(link),
|
||||
Type: sourceType(link),
|
||||
Referer: referer,
|
||||
})
|
||||
}
|
||||
@@ -215,19 +216,19 @@ func buildProviderLinkSources(items []providerLinkItem, referer string) []Stream
|
||||
return sources
|
||||
}
|
||||
|
||||
func detectProviderSourceType(link string) string {
|
||||
sourceType := detectStreamType(link)
|
||||
if sourceType != "unknown" {
|
||||
return sourceType
|
||||
func sourceType(link string) string {
|
||||
typ := detectStreamType(link)
|
||||
if typ != "unknown" {
|
||||
return typ
|
||||
}
|
||||
|
||||
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))
|
||||
for _, item := range items {
|
||||
playlistURL, ok := providerPlaylistURL(item)
|
||||
playlistURL, ok := playlistURL(item)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -252,7 +253,7 @@ func (e *providerExtractor) buildProviderHLSSources(ctx context.Context, items [
|
||||
return sources
|
||||
}
|
||||
|
||||
func providerPlaylistURL(item providerHLSItem) (string, bool) {
|
||||
func playlistURL(item providerHLSItem) (string, bool) {
|
||||
playlistURL := strings.TrimSpace(item.url)
|
||||
if playlistURL == "" || item.hardsubLang != "en-US" {
|
||||
return "", false
|
||||
@@ -298,7 +299,7 @@ func parseM3U8Sources(body string, masterURL string, referer string) []StreamSou
|
||||
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if bandwidth, ok := parseStreamBandwidth(trimmed, bwPattern); ok {
|
||||
if bandwidth, ok := streamBandwidth(trimmed, bwPattern); ok {
|
||||
bw = bandwidth
|
||||
continue
|
||||
}
|
||||
@@ -313,7 +314,7 @@ func parseM3U8Sources(body string, masterURL string, referer string) []StreamSou
|
||||
|
||||
sources = append(sources, StreamSource{
|
||||
URL: streamURL,
|
||||
Quality: qualityFromBandwidth(bw),
|
||||
Quality: quality(bw),
|
||||
Provider: "hls",
|
||||
Type: "m3u8",
|
||||
Referer: referer,
|
||||
@@ -331,7 +332,7 @@ func playlistBaseURL(masterURL string) string {
|
||||
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") {
|
||||
return 0, false
|
||||
}
|
||||
@@ -349,7 +350,7 @@ func parseStreamBandwidth(line string, bwPattern *regexp.Regexp) (int, bool) {
|
||||
return value, true
|
||||
}
|
||||
|
||||
func qualityFromBandwidth(bandwidth int) string {
|
||||
func quality(bandwidth int) string {
|
||||
kbps := bandwidth / 1000
|
||||
|
||||
switch {
|
||||
@@ -414,7 +415,7 @@ func parseMP4UploadSources(body string, referer string) []StreamSource {
|
||||
return []StreamSource{{
|
||||
URL: mediaURL,
|
||||
Provider: "mp4upload",
|
||||
Type: detectProviderSourceType(mediaURL),
|
||||
Type: sourceType(mediaURL),
|
||||
Referer: referer,
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -55,12 +55,12 @@ func (c *AllAnimeProvider) GetEpisodeSources(ctx context.Context, showID string,
|
||||
return nil, errors.New("no source urls")
|
||||
}
|
||||
|
||||
references := buildSourceReferences(sourceURLs)
|
||||
references := sourceRefs(sourceURLs)
|
||||
if len(references) == 0 {
|
||||
return nil, errors.New("no source references")
|
||||
}
|
||||
|
||||
out := c.resolveSourceReferences(ctx, references)
|
||||
out := c.resolveRefs(ctx, references)
|
||||
|
||||
if len(out) == 0 {
|
||||
return nil, errors.New("no playable sources extracted")
|
||||
@@ -80,15 +80,15 @@ func (c *AllAnimeProvider) extractSourceURLsFromData(ctx context.Context, data m
|
||||
return nil
|
||||
}
|
||||
|
||||
references := buildSourceReferences(sourceURLs)
|
||||
references := sourceRefs(sourceURLs)
|
||||
if len(references) == 0 {
|
||||
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))
|
||||
for _, ref := range references {
|
||||
if source, ok := resolveDirectSource(ref); ok {
|
||||
@@ -96,7 +96,7 @@ func (c *AllAnimeProvider) resolveSourceReferences(ctx context.Context, referenc
|
||||
return out
|
||||
}
|
||||
|
||||
extracted := c.resolveExtractedSources(ctx, ref)
|
||||
extracted := c.resolveExtracted(ctx, ref)
|
||||
if len(extracted) > 0 {
|
||||
out = append(out, extracted...)
|
||||
return out
|
||||
@@ -131,7 +131,7 @@ func resolveDirectSource(ref sourceReference) (StreamSource, bool) {
|
||||
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)
|
||||
decoded := decodeSourceURL(rawURL)
|
||||
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"}
|
||||
prioritySet := map[string]struct{}{"default": {}, "yt-mp4": {}, "s-mp4": {}, "luf-mp4": {}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user