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
/
stream.go
129 lines (109 loc) Β· 3.39 KB
/
stream.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
117
118
119
120
121
122
123
124
125
126
127
128
129
package crunchyroll
import (
"encoding/json"
"fmt"
"github.com/grafov/m3u8"
"net/http"
"regexp"
)
// Stream contains information about all available video stream of an episode.
type Stream struct {
crunchy *Crunchyroll
children []*Format
HardsubLocale LOCALE
AudioLocale LOCALE
Subtitles []*Subtitle
formatType FormatType
id string
streamURL string
}
// StreamsFromID returns a stream by its api id.
func StreamsFromID(crunchy *Crunchyroll, id string) ([]*Stream, error) {
return fromVideoStreams(crunchy, fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
crunchy.Config.Bucket,
id,
crunchy.Locale,
crunchy.Config.Signature,
crunchy.Config.Policy,
crunchy.Config.KeyPairID))
}
// Formats returns all formats which are available for the stream.
func (s *Stream) Formats() ([]*Format, error) {
if s.children != nil {
return s.children, nil
}
resp, err := s.crunchy.Client.Get(s.streamURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
playlist, _, err := m3u8.DecodeFrom(resp.Body, true)
if err != nil {
return nil, err
}
var formats []*Format
for _, variant := range playlist.(*m3u8.MasterPlaylist).Variants {
formats = append(formats, &Format{
crunchy: s.crunchy,
ID: s.id,
FormatType: s.formatType,
Video: variant,
AudioLocale: s.AudioLocale,
Hardsub: s.HardsubLocale,
Subtitles: s.Subtitles,
})
}
if s.crunchy.cache {
s.children = formats
}
return formats, nil
}
// fromVideoStreams returns all streams which are accessible via the endpoint.
func fromVideoStreams(crunchy *Crunchyroll, endpoint string) (streams []*Stream, err error) {
resp, err := crunchy.request(endpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
if len(jsonBody) == 0 {
// this may get thrown when the crunchyroll account is just a normal account and not one with premium
if !crunchy.Config.Premium {
return nil, fmt.Errorf("no stream available, this might be the result of using a non-premium account")
} else {
return nil, fmt.Errorf("no stream available")
}
}
audioLocale := jsonBody["audio_locale"].(string)
var subtitles []*Subtitle
for _, rawSubtitle := range jsonBody["subtitles"].(map[string]interface{}) {
subtitle := &Subtitle{
crunchy: crunchy,
}
decodeMapToStruct(rawSubtitle.(map[string]interface{}), subtitle)
subtitles = append(subtitles, subtitle)
}
for _, streamData := range jsonBody["streams"].(map[string]interface{})["adaptive_hls"].(map[string]interface{}) {
streamData := streamData.(map[string]interface{})
hardsubLocale := streamData["hardsub_locale"].(string)
var id string
var formatType FormatType
href := jsonBody["__links__"].(map[string]interface{})["resource"].(map[string]interface{})["href"].(string)
if match := regexp.MustCompile(`(?sm)/(\w+)/(\w+)$`).FindAllStringSubmatch(href, -1); len(match) > 0 {
formatType = FormatType(match[0][1])
id = match[0][2]
}
stream := &Stream{
crunchy: crunchy,
HardsubLocale: LOCALE(hardsubLocale),
formatType: formatType,
id: id,
streamURL: streamData["url"].(string),
AudioLocale: LOCALE(audioLocale),
Subtitles: subtitles,
}
streams = append(streams, stream)
}
return
}