add config.yaml support

pull/14/head
itouakirai 5 months ago committed by GitHub
parent 3089c5b5bf
commit 112dfff400
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 54
      main_select.go

@ -5,6 +5,7 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"gopkg.in/yaml.v2"
"errors"
"fmt"
"github.com/beevik/etree"
@ -35,6 +36,18 @@ const (
var (
forbiddenNames = regexp.MustCompile(`[/\\<>:"|?*]`)
)
type Config struct {
MediaUserToken string `yaml:"media-user-token"`
SaveLrcFile bool `yaml:"save-lrc-file"`
EmbedLrc bool `yaml:"embed-lrc"`
EmbedCover bool `yaml:"embed-cover"`
CoverSize string `yaml:"cover-size"`
CoverFormat string `yaml:"cover-format"`
AlacSaveFolder string `yaml:"alac-save-folder"`
AtmosSaveFolder string `yaml:"atmos-save-folder"`
}
var config Config
type SampleInfo struct {
data []byte
@ -48,6 +61,20 @@ type SongInfo struct {
samples []SampleInfo
}
func loadConfig() error {
// 读取config.yaml文件内容
data, err := ioutil.ReadFile("config.yaml")
if err != nil {
return err
}
// 将yaml解析到config变量中
err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
return nil
}
func (s *SongInfo) Duration() (ret uint64) {
for i := range s.samples {
ret += uint64(s.samples[i].duration)
@ -1020,7 +1047,7 @@ func getSongLyrics(songId string, storefront string, token string, userToken str
}
func writeCover(sanAlbumFolder, url string) error {
covPath := filepath.Join(sanAlbumFolder, "cover.jpg")
covPath := filepath.Join(sanAlbumFolder, "cover." + config.CoverFormat)
exists, err := fileExists(covPath)
if err != nil {
fmt.Println("Failed to check if cover exists.")
@ -1029,7 +1056,7 @@ func writeCover(sanAlbumFolder, url string) error {
if exists {
return nil
}
url = strings.Replace(url, "{w}x{h}", "5000x5000", 1)
url = strings.Replace(url, "{w}x{h}", config.CoverSize, 1)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
@ -1149,7 +1176,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
lrcFilename := fmt.Sprintf("%02d. %s.lrc", trackNum, forbiddenNames.ReplaceAllString(track.Attributes.Name, "_"))
trackPath := filepath.Join(sanAlbumFolder, filename)
var lrc string = ""
if userToken != "" {
if userToken != "" && (config.EmbedLrc || config.SaveLrcFile) {
ttml, err := getSongLyrics(track.ID, storefront, token, userToken)
if err != nil {
fmt.Println("Failed to get lyrics")
@ -1158,10 +1185,15 @@ func rip(albumId string, token string, storefront string, userToken string) erro
if err != nil {
fmt.Printf("Failed to parse lyrics: %s \n", err)
} else {
if config.SaveLrcFile {
err := writeLyrics(sanAlbumFolder, lrcFilename, lrc)
if err != nil {
fmt.Printf("Failed to write lyrics")
}
if !config.EmbedLrc {
lrc = ""
}
}
}
}
}
@ -1203,7 +1235,9 @@ func rip(albumId string, token string, storefront string, userToken string) erro
}
tags := []string{
fmt.Sprintf("lyrics=%s", lrc),
fmt.Sprintf("cover=%s/cover.jpg", sanAlbumFolder),
}
if config.EmbedCover {
tags = append(tags, fmt.Sprintf("cover=%s/cover.%s", sanAlbumFolder, config.CoverFormat))
}
tagsString := strings.Join(tags, ":")
cmd := exec.Command("MP4Box", "-itags", tagsString, trackPath)
@ -1217,12 +1251,10 @@ func rip(albumId string, token string, storefront string, userToken string) erro
}
func main() {
var mediaUserToken string
if _, err := os.Stat("media-user-token.txt"); err == nil {
file, err := os.ReadFile("media-user-token.txt")
if err == nil && file != nil {
mediaUserToken = string(file)
}
err := loadConfig()
if err != nil {
fmt.Printf("load config failed: %v", err)
return
}
token, err := getToken()
if err != nil {
@ -1243,7 +1275,7 @@ func main() {
fmt.Printf("Invalid URL: %s\n", url)
continue
}
err := rip(albumId, token, storefront, mediaUserToken)
err := rip(albumId, token, storefront, config.MediaUserToken)
if err != nil {
fmt.Println("Album failed.")
fmt.Println(err)

Loading…
Cancel
Save