fix lyrc charset auto decoding

This commit is contained in:
aynakeya
2024-06-23 15:22:13 +08:00
parent ceabb3009f
commit 10ba44e33a
2 changed files with 57 additions and 2 deletions

3
go.mod
View File

@@ -6,8 +6,10 @@ require (
github.com/XiaoMengXinX/Music163Api-Go v0.1.30
github.com/aynakeya/deepcolor v1.0.2
github.com/dhowden/tag v0.0.0-20230630033851-978a0926ee25
github.com/go-resty/resty/v2 v2.7.0
github.com/jinzhu/copier v0.4.0
github.com/sahilm/fuzzy v0.1.0
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d
github.com/spf13/cast v1.5.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.16.0
@@ -17,7 +19,6 @@ require (
github.com/PuerkitoBio/goquery v1.7.1 // indirect
github.com/andybalholm/cascadia v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect

View File

@@ -4,12 +4,59 @@ import (
"fmt"
"github.com/AynaLivePlayer/miaosic"
"github.com/dhowden/tag"
"github.com/saintfish/chardet"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"os"
"path"
"path/filepath"
"strings"
)
var detector = chardet.NewTextDetector()
func getEncoding(name string) encoding.Encoding {
switch name {
case "UTF-8":
return unicode.UTF8
case "ISO-8859-1":
return charmap.ISO8859_1
case "Windows-1252":
return charmap.Windows1252
case "GBK":
return simplifiedchinese.GBK
case "GB-18030":
return simplifiedchinese.GB18030
case "Big5":
return traditionalchinese.Big5
case "Shift_JIS":
return japanese.ShiftJIS
case "EUC-KR":
return korean.EUCKR
default:
return unicode.UTF8
}
}
func decodeBytes(data []byte, enc encoding.Encoding) (string, error) {
// Create a transformer that will decode the bytes
transformer := enc.NewDecoder()
// Transform the bytes into a string
result, _, err := transform.String(transformer, string(data))
if err != nil {
return "", err
}
return result, nil
}
func getPlaylistNames(localdir string) []string {
names := make([]string, 0)
items, _ := os.ReadDir(localdir)
@@ -102,7 +149,14 @@ func readLyric(localdir string, meta miaosic.MetaData) ([]miaosic.Lyrics, error)
data, err := os.ReadFile(path.Join(filepath.Dir(p), strings.TrimSuffix(filepath.Base(p), filepath.Ext(p))+".lrc"))
if err == nil && len(data) > 0 {
lyrics = append(lyrics, miaosic.ParseLyrics("default", string(data)))
detectedChar, err := detector.DetectBest(data)
if err != nil {
detectedChar.Charset = "UTF-8"
}
datastr, _ := decodeBytes(data, getEncoding(detectedChar.Charset))
if datastr != "" {
lyrics = append(lyrics, miaosic.ParseLyrics("default", datastr))
}
}
f, err := os.Open(p)
defer f.Close()