add config.yaml support

pull/14/head
itouakirai 7 months ago committed by GitHub
parent af454be33c
commit 03b3b18223
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 51
      main.go

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"gopkg.in/yaml.v2"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -34,6 +35,18 @@ const (
var ( var (
forbiddenNames = regexp.MustCompile(`[/\\<>:"|?*]`) 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
var oktrackNum int = 0 var oktrackNum int = 0
var trackTotalnum int = 0 var trackTotalnum int = 0
@ -48,6 +61,19 @@ type SongInfo struct {
alacParam *Alac alacParam *Alac
samples []SampleInfo 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) { func (s *SongInfo) Duration() (ret uint64) {
for i := range s.samples { for i := range s.samples {
@ -1030,7 +1056,7 @@ func writeCover(sanAlbumFolder, url string) error {
if exists { if exists {
return nil 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) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
return err return err
@ -1081,7 +1107,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
singerFoldername = strings.ReplaceAll(singerFoldername, ".", "") singerFoldername = strings.ReplaceAll(singerFoldername, ".", "")
} }
singerFoldername = strings.TrimSpace(singerFoldername) singerFoldername = strings.TrimSpace(singerFoldername)
singerFolder := filepath.Join("AM-DL downloads", forbiddenNames.ReplaceAllString(singerFoldername, "_")) singerFolder := filepath.Join(config.AlacSaveFolder, forbiddenNames.ReplaceAllString(singerFoldername, "_"))
albumFolder := fmt.Sprintf("%s", meta.Data[0].Attributes.Name) albumFolder := fmt.Sprintf("%s", meta.Data[0].Attributes.Name)
if strings.HasSuffix(albumFolder, ".") { if strings.HasSuffix(albumFolder, ".") {
albumFolder = strings.ReplaceAll(albumFolder, ".", "") albumFolder = strings.ReplaceAll(albumFolder, ".", "")
@ -1113,7 +1139,7 @@ func rip(albumId string, token string, storefront string, userToken string) erro
lrcFilename := fmt.Sprintf("%02d. %s.lrc", trackNum, forbiddenNames.ReplaceAllString(track.Attributes.Name, "_")) lrcFilename := fmt.Sprintf("%02d. %s.lrc", trackNum, forbiddenNames.ReplaceAllString(track.Attributes.Name, "_"))
trackPath := filepath.Join(sanAlbumFolder, filename) trackPath := filepath.Join(sanAlbumFolder, filename)
var lrc string = "" var lrc string = ""
if userToken != "" { if userToken != "" && (config.EmbedLrc || config.SaveLrcFile) {
ttml, err := getSongLyrics(track.ID, storefront, token, userToken) ttml, err := getSongLyrics(track.ID, storefront, token, userToken)
if err != nil { if err != nil {
fmt.Println("Failed to get lyrics") fmt.Println("Failed to get lyrics")
@ -1122,10 +1148,15 @@ func rip(albumId string, token string, storefront string, userToken string) erro
if err != nil { if err != nil {
fmt.Printf("Failed to parse lyrics: %s \n", err) fmt.Printf("Failed to parse lyrics: %s \n", err)
} else { } else {
if config.SaveLrcFile {
err := writeLyrics(sanAlbumFolder, lrcFilename, lrc) err := writeLyrics(sanAlbumFolder, lrcFilename, lrc)
if err != nil { if err != nil {
fmt.Printf("Failed to write lyrics") fmt.Printf("Failed to write lyrics")
} }
if !config.EmbedLrc {
lrc = ""
}
}
} }
} }
} }
@ -1168,7 +1199,9 @@ func rip(albumId string, token string, storefront string, userToken string) erro
} }
tags := []string{ tags := []string{
fmt.Sprintf("lyrics=%s", lrc), 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, ":") tagsString := strings.Join(tags, ":")
cmd := exec.Command("MP4Box", "-itags", tagsString, trackPath) cmd := exec.Command("MP4Box", "-itags", tagsString, trackPath)
@ -1182,12 +1215,10 @@ func rip(albumId string, token string, storefront string, userToken string) erro
} }
func main() { func main() {
var mediaUserToken string err := loadConfig()
if _, err := os.Stat("media-user-token.txt"); err == nil { if err != nil {
file, err := os.ReadFile("media-user-token.txt") fmt.Printf("load config failed: %v", err)
if err == nil && file != nil { return
mediaUserToken = string(file)
}
} }
token, err := getToken() token, err := getToken()
if err != nil { if err != nil {

Loading…
Cancel
Save