From b30b579fe6cb1fddb2302b08a9d3b456b9302ace Mon Sep 17 00:00:00 2001 From: norwnd Date: Sun, 28 May 2023 12:47:45 +0300 Subject: [PATCH] release: fix client panic --- Makefile | 2 +- README.md | 26 +++++++++++++------------- client.go | 10 ++++++++-- cmd/youtubedr/downloader.go | 4 ++-- decipher.go | 2 +- downloader/downloader.go | 2 +- downloader/downloader_test.go | 2 +- example_test.go | 2 +- format_list.go | 2 +- go.mod | 2 +- video_test.go | 2 +- 11 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index c6b24812..6beef1f4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ FILES_TO_FMT ?= $(shell find . -path ./vendor -prune -o -name '*.go' -print ## help: Show makefile commands .PHONY: help help: Makefile - @echo "---- Project: kkdai/youtube ----" + @echo "---- Project: norwnd/youtube ----" @echo " Usage: make COMMAND" @echo @echo " Management Commands:" diff --git a/README.md b/README.md index df5cb5e2..db883f93 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ Download Youtube Video in Golang ================== -[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/kkdai/youtube/master/LICENSE) -[![Go Reference](https://pkg.go.dev/badge/github.com/kkdai/youtube.svg)](https://pkg.go.dev/github.com/kkdai/youtube/v2) -[![Build Status](https://github.com/kkdai/youtube/workflows/go/badge.svg?branch=master)](https://github.com/kkdai/youtube/actions) -[![Coverage](https://codecov.io/gh/kkdai/youtube/branch/master/graph/badge.svg)](https://codecov.io/gh/kkdai/youtube) -[![](https://goreportcard.com/badge/github.com/kkdai/youtube)](https://goreportcard.com/badge/github.com/kkdai/youtube) +[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/norwnd/youtube/master/LICENSE) +[![Go Reference](https://pkg.go.dev/badge/github.com/norwnd/youtube.svg)](https://pkg.go.dev/github.com/norwnd/youtube/v2) +[![Build Status](https://github.com/norwnd/youtube/workflows/go/badge.svg?branch=master)](https://github.com/norwnd/youtube/actions) +[![Coverage](https://codecov.io/gh/norwnd/youtube/branch/master/graph/badge.svg)](https://codecov.io/gh/norwnd/youtube) +[![](https://goreportcard.com/badge/github.com/norwnd/youtube)](https://goreportcard.com/badge/github.com/norwnd/youtube) This package is a Youtube video download package, for more detail refer [https://github.com/ytdl-org/youtube-dl](https://github.com/ytdl-org/youtube-dl) for more download options. @@ -24,13 +24,13 @@ This tool is meant to be used to download CC0 licenced content, we do not suppor Please ensure you have installed Go 1.18 or later. ```shell -go get github.com/kkdai/youtube/v2 +go get github.com/norwnd/youtube/v2 ``` ### From source code ```shell -git clone https://github.com/kkdai/youtube.git +git clone https://github.com/norwnd/youtube.git cd youtube go run ./cmd/youtubedr ``` @@ -69,7 +69,7 @@ Please check out the [example_test.go](example_test.go) for example code. ## Example: * ### Get information of dotGo-2015-rob-pike video for downloading - `go get github.com/kkdai/youtube/v2/youtubedr` + `go get github.com/norwnd/youtube/v2/youtubedr` Download video from [dotGo 2015 - Rob Pike - Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) @@ -87,7 +87,7 @@ Please check out the [example_test.go](example_test.go) for example code. ``` * ### Download dotGo-2015-rob-pike-video - `go get github.com/kkdai/youtube/v2/youtubedr` + `go get github.com/norwnd/youtube/v2/youtubedr` Download video from [dotGo 2015 - Rob Pike - Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) @@ -97,7 +97,7 @@ Please check out the [example_test.go](example_test.go) for example code. * ### Download video to specific folder and name - `go get github.com/kkdai/youtube/v2/youtubedr` + `go get github.com/norwnd/youtube/v2/youtubedr` Download video from [dotGo 2015 - Rob Pike - Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) to current directory and name the file to simplicity-is-complicated.mp4 @@ -107,7 +107,7 @@ Please check out the [example_test.go](example_test.go) for example code. * ### Download video with specific quality - `go get github.com/kkdai/youtube/v2/youtubedr` + `go get github.com/norwnd/youtube/v2/youtubedr` Download video from [dotGo 2015 - Rob Pike - Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) with specific quality @@ -125,7 +125,7 @@ Please check out the [example_test.go](example_test.go) for example code. * ### Download video with specific itag - `go get github.com/kkdai/youtube/v2/youtubedr` + `go get github.com/norwnd/youtube/v2/youtubedr` Download video from [dotGo 2015 - Rob Pike - Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) @@ -154,7 +154,7 @@ Please check out the [example_test.go](example_test.go) for example code. ## Project52 -It is one of my [project 52](https://github.com/kkdai/project52). +It is one of my [project 52](https://github.com/norwnd/project52). ## License diff --git a/client.go b/client.go index 3cda4dc9..2f3aad26 100644 --- a/client.go +++ b/client.go @@ -477,6 +477,8 @@ func (c *Client) GetStreamURL(video *Video, format *Format) (string, error) { // GetStreamURLContext returns the url for a specific format with a context func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error) { + c.assureClient() + if format == nil { return "", ErrNoFormat } @@ -613,7 +615,7 @@ func (c *Client) httpPostBodyBytes(ctx context.Context, url string, body interfa // downloadChunk returns the chunk bytes. // Downloading in multiple chunks is much faster: -// https://github.com/kkdai/youtube/pull/190 +// https://github.com/norwnd/youtube/pull/190 func (c *Client) downloadChunk(req *http.Request, chunk chunk) ([]byte, error) { q := req.URL.Query() q.Set("range", fmt.Sprintf("%d-%d", chunk.start, chunk.end)) @@ -621,7 +623,11 @@ func (c *Client) downloadChunk(req *http.Request, chunk chunk) ([]byte, error) { resp, err := c.httpDo(req) if err != nil { - return nil, ErrUnexpectedStatusCode(resp.StatusCode) + statusCode := 0 // unknown + if resp != nil { + statusCode = resp.StatusCode + } + return nil, fmt.Errorf("unexpected error in downloadChunk, status code: %d, err: %w", statusCode, err) } defer resp.Body.Close() diff --git a/cmd/youtubedr/downloader.go b/cmd/youtubedr/downloader.go index 3a478940..a70155a2 100644 --- a/cmd/youtubedr/downloader.go +++ b/cmd/youtubedr/downloader.go @@ -14,8 +14,8 @@ import ( "github.com/spf13/pflag" "golang.org/x/net/http/httpproxy" - "github.com/kkdai/youtube/v2" - ytdl "github.com/kkdai/youtube/v2/downloader" + "github.com/norwnd/youtube/v2" + ytdl "github.com/norwnd/youtube/v2/downloader" ) var ( diff --git a/decipher.go b/decipher.go index a8ce33da..8570c399 100644 --- a/decipher.go +++ b/decipher.go @@ -47,7 +47,7 @@ func (c *Client) decipherURL(ctx context.Context, videoID string, cipher string) return uri.String(), nil } -// see https://github.com/kkdai/youtube/pull/244 +// see https://github.com/norwnd/youtube/pull/244 func (c *Client) unThrottle(ctx context.Context, videoID string, urlString string) (string, error) { config, err := c.getPlayerConfig(ctx, videoID) if err != nil { diff --git a/downloader/downloader.go b/downloader/downloader.go index 748de705..c4236c0b 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -9,7 +9,7 @@ import ( "os/exec" "path/filepath" - "github.com/kkdai/youtube/v2" + "github.com/norwnd/youtube/v2" "github.com/vbauerster/mpb/v5" "github.com/vbauerster/mpb/v5/decor" ) diff --git a/downloader/downloader_test.go b/downloader/downloader_test.go index 84910715..fde2147a 100644 --- a/downloader/downloader_test.go +++ b/downloader/downloader_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/kkdai/youtube/v2" + "github.com/norwnd/youtube/v2" ) var testDownloader = func() (dl Downloader) { diff --git a/example_test.go b/example_test.go index 00419750..970cce1c 100644 --- a/example_test.go +++ b/example_test.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "github.com/kkdai/youtube/v2" + "github.com/norwnd/youtube/v2" ) // ExampleDownload : Example code for how to use this package for download video. diff --git a/format_list.go b/format_list.go index 827d2f0d..95f2735f 100644 --- a/format_list.go +++ b/format_list.go @@ -92,7 +92,7 @@ func sortFormat(i int, j int, formats FormatList) bool { // Sort by Width if formats[i].Width == formats[j].Width { // Format 137 downloads slowly, give it less priority - // see https://github.com/kkdai/youtube/pull/171 + // see https://github.com/norwnd/youtube/pull/171 switch 137 { case formats[i].ItagNo: return false diff --git a/go.mod b/go.mod index 02b0a1a1..e86fad9f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kkdai/youtube/v2 +module github.com/norwnd/youtube/v2 go 1.19 diff --git a/video_test.go b/video_test.go index 9105310e..d959c5df 100644 --- a/video_test.go +++ b/video_test.go @@ -156,7 +156,7 @@ func TestDownload_WhenPlayabilityStatusIsNotOK(t *testing.T) { } } -// See https://github.com/kkdai/youtube/pull/238 +// See https://github.com/norwnd/youtube/pull/238 func TestDownload_SensitiveContent(t *testing.T) { _, err := testClient.GetVideo("MS91knuzoOA") require.EqualError(t, err, "can't bypass age restriction: embedding of this video has been disabled")