Skip to content

Commit

Permalink
Merge pull request #19 from yliu7949/v0.9.0
Browse files Browse the repository at this point in the history
V0.9.0
  • Loading branch information
yliu7949 authored Oct 16, 2022
2 parents 9f032f1 + 1080d7e commit 4f43198
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.pdf

*.exe
*.old
*.token
*.txt

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

# 使用方法

Expand All @@ -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 程序的帮助信息了。

# 命令简介

Expand All @@ -126,6 +135,7 @@ KouShare-dl 程序的命令具有下面的格式:
record 录制指定直播间ID的直播,命令别名为live
save 保存指定vid的视频(vid为视频网址里最后面的一串数字),命令别名为video
slide 下载指定vid的视频对应的课件
upgrade 升级为最新版本
version 输出版本号,并检查最新版本
```

Expand All @@ -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 参数。
Expand Down
26 changes: 26 additions & 0 deletions THIRD-PARTY-LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
30 changes: 30 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
18 changes: 18 additions & 0 deletions internal/upgrade/fileReplace.go
Original file line number Diff line number Diff line change
@@ -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))
}
20 changes: 20 additions & 0 deletions internal/upgrade/fileReplace_windows.go
Original file line number Diff line number Diff line change
@@ -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))
}
80 changes: 80 additions & 0 deletions internal/upgrade/upgrade.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 31 additions & 7 deletions ks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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("当前已是最新版本。")
}
Expand All @@ -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
}
Loading

0 comments on commit 4f43198

Please sign in to comment.