add config.yaml support
This commit is contained in:
parent
3089c5b5bf
commit
112dfff400
@ -5,6 +5,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/beevik/etree"
|
"github.com/beevik/etree"
|
||||||
@ -35,6 +36,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
|
||||||
|
|
||||||
type SampleInfo struct {
|
type SampleInfo struct {
|
||||||
data []byte
|
data []byte
|
||||||
@ -48,6 +61,20 @@ type SongInfo struct {
|
|||||||
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 {
|
||||||
ret += uint64(s.samples[i].duration)
|
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 {
|
func writeCover(sanAlbumFolder, url string) error {
|
||||||
covPath := filepath.Join(sanAlbumFolder, "cover.jpg")
|
covPath := filepath.Join(sanAlbumFolder, "cover." + config.CoverFormat)
|
||||||
exists, err := fileExists(covPath)
|
exists, err := fileExists(covPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to check if cover exists.")
|
fmt.Println("Failed to check if cover exists.")
|
||||||
@ -1029,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
|
||||||
@ -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, "_"))
|
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")
|
||||||
@ -1158,10 +1185,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 = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1203,7 +1235,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)
|
||||||
@ -1217,12 +1251,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 {
|
||||||
@ -1243,7 +1275,7 @@ func main() {
|
|||||||
fmt.Printf("Invalid URL: %s\n", url)
|
fmt.Printf("Invalid URL: %s\n", url)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := rip(albumId, token, storefront, mediaUserToken)
|
err := rip(albumId, token, storefront, config.MediaUserToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Album failed.")
|
fmt.Println("Album failed.")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user