-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathepisode.go
More file actions
122 lines (107 loc) · 3.19 KB
/
episode.go
File metadata and controls
122 lines (107 loc) · 3.19 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type Episode struct {
// Dash manifest file URL
ManifestURL string `json:"url"`
// List of .ass files
Subtitles map[string]*Subtitle `json:"subtitles"`
// Token to give to the Widevine CDM challenge
Token string `json:"token"`
// Error, `nil` if there's no error
Error *string `json:"error"`
}
type Subtitle struct {
// Language represents a subtitle language in the "en-US" format
Language string `json:"language"`
// Direct URL to the .ass file
URL string `json:"url"`
}
func getEpisode(id string) Episode {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://www.crunchyroll.com/playback/v3/%s/web/firefox/play", id), nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0")
resp, err := DoRequest(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var episode Episode
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if err = json.Unmarshal(body, &episode); err != nil {
panic(err)
}
if episode.Error != nil {
print("Error:", *episode.Error)
os.Exit(1)
}
return episode
}
type EpisodeMetadataResponse struct {
Data []EpisodeInfo `json:"data"`
}
type EpisodeInfo struct {
EpisodeMetadata EpisodeMetadata `json:"episode_metadata"`
// Episode title
Title string `json:"title"`
}
type EpisodeMetadata struct {
AudioLocale string `json:"audio_locale"`
EpisodeNumber int `json:"episode_number"`
SeasonNumber int `json:"season_number"`
SeriesTitle string `json:"series_title"`
// AvailabilityStarts represents the date when the episode was released on Crunchyroll
AvailabilityStarts string `json:"availability_starts"`
Versions []*DubVersion `json:"versions"`
}
type DubVersion struct {
AudioLocale string `json:"audio_locale"`
GUID string `json:"guid"`
}
func getEpisodeInfo(id string) EpisodeInfo {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://www.crunchyroll.com/content/v2/cms/objects/%s?ratings=true&preferred_audio_language=ja-JP&locale=en-US", id), nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0")
resp, err := DoRequest(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var info EpisodeMetadataResponse
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if err = json.Unmarshal(body, &info); err != nil {
panic(err)
}
return info.Data[0]
}
// deleteStream removes the stream to make Crunchyroll think we "left" the playback
func deleteStream(contentId, sToken string) bool {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("https://www.crunchyroll.com/playback/v1/token/%s/%s", contentId, sToken), nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0")
resp, err := DoRequest(req)
if err != nil {
panic(err)
}
return resp.StatusCode == http.StatusNoContent
}