-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepisode.go
303 lines (254 loc) · 7.96 KB
/
episode.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package tohru
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
rncryptor "github.com/RNCryptor/RNCryptor-go"
"github.com/khatibomar/kobayashi"
)
const (
GetEpisodePath = "/anime/public/episodes/get-episodes-new"
EpisodeDownloadPath = "/la/public/api/fw"
BackupLinksPath = "/anime/public/v-qs.php"
)
var (
ErrBackupLink = fmt.Errorf("error while getting backup links")
)
type EpisodeService service
type episodeEndRes struct {
Response episodesResponse `json:"response"`
}
type episodeUrls struct {
EpisodeURLID string `json:"episode_url_id"`
EpisodeServerID string `json:"episode_server_id"`
EpisodeServerName string `json:"episode_server_name"`
EpisodeURL string `json:"episode_url"`
}
type nextEpisode struct {
EpisodeID string `json:"episode_id"`
EpisodeName string `json:"episode_name"`
}
type Episode struct {
EpisodeID string `json:"episode_id"`
EpisodeName string `json:"episode_name"`
EpisodeNumber string `json:"episode_number"`
AllowComment string `json:"allow_comment"`
SkipFrom string `json:"skip_from"`
SkipTo string `json:"skip_to"`
EpisodeRating string `json:"episode_rating"`
EpisodeRatingUserCount string `json:"episode_rating_user_count"`
EpisodeWatchedHistory interface{} `json:"episode_watched_history"`
EpisodeAlreadyRatedByUser interface{} `json:"episode_already_rated_by_user"`
EpisodeRatingByUser interface{} `json:"episode_rating_by_user"`
EpisodeUrls []episodeUrls `json:"episode_urls"`
NextEpisode []nextEpisode `json:"next_episode"`
PreviousEpisode []interface{} `json:"previous_episode"`
}
type episodesResponse struct {
Episodes []Episode `json:"data"`
Count int `json:"count"`
}
type DownloadInfo struct {
EpisodeHostLink string
EpisodeDirectDownloadLink string
}
type DownloadLinks []string
type DownloadInfos []DownloadInfo
func (s *EpisodeService) getEpisode(params url.Values, path, method, payload string) (*http.Response, error) {
return s.getEpisodeWithContext(context.Background(), params, path, method, payload)
}
func (s *EpisodeService) getEpisodeWithContext(ctx context.Context, params url.Values, path, method, payload string) (*http.Response, error) {
u, _ := url.Parse(BaseAPI)
u.Path = path
u.RawQuery = params.Encode()
payloadReader := strings.NewReader(payload)
var res *http.Response
res, err := s.client.request(ctx, method, u.String(), payloadReader)
return res, err
}
func (s *EpisodeService) GetEpisodesList(animeID int) ([]Episode, error) {
params := url.Values{}
payload := make(JsonPayload)
var err error
var payloadStr string
err = payload.WithJustInfo("No")
if err != nil {
return []Episode{}, err
}
err = payload.WithAnimeId(animeID)
if err != nil {
return []Episode{}, err
}
payloadStr, err = payload.ToJson()
if err != nil {
return []Episode{}, err
}
res, err := s.getEpisode(params, GetEpisodePath, http.MethodPost, "json="+payloadStr)
if err != nil {
return []Episode{}, err
}
var episodes episodeEndRes
err = json.NewDecoder(res.Body).Decode(&episodes)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
return episodes.Response.Episodes, err
}
func (s *EpisodeService) GetEpisodeDetails(animeID, episodeID int) (Episode, error) {
params := url.Values{}
payload := make(JsonPayload)
var err error
var payloadStr string
err = payload.WithEpisodeId(episodeID)
if err != nil {
return Episode{}, err
}
err = payload.WithAnimeId(animeID)
if err != nil {
return Episode{}, err
}
payloadStr, err = payload.ToJson()
if err != nil {
return Episode{}, err
}
res, err := s.getEpisode(params, GetEpisodePath, http.MethodPost, "json="+payloadStr)
if err != nil {
return Episode{}, err
}
var episodes episodeEndRes
err = json.NewDecoder(res.Body).Decode(&episodes)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
return episodes.Response.Episodes[0], err
}
func (s *EpisodeService) GetDownloadLinks(animeName string, episodeNb int) (DownloadLinks, error) {
params := url.Values{}
var err error
res, err := s.getEpisode(params, EpisodeDownloadPath, http.MethodPost, constructN(animeName, episodeNb))
if err != nil {
return DownloadLinks{}, err
}
var dwnLinks DownloadLinks
err = json.NewDecoder(res.Body).Decode(&dwnLinks)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
return dwnLinks, err
}
func (s *EpisodeService) GetDirectDownloadInfos(animeName string, episodeNb int) (DownloadInfos, error) {
return s.GetDirectDownloadInfosWithMax(animeName, episodeNb, -1)
}
func (s *EpisodeService) GetFirstDirectDownloadInfo(animeName string, episodeNb int) (DownloadInfo, error) {
link, err := s.GetDirectDownloadInfosWithMax(animeName, episodeNb, 1)
if err != nil {
return DownloadInfo{}, err
}
return link[0], err
}
type BackupLinks []struct {
File string `json:"file"`
Label string `json:"label"`
}
func (s *EpisodeService) GetDirectDownloadInfosWithMax(animeName string, episodeNb int, maxNbOfLinks int) (DownloadInfos, error) {
params := url.Values{}
var err error
res, err := s.getEpisode(params, EpisodeDownloadPath, http.MethodPost, constructN(animeName, episodeNb))
if err != nil {
return DownloadInfos{}, err
}
var dwnLinks DownloadLinks
err = json.NewDecoder(res.Body).Decode(&dwnLinks)
if err != nil {
return DownloadInfos{}, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
d := kobayashi.Decoder{}
if len(dwnLinks) < maxNbOfLinks || maxNbOfLinks <= 0 {
maxNbOfLinks = len(dwnLinks)
}
linksChan := make(chan DownloadInfo)
var wg sync.WaitGroup
for i := 0; i < len(dwnLinks); i++ {
wg.Add(1)
go func(link string) {
url, _ := d.Decode(link)
linksChan <- DownloadInfo{link, url}
wg.Done()
}(dwnLinks[i])
}
go func() {
wg.Wait()
close(linksChan)
}()
var endRes DownloadInfos
for link := range linksChan {
if link.EpisodeDirectDownloadLink == "" {
continue
}
endRes = append(endRes, link)
if len(endRes) == maxNbOfLinks {
return endRes, nil
}
}
if len(endRes) == 0 {
return s.GetBackupLinks(animeName, episodeNb)
}
return endRes, nil
}
func (s *EpisodeService) GetBackupLinks(animeName string, episodeNb int) (DownloadInfos, error) {
var endRes []DownloadInfo
if s.client.cfg.backupLinksSecret != "" {
apiUrl := BaseAPI
resource := BackupLinksPath
data := url.Values{}
data.Set("f", animeName)
data.Set("e", fmt.Sprintf("%d", episodeNb))
data.Set("inf", `{"a": "4+mwbwVfA5wLr7a4GBQvzMy1/jO9fRQ/lKJXNS4vbW/FqNL3j0vtOPd5pQx2UxrJ/8UF0Xr/v/dxkse3tjvEg/1uLKKZM8CALrQrGtw0pQqZ+UiyBJqVXe9tlbFSkV9XQRkIC6qjY66uzkzk6wauPw==", "b": "217.138.207.148"}`)
u, _ := url.ParseRequestURI(apiUrl)
u.Path = resource
urlStr := u.String()
client := &http.Client{}
r, _ := http.NewRequest(http.MethodPost, urlStr, strings.NewReader(data.Encode())) // URL-encoded payload
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := client.Do(r)
if err != nil {
return DownloadInfos{}, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return DownloadInfos{}, err
}
var backuplinks BackupLinks
encrypted, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return DownloadInfos{}, ErrBackupLink
}
decrypted, err := rncryptor.Decrypt(s.client.cfg.backupLinksSecret, encrypted)
if err != nil {
return DownloadInfos{}, ErrBackupLink
}
if err := json.Unmarshal(decrypted, &backuplinks); err != nil {
return DownloadInfos{}, ErrBackupLink
}
for _, bl := range backuplinks {
endRes = append(endRes, DownloadInfo{"Backup link", bl.File})
}
}
if len(endRes) == 0 {
return DownloadInfos{}, fmt.Errorf("all links are dead")
}
return endRes, nil
}
func constructN(animeName string, episodeNb int) string {
return fmt.Sprintf(`n=%s\%d`, animeName, episodeNb)
}