This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathurl.go
116 lines (105 loc) · 2.53 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package crunchyroll
import (
"fmt"
)
// ExtractEpisodesFromUrl extracts all episodes from an url.
// If audio is not empty, the episodes gets filtered after the given locale.
func (c *Crunchyroll) ExtractEpisodesFromUrl(url string, audio ...LOCALE) ([]*Episode, error) {
series, episodes, err := c.ParseUrl(url)
if err != nil {
return nil, err
}
var eps []*Episode
var notAvailableContinue bool
if series != nil {
seasons, err := series.Seasons()
if err != nil {
return nil, err
}
for _, season := range seasons {
if audio != nil {
if available, err := season.Available(); err != nil {
return nil, err
} else if !available {
notAvailableContinue = true
continue
}
locale, err := season.AudioLocale()
if err != nil {
return nil, err
}
var found bool
for _, l := range audio {
if locale == l {
found = true
break
}
}
if !found {
continue
}
}
e, err := season.Episodes()
if err != nil {
return nil, err
}
eps = append(eps, e...)
}
} else if episodes != nil {
if audio == nil {
return episodes, nil
}
for _, episode := range episodes {
// if no episode streams are available, calling episode.AudioLocale
// will result in an unwanted error
if !episode.Available() {
notAvailableContinue = true
continue
}
locale, err := episode.AudioLocale()
if err != nil {
return nil, err
}
if audio != nil {
var found bool
for _, l := range audio {
if locale == l {
found = true
break
}
}
if !found {
continue
}
}
eps = append(eps, episode)
}
}
if len(eps) == 0 {
if notAvailableContinue {
return nil, fmt.Errorf("could not find any matching episode which is accessable with a non-premium account")
} else {
return nil, fmt.Errorf("could not find any matching episode")
}
}
return eps, nil
}
// ParseUrl parses the given url into a series or episode.
// The returning episode is a slice because non-beta urls have the same episode with different languages.
func (c *Crunchyroll) ParseUrl(url string) (*Series, []*Episode, error) {
if seriesId, ok := ParseSeriesURL(url); ok {
series, err := SeriesFromID(c, seriesId)
if err != nil {
return nil, nil, err
}
return series, nil, nil
} else if episodeId, ok := ParseEpisodeURL(url); ok {
episode, err := EpisodeFromID(c, episodeId)
if err != nil {
return nil, nil, err
}
return nil, []*Episode{episode}, nil
} else {
return nil, nil, fmt.Errorf("invalid url %s", url)
}
}