From ac7904668f212cfadb58fb98248648c8aa775bc5 Mon Sep 17 00:00:00 2001 From: bytedream Date: Mon, 21 Mar 2022 16:55:27 +0100 Subject: [PATCH] Added context to the crunchyroll struct --- crunchyroll.go | 4 ++++ downloader.go | 3 +-- subtitle.go | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/crunchyroll.go b/crunchyroll.go index f59fb30..82cd118 100644 --- a/crunchyroll.go +++ b/crunchyroll.go @@ -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, diff --git a/downloader.go b/downloader.go index 48c0295..be625b3 100644 --- a/downloader.go +++ b/downloader.go @@ -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 { diff --git a/subtitle.go b/subtitle.go index 06ec9aa..6a33e14 100644 --- a/subtitle.go +++ b/subtitle.go @@ -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 }