mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Add custom useragent for cli request
This commit is contained in:
parent
3617955bc5
commit
598e460e6c
3 changed files with 47 additions and 16 deletions
13
Makefile
13
Makefile
|
|
@ -6,8 +6,7 @@ DESTDIR=
|
||||||
PREFIX=/usr
|
PREFIX=/usr
|
||||||
|
|
||||||
build:
|
build:
|
||||||
cd cmd/crunchyroll-go && go build -o $(BINARY_NAME)
|
go build -ldflags "-X 'github.com/ByteDream/crunchyroll-go/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(BINARY_NAME) cmd/crunchyroll-go/main.go
|
||||||
mv cmd/crunchyroll-go/$(BINARY_NAME) .
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(BINARY_NAME) $(VERSION_BINARY_NAME)_*
|
rm -f $(BINARY_NAME) $(VERSION_BINARY_NAME)_*
|
||||||
|
|
@ -25,10 +24,8 @@ uninstall:
|
||||||
rm -f $(DESTDIR)$(PREFIX)/share/licenses/crunchyroll-go/LICENSE
|
rm -f $(DESTDIR)$(PREFIX)/share/licenses/crunchyroll-go/LICENSE
|
||||||
|
|
||||||
release:
|
release:
|
||||||
cd cmd/crunchyroll-go && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_linux
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X 'github.com/ByteDream/crunchyroll-go/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_linux cmd/crunchyroll-go/main.go
|
||||||
cd cmd/crunchyroll-go && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_windows.exe
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-X 'github.com/ByteDream/crunchyroll-go/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_windows.exe cmd/crunchyroll-go/main.go
|
||||||
cd cmd/crunchyroll-go && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o $(VERSION_BINARY_NAME)_darwin
|
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X 'github.com/ByteDream/crunchyroll-go/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_darwin cmd/crunchyroll-go/main.go
|
||||||
|
|
||||||
strip cmd/crunchyroll-go/$(VERSION_BINARY_NAME)_linux
|
strip $(VERSION_BINARY_NAME)_linux
|
||||||
|
|
||||||
mv cmd/crunchyroll-go/$(VERSION_BINARY_NAME)_* .
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/ByteDream/crunchyroll-go"
|
"github.com/ByteDream/crunchyroll-go"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -10,6 +11,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Version = "development"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
client *http.Client
|
client *http.Client
|
||||||
crunchy *crunchyroll.Crunchyroll
|
crunchy *crunchyroll.Crunchyroll
|
||||||
|
|
@ -17,7 +20,10 @@ var (
|
||||||
|
|
||||||
quietFlag bool
|
quietFlag bool
|
||||||
verboseFlag bool
|
verboseFlag bool
|
||||||
proxyFlag string
|
|
||||||
|
proxyFlag string
|
||||||
|
|
||||||
|
useragentFlag string
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
|
|
@ -36,7 +42,7 @@ var rootCmd = &cobra.Command{
|
||||||
|
|
||||||
out.DebugLog.Printf("Executing `%s` command with %d arg(s)\n", cmd.Name(), len(args))
|
out.DebugLog.Printf("Executing `%s` command with %d arg(s)\n", cmd.Name(), len(args))
|
||||||
|
|
||||||
client, err = createOrDefaultClient(proxyFlag)
|
client, err = createOrDefaultClient(proxyFlag, useragentFlag)
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +50,10 @@ var rootCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output")
|
rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output")
|
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
|
rootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
|
||||||
|
|
||||||
|
rootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchyroll-go/%s", Version), "Useragent to do all request with")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,23 @@ func allLocalesAsStrings() (locales []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func createOrDefaultClient(proxy string) (*http.Client, error) {
|
type headerRoundTripper struct {
|
||||||
|
http.RoundTripper
|
||||||
|
header map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rht headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
|
resp, err := rht.RoundTripper.RoundTrip(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for k, v := range rht.header {
|
||||||
|
resp.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createOrDefaultClient(proxy, useragent string) (*http.Client, error) {
|
||||||
if proxy == "" {
|
if proxy == "" {
|
||||||
return http.DefaultClient, nil
|
return http.DefaultClient, nil
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -80,12 +96,21 @@ func createOrDefaultClient(proxy string) (*http.Client, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rt http.RoundTripper = &http.Transport{
|
||||||
|
DisableCompression: true,
|
||||||
|
Proxy: http.ProxyURL(proxyURL),
|
||||||
|
}
|
||||||
|
if useragent != "" {
|
||||||
|
rt = headerRoundTripper{
|
||||||
|
RoundTripper: rt,
|
||||||
|
header: map[string]string{"User-Agent": useragent},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: rt,
|
||||||
DisableCompression: true,
|
Timeout: 30 * time.Second,
|
||||||
Proxy: http.ProxyURL(proxyURL),
|
|
||||||
},
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue