gomu/lyric/lyric.go
2021-03-26 00:48:01 +08:00

50 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lyric
import (
"html"
"regexp"
"strings"
)
// SongTag is the tag information for songs
type SongTag struct {
Artist string
Title string
Album string
URL string
TitleForPopup string
LangExt string
ServiceProvider string
SongID string // SongID and LyricID is returned from cn server. It's not guaranteed to be identical
LyricID string
}
type GetLyrics interface {
GetLyric(songTag *SongTag) (string, error)
GetLyricOptions(search string) ([]*SongTag, error)
}
// cleanHTML parses html text to valid utf-8 text
func cleanHTML(input string) string {
content := html.UnescapeString(input)
// delete heading tag
re := regexp.MustCompile(`^<h3>.*`)
content = re.ReplaceAllString(content, "")
content = strings.ReplaceAll(content, "\r\n", "")
content = strings.ReplaceAll(content, "\n", "")
content = strings.ReplaceAll(content, "<br/>", "\n")
// remove non-utf8 character
re = regexp.MustCompile(``)
content = re.ReplaceAllString(content, ",")
content = strings.ToValidUTF8(content, " ")
content = strings.Map(func(r rune) rune {
if r == 160 {
return 32
}
return r
}, content)
return content
}