diff --git a/.gitignore b/.gitignore index 6e894b5..b4fd42f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.pdf *.exe +*.old *.token *.txt diff --git a/CHANGELOG b/CHANGELOG index 67d85b0..17afc35 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +Sunday, 16 October 2022: Version 0.9.0 + Added "--proxy" flag to specify a proxy server to use for all requests. + Added "upgrade" command for convenient upgrade on Windows and Linux platforms. + Always save the token file in the path where the binary file is located. + Sunday, 21 August 2022: Version 0.8.5 Adjusted the project layout. Changed the Golang version from 1.18 to 1.19. diff --git a/README.md b/README.md index 2bc8671..c752269 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,17 @@ KouShare-dl 是一个使用 [Cobra](https://github.com/spf13/cobra) # 编译 -如果您是 Windows 平台用户,可以直接下载 [Releases](https://github.com/yliu7949/KouShare-dl/releases/latest) -中的可执行文件`ks.exe`。 否则,您需要下载源代码自行编译。 +您可以下载 [Releases](https://github.com/yliu7949/KouShare-dl/releases/latest) +中的二进制文件`ks.exe`或`ks`后直接使用,也可以下载源代码自行编译。 +### Windows +```shell +go build -o ks.exe -trimpath -ldflags "-s -w -buildid=" ks.go +``` + +### Linux +```shell +go build -o ks -trimpath -ldflags "-s -w -buildid=" ks.go +``` # 使用方法 @@ -103,7 +112,7 @@ cd C:\Users\lenovo\Downloads\ ks version ``` -若出现`KouShare-dl v0.8.5`字样,则说明可以正常使用。接下来您可以继续输入 KouShare-dl 程序的命令来进行交互。比如,输入`ks help`并回车,您就可以看到 KouShare-dl 程序的帮助信息了。 +若出现`KouShare-dl v0.9.0`字样,则说明可以正常使用。接下来您可以继续输入 KouShare-dl 程序的命令来进行交互。比如,输入`ks help`并回车,您就可以看到 KouShare-dl 程序的帮助信息了。 # 命令简介 @@ -126,6 +135,7 @@ KouShare-dl 程序的命令具有下面的格式: record 录制指定直播间ID的直播,命令别名为live save 保存指定vid的视频(vid为视频网址里最后面的一串数字),命令别名为video slide 下载指定vid的视频对应的课件 + upgrade 升级为最新版本 version 输出版本号,并检查最新版本 ``` @@ -137,12 +147,13 @@ KouShare-dl 程序的命令具有下面的格式: -h, --help 查看帮助信息 -n, --name 指定输出文件的名字 -p, --path 指定保存文件的路径(若不指定,则默认为该程序当前所在的路径) + -P, --proxy 指定使用的http/https/socks5代理服务地址 -q, --quality 指定下载视频的清晰度(high为超清,standard为高清,low为标清,不指定则默认为超清) --qpdf-bin 指定qpdf的bin文件夹所在的路径(注:该flag无简写形式) -r, --replay 指定是否下载直播间快速回放视频 -s, --series 指定是否下载整个专题的文件 --nocolor 指定是否不使用彩色输出 - -v, --version 查看版本号 + -v, --version 查看版本号 ``` 需要注意的是,对于每个 command 命令,仅有部分 flag 参数是可用且有效的。可以通过`ks help [command]`来查看某个命令的详细描述及其可用的 flag 参数。 diff --git a/THIRD-PARTY-LICENSES b/THIRD-PARTY-LICENSES index feb7bfe..2a0d1c5 100644 --- a/THIRD-PARTY-LICENSES +++ b/THIRD-PARTY-LICENSES @@ -200,3 +200,29 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +------ + +** color; version v1.13.0 -- https://github.com/fatih/color +Copyright (c) 2013 Fatih Arslan + +The MIT License (MIT) + +Copyright (c) 2013 Fatih Arslan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/go.mod b/go.mod index d435a9b..1b220bf 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/fatih/color v1.13.0 - github.com/spf13/cobra v1.5.0 + github.com/spf13/cobra v1.6.0 github.com/tidwall/gjson v1.14.3 ) @@ -14,6 +14,6 @@ require ( github.com/mattn/go-isatty v0.0.16 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.0 // indirect - golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect ) diff --git a/go.sum b/go.sum index 750c590..f28f002 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= +github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI= +github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= @@ -22,11 +24,16 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U= golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4= +golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go new file mode 100644 index 0000000..951fce1 --- /dev/null +++ b/internal/proxy/proxy.go @@ -0,0 +1,30 @@ +package proxy + +import ( + "crypto/tls" + "log" + "net/http" + "net/url" +) + +var Client = http.Client{} + +func EnableProxy(proxyURL string) { + proxyFunc := http.ProxyFromEnvironment + if proxyURL != "" { + u, err := url.Parse(proxyURL) + if err != nil { + log.Fatal("Parse proxy url error: ", err) + } + proxyFunc = http.ProxyURL(u) + } + + Client = http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + Proxy: proxyFunc, + }, + } +} diff --git a/internal/upgrade/fileReplace.go b/internal/upgrade/fileReplace.go new file mode 100644 index 0000000..8e49d95 --- /dev/null +++ b/internal/upgrade/fileReplace.go @@ -0,0 +1,18 @@ +//go:build !windows + +package upgrade + +import ( + "fmt" + "os" + "os/exec" +) + +func fileReplace() { + _ = os.Remove(ksFilePath + ksFileName) + _ = os.Rename(ksFilePath+ksFileName+".new", ksFilePath+ksFileName) + + cmd := exec.Command(ksFilePath+ksFileName, "version") + output, _ := cmd.Output() + fmt.Println(string(output)) +} diff --git a/internal/upgrade/fileReplace_windows.go b/internal/upgrade/fileReplace_windows.go new file mode 100644 index 0000000..b392363 --- /dev/null +++ b/internal/upgrade/fileReplace_windows.go @@ -0,0 +1,20 @@ +//go:build windows + +package upgrade + +import ( + "fmt" + "os" + "os/exec" + "syscall" +) + +func fileReplace() { + _ = os.Rename(ksFilePath+ksFileName, ksOldFile) + _ = os.Rename(ksFilePath+ksFileName+".new", ksFilePath+ksFileName) + + cmd := exec.Command(ksFilePath+ksFileName, "version") + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + output, _ := cmd.Output() + fmt.Println(string(output)) +} diff --git a/internal/upgrade/upgrade.go b/internal/upgrade/upgrade.go new file mode 100644 index 0000000..32b48d3 --- /dev/null +++ b/internal/upgrade/upgrade.go @@ -0,0 +1,80 @@ +package upgrade + +import ( + "fmt" + "io" + "net" + "os" + "path/filepath" + "runtime" + + "github.com/yliu7949/KouShare-dl/internal/color" + "github.com/yliu7949/KouShare-dl/internal/proxy" +) + +var ( + ksFileName string + ksFilePath string + ksOldFile string +) + +func init() { + if runtime.GOOS == "windows" { + ksFileName = "ks.exe" + } else { + ksFileName = "ks" + } + + binaryFilePath, _ := os.Executable() + ksFilePath = filepath.Dir(binaryFilePath) + string(os.PathSeparator) + ksOldFile = ksFilePath + ksFileName + ".old" +} + +// GetLatestVersion 获取最新的KouShare-dl版本号 +func GetLatestVersion() string { + _ = os.Remove(ksOldFile) + latestVersion, _ := net.LookupTXT("ks-version.gleamoe.com") + return latestVersion[0] +} + +// Upgrade 查询并升级KouShare-dl至最新版本 +func Upgrade() { + _ = os.Remove(ksOldFile) + + fmt.Println("正在更新KouShare-dl ...") + if downloadBinaryFile() != nil { + _ = os.Remove(ksFilePath + ksFileName + ".new") + fmt.Println(color.Error("无法完整下载新版本程序,请访问 https://github.com/yliu7949/KouShare-dl/releases/latest 手动下载最新版本。")) + return + } + fmt.Print(color.Done("新版本程序下载完毕。"), "\n\n") + fileReplace() +} + +func downloadBinaryFile() error { + URL := fmt.Sprintf("https://github.com/yliu7949/KouShare-dl/releases/download/%s/%s", GetLatestVersion(), ksFileName) + resp, err := proxy.Client.Get(URL) + if err != nil { + fmt.Println(err) + return err + } + defer func() { + err = resp.Body.Close() + if err != nil { + fmt.Println(err) + } + }() + + data, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println(err.Error()) + return err + } + + if err = os.WriteFile(ksFilePath+ksFileName+".new", data, 0664); err != nil { + fmt.Println(err.Error()) + return err + } + + return nil +} diff --git a/ks.go b/ks.go index fe39470..72e982a 100644 --- a/ks.go +++ b/ks.go @@ -2,30 +2,35 @@ package main import ( "fmt" - "net" //"github.com/pkg/profile" "github.com/spf13/cobra" ks "github.com/yliu7949/KouShare-dl/cmd/ks" "github.com/yliu7949/KouShare-dl/internal/color" + "github.com/yliu7949/KouShare-dl/internal/proxy" + "github.com/yliu7949/KouShare-dl/internal/upgrade" ) -const version = "v0.8.5" +const version = "v0.9.0" func main() { //defer profile.Start().Stop() var noColor bool + var proxyURL string var rootCmd = &cobra.Command{ Use: "ks", PersistentPreRun: func(cmd *cobra.Command, args []string) { color.DisableColor(noColor) + proxy.EnableProxy(proxyURL) }, } - rootCmd.AddCommand(ks.InfoCmd(), ks.SaveCmd(), ks.RecordCmd(), ks.MergeCmd(), ks.SlideCmd(), ks.LoginCmd(), ks.LogoutCmd(), VersionCmd()) + rootCmd.AddCommand(ks.InfoCmd(), ks.SaveCmd(), ks.RecordCmd(), ks.MergeCmd(), ks.SlideCmd(), + ks.LoginCmd(), ks.LogoutCmd(), VersionCmd(), UpgradeCmd()) rootCmd.SetVersionTemplate(`{{printf "KouShare-dl %s\n" .Version}}`) rootCmd.Version = version rootCmd.PersistentFlags().BoolVar(&noColor, "nocolor", false, "指定是否不使用彩色输出") + rootCmd.PersistentFlags().StringVarP(&proxyURL, "proxy", "P", "", "指定使用的http/https/socks5代理服务地址") _ = rootCmd.Execute() } @@ -37,10 +42,10 @@ func VersionCmd() *cobra.Command { Long: `输出KouSHare-dl的版本号,并检查最新版本`, Run: func(cmd *cobra.Command, args []string) { fmt.Println(color.Emphasize("KouShare-dl " + version)) - latestVersion, _ := net.LookupTXT("ks-version.gleamoe.com") - if latestVersion[0] != version { - fmt.Println("发现新版本:KouShare-dl", latestVersion[0]) - fmt.Println("请访问 https://github.com/yliu7949/KouShare-dl/releases/latest 下载最新版本。") + latestVersion := upgrade.GetLatestVersion() + if latestVersion != version { + fmt.Println("发现新版本:KouShare-dl", latestVersion) + fmt.Println("使用ks upgrade命令升级至最新版本,或访问 https://github.com/yliu7949/KouShare-dl/releases/latest 手动下载更新。") } else { fmt.Println("当前已是最新版本。") } @@ -49,3 +54,22 @@ func VersionCmd() *cobra.Command { return cmdVersion } + +// UpgradeCmd 查询并升级KouShare-dl至最新版本 +func UpgradeCmd() *cobra.Command { + var cmdUpgrade = &cobra.Command{ + Use: "upgrade", + Short: "升级为最新版本", + Long: `查询并升级至最新版本.`, + Args: cobra.MinimumNArgs(0), + Run: func(cmd *cobra.Command, args []string) { + if upgrade.GetLatestVersion() != version { + upgrade.Upgrade() + } else { + fmt.Println("当前已是最新版本。") + } + }, + } + + return cmdUpgrade +} diff --git a/live/live.go b/live/live.go index caa104d..293939c 100644 --- a/live/live.go +++ b/live/live.go @@ -13,6 +13,7 @@ import ( "time" "github.com/tidwall/gjson" + "github.com/yliu7949/KouShare-dl/internal/proxy" "github.com/yliu7949/KouShare-dl/user" ) @@ -225,8 +226,8 @@ func (l *Live) downloadTsFile() { req.Header.Set("Host", "live.am-stc.cn") req.Header.Set("Origin", "https://www.koushare.com") req.Header.Set("Referer", "https://www.koushare.com") - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") - resp, _ := http.DefaultClient.Do(req) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36") + resp, _ := proxy.Client.Do(req) defer func() { err = resp.Body.Close() if err != nil { @@ -265,8 +266,8 @@ func (l *Live) downloadAndMergeTsFile() { req.Header.Set("Host", "live.am-stc.cn") req.Header.Set("Origin", "https://www.koushare.com") req.Header.Set("Referer", "https://www.koushare.com") - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") - resp, _ := http.DefaultClient.Do(req) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36") + resp, _ := proxy.Client.Do(req) defer func() { err = resp.Body.Close() if err != nil { diff --git a/slide/slide.go b/slide/slide.go index c81d1df..b07bb9d 100644 --- a/slide/slide.go +++ b/slide/slide.go @@ -2,8 +2,8 @@ package slide import ( "fmt" + "github.com/yliu7949/KouShare-dl/internal/proxy" "io" - "net/http" "os" "strings" @@ -130,7 +130,7 @@ func (s *Slide) findSeriesSlides() { } func (s *Slide) saveFile() { - resp, err := http.Get(s.url) + resp, err := proxy.Client.Get(s.url) if err != nil { fmt.Println("Get请求出错:", err.Error()) return diff --git a/user/hide.go b/user/hide.go index 67e5701..b4547b3 100644 --- a/user/hide.go +++ b/user/hide.go @@ -10,7 +10,7 @@ import ( func hideFile(filename string) error { if !strings.HasPrefix(filepath.Base(filename), ".") { - err := os.Rename(filename, "."+filename) + err := os.Rename(filename, filepath.Dir(filename)+string(os.PathSeparator)+"."+filepath.Base(filename)) if err != nil { return err } diff --git a/user/user.go b/user/user.go index 16acd30..cada5d7 100644 --- a/user/user.go +++ b/user/user.go @@ -6,12 +6,14 @@ import ( "net/http" "net/url" "os" + "path/filepath" "runtime" "strconv" "strings" "time" "github.com/tidwall/gjson" + "github.com/yliu7949/KouShare-dl/internal/proxy" ) // User 用户,包含手机号码、依据token文件判断的登陆状态和token的值 @@ -25,10 +27,12 @@ var tokenFileName string var u User func init() { + binaryFilePath, _ := os.Executable() + ksFilePath := filepath.Dir(binaryFilePath) + string(os.PathSeparator) if runtime.GOOS == "windows" { - tokenFileName = "ks.token" + tokenFileName = ksFilePath + "ks.token" } else { - tokenFileName = ".ks.token" + tokenFileName = ksFilePath + ".ks.token" } u.LoadToken() } @@ -58,10 +62,10 @@ func (u *User) LoadToken() { } } -// Login 使用短信验证码的方式登陆“蔻享学术”平台,登陆成功后获得token,并将token保存在当前路径下的token文件中 +// Login 使用短信验证码的方式登陆“蔻享学术”平台,登陆成功后获得token,并将token保存在可执行文件所在路径下的token文件中 func (u *User) Login() error { URL := "https://login.koushare.com/api/api-user/" - res1, err := http.PostForm(URL+"sendSms", url.Values{"phone": {u.PhoneNumber}, "scope": {"LOGIN"}}) + res1, err := proxy.Client.PostForm(URL+"sendSms", url.Values{"phone": {u.PhoneNumber}, "scope": {"LOGIN"}}) if err != nil { return err } @@ -78,7 +82,7 @@ func (u *User) Login() error { return err } - res2, err := http.PostForm(URL+"smsLogin", url.Values{"phone": {u.PhoneNumber}, "key": {verifyCode}, "rm": {"1"}}) + res2, err := proxy.Client.PostForm(URL+"smsLogin", url.Values{"phone": {u.PhoneNumber}, "key": {verifyCode}, "rm": {"1"}}) if err != nil { return err } @@ -129,7 +133,7 @@ func saveToken(cookie http.Cookie) error { } } - f, err := os.OpenFile("ks.token", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + f, err := os.OpenFile(tokenFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { return err } @@ -141,7 +145,7 @@ func saveToken(cookie http.Cookie) error { return err } // 设置“ks.token”文件为隐藏文件 - if err = hideFile("ks.token"); err != nil { + if err = hideFile(tokenFileName); err != nil { return err } return nil @@ -157,7 +161,7 @@ func MyGetRequest(url string, headers ...map[string]string) (string, error) { req.Header.Set("Accept-Encoding", "gzip, deflate, br") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") req.Header.Set("Referer", "https://www.koushare.com/") - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36") if u.LoginState == 1 { //如果token有效,则添加cookie请求头 req.Header.Set("Cookie", "Token="+u.Token) } @@ -167,7 +171,7 @@ func MyGetRequest(url string, headers ...map[string]string) (string, error) { } } - resp, _ := http.DefaultClient.Do(req) + resp, _ := proxy.Client.Do(req) defer func(Body io.ReadCloser) { _ = Body.Close() }(resp.Body) diff --git a/video/video.go b/video/video.go index ba14fb1..630d053 100644 --- a/video/video.go +++ b/video/video.go @@ -15,6 +15,7 @@ import ( "github.com/tidwall/gjson" "github.com/yliu7949/KouShare-dl/internal/color" + "github.com/yliu7949/KouShare-dl/internal/proxy" "github.com/yliu7949/KouShare-dl/user" ) @@ -132,8 +133,8 @@ func (v *Video) DownloadSingleVideo(quality string) { req.Header.Set("Host", "1254321318.vod2.myqcloud.com") req.Header.Set("Range", "bytes="+strconv.Itoa(firstByte)+"-") req.Header.Set("Referer", v.url) - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") - resp, _ := http.DefaultClient.Do(req) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36") + resp, _ := proxy.Client.Do(req) defer func() { err = resp.Body.Close() if err != nil { @@ -261,8 +262,8 @@ func (v *Video) getVideoSize(URL string) { req.Header.Set("Host", "1254321318.vod2.myqcloud.com") req.Header.Set("Range", "bytes=0-104857") req.Header.Set("Upgrade-Insecure-Requests", "1") - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36") - resp, err := http.DefaultClient.Do(req) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36") + resp, err := proxy.Client.Do(req) if err != nil || resp == nil { return } @@ -287,11 +288,12 @@ func (v *Video) showBar() { saveRateGraph += ">" } speed := float64((v.checkTmpFileSize()-startSize)/1024/1024) / time.Since(startTime).Seconds() - fmt.Printf("\r [%-50s]%s %4dMB/%dMB %-9s ", saveRateGraph, color.Highlight(fmt.Sprintf(" %2d%%", rate)), - v.checkTmpFileSize()/1024/1024, v.size/1024/1024, color.Emphasize(fmt.Sprintf("%.1fMB/s", speed))) + fmt.Printf("\r [%-50s]%s %6.2fMB/%.2fMB %-9s ", saveRateGraph, color.Highlight(fmt.Sprintf(" %2d%%", rate)), + float64(v.checkTmpFileSize())/1024/1024, float64(v.size)/1024/1024, color.Emphasize(fmt.Sprintf("%.1fMB/s", speed))) time.Sleep(100 * time.Millisecond) } else { - fmt.Printf("\r [%-50s]%s %4dMB/%dMB\n\n", strings.Repeat(">", 50), color.Done(" 100%"), v.checkTmpFileSize()/1024/1024, v.size/1024/1024) + fmt.Printf("\r [%-50s]%s %6.2fMB/%.2fMB\n\n", strings.Repeat(">", 50), color.Done(" 100%"), + float64(v.checkTmpFileSize())/1024/1024, float64(v.size)/1024/1024) //将下载完成的tmp文件重命名为mp4文件 err := os.Rename(v.SaveDir+title+"_"+v.videoQuality+".tmp", v.SaveDir+title+"_"+v.videoQuality+".mp4") if err != nil {