forked from crunchy-labs/crunchy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
116 lines (99 loc) · 3.11 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
package crunchyroll
import (
"encoding/json"
"errors"
"fmt"
"github.com/grafov/m3u8"
"regexp"
)
type Stream struct {
crunchy *Crunchyroll
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/%s/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
crunchy.Config.CountryCode,
crunchy.Config.MaturityRating,
crunchy.Config.Channel,
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) {
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,
})
}
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)
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 has just a normal account and not one with premium
return nil, errors.New("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)^/cms/v2/\S+/crunchyroll/(\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
}