Made verbose output optional when receiving system locale

This commit is contained in:
bytedream 2022-03-21 20:45:38 +01:00
parent ba8054b611
commit 83c9ae4927
2 changed files with 14 additions and 8 deletions

View file

@ -46,7 +46,7 @@ func init() {
func loginCredentials(user, password string) error { func loginCredentials(user, password string) error {
out.Debug("Logging in via credentials") out.Debug("Logging in via credentials")
if _, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(), client); err != nil { if _, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(false), client); err != nil {
out.Err(err.Error()) out.Err(err.Error())
os.Exit(1) os.Exit(1)
} }
@ -56,7 +56,7 @@ func loginCredentials(user, password string) error {
func loginSessionID(sessionID string) error { func loginSessionID(sessionID string) error {
out.Debug("Logging in via session id") out.Debug("Logging in via session id")
if _, err := crunchyroll.LoginWithSessionID(sessionID, systemLocale(), client); err != nil { if _, err := crunchyroll.LoginWithSessionID(sessionID, systemLocale(false), client); err != nil {
out.Err(err.Error()) out.Err(err.Error())
os.Exit(1) os.Exit(1)
} }

View file

@ -29,14 +29,16 @@ var urlFilter = regexp.MustCompile(`(S(\d+))?(E(\d+))?((-)(S(\d+))?(E(\d+))?)?(,
// systemLocale receives the system locale // systemLocale receives the system locale
// https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590 // https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590
func systemLocale() crunchyroll.LOCALE { func systemLocale(verbose bool) crunchyroll.LOCALE {
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
if lang, ok := os.LookupEnv("LANG"); ok { if lang, ok := os.LookupEnv("LANG"); ok {
prefix := strings.Split(lang, "_")[0] prefix := strings.Split(lang, "_")[0]
suffix := strings.Split(strings.Split(lang, ".")[0], "_")[1] suffix := strings.Split(strings.Split(lang, ".")[0], "_")[1]
l := crunchyroll.LOCALE(fmt.Sprintf("%s-%s", prefix, suffix)) l := crunchyroll.LOCALE(fmt.Sprintf("%s-%s", prefix, suffix))
if !utils.ValidateLocale(l) { if !utils.ValidateLocale(l) {
if verbose {
out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US) out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
}
l = crunchyroll.US l = crunchyroll.US
} }
return l return l
@ -46,13 +48,17 @@ func systemLocale() crunchyroll.LOCALE {
if output, err := cmd.Output(); err == nil { if output, err := cmd.Output(); err == nil {
l := crunchyroll.LOCALE(strings.Trim(string(output), "\r\n")) l := crunchyroll.LOCALE(strings.Trim(string(output), "\r\n"))
if !utils.ValidateLocale(l) { if !utils.ValidateLocale(l) {
if verbose {
out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US) out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
}
l = crunchyroll.US l = crunchyroll.US
} }
return l return l
} }
} }
if verbose {
out.Err("Failed to get locale, using %s", crunchyroll.US) out.Err("Failed to get locale, using %s", crunchyroll.US)
}
return crunchyroll.US return crunchyroll.US
} }
@ -130,13 +136,13 @@ func loadCrunchy() {
split := strings.SplitN(string(body), "\n", 2) split := strings.SplitN(string(body), "\n", 2)
if len(split) == 1 || split[1] == "" { if len(split) == 1 || split[1] == "" {
if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(), client); err != nil { if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(true), client); err != nil {
out.StopProgress(err.Error()) out.StopProgress(err.Error())
os.Exit(1) os.Exit(1)
} }
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0]) out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0])
} else { } else {
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(), client); err != nil { if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(true), client); err != nil {
out.StopProgress(err.Error()) out.StopProgress(err.Error())
os.Exit(1) os.Exit(1)
} }