Added context to the crunchyroll struct

This commit is contained in:
bytedream 2022-03-21 16:55:27 +01:00
parent a6c14cb6c9
commit ac7904668f
3 changed files with 12 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package crunchyroll
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@ -32,6 +33,8 @@ const (
type Crunchyroll struct {
// Client is the http.Client to perform all requests over
Client *http.Client
// Context can be used to stop requests with Client and is context.Background by default
Context context.Context
// Locale specifies in which language all results should be returned / requested
Locale LOCALE
// SessionID is the crunchyroll session id which was used for authentication
@ -88,6 +91,7 @@ func LoginWithCredentials(user string, password string, locale LOCALE, client *h
func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*Crunchyroll, error) {
crunchy := &Crunchyroll{
Client: client,
Context: context.Background(),
Locale: locale,
SessionID: sessionID,
cache: true,

View file

@ -363,11 +363,10 @@ func (d Downloader) downloadSegment(format *Format, segment *m3u8.MediaSegment,
// https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/tool/crypt.go#L25
func (d Downloader) decryptSegment(client *http.Client, segment *m3u8.MediaSegment, block cipher.Block, iv []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, segment.URI, nil)
req, err := http.NewRequestWithContext(d.Context, http.MethodGet, segment.URI, nil)
if err != nil {
return nil, err
}
req.WithContext(d.Context)
resp, err := client.Do(req)
if err != nil {

View file

@ -2,6 +2,7 @@ package crunchyroll
import (
"io"
"net/http"
)
type Subtitle struct {
@ -13,7 +14,12 @@ type Subtitle struct {
}
func (s Subtitle) Save(writer io.Writer) error {
resp, err := s.crunchy.Client.Get(s.URL)
req, err := http.NewRequestWithContext(s.crunchy.Context, http.MethodGet, s.URL, nil)
if err != nil {
return err
}
resp, err := s.crunchy.Client.Do(req)
if err != nil {
return err
}