ncmdump-go/main.go

107 lines
2.3 KiB
Go
Raw Normal View History

2024-09-14 22:39:50 +08:00
package main
import (
"fmt"
2024-09-15 21:37:11 +08:00
"github.com/taurusxin/ncmdump-go/ncmcrypt"
"github.com/taurusxin/ncmdump-go/utils"
2024-09-14 22:39:50 +08:00
"os"
"path/filepath"
2024-09-15 08:59:07 +08:00
flag "github.com/spf13/pflag"
2024-09-14 22:39:50 +08:00
)
func processFile(filePath string) error {
currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath)
if err != nil {
2024-09-15 21:58:12 +08:00
utils.ErrorPrintfln("Reading '%s' failed: %s", filePath, err.Error())
2024-09-14 22:39:50 +08:00
return err
}
2024-09-15 21:04:52 +08:00
dump, err := currentFile.Dump(filepath.Dir(filePath))
2024-09-14 22:39:50 +08:00
if err != nil {
2024-09-15 21:58:12 +08:00
utils.ErrorPrintfln("Processing '%s' failed: %s", filePath, err.Error())
2024-09-14 22:39:50 +08:00
return err
}
if dump {
2024-09-15 21:37:11 +08:00
metadata, err := currentFile.FixMetadata(true)
2024-09-14 22:39:50 +08:00
if !metadata {
2024-09-15 21:58:12 +08:00
utils.WarningPrintfln("Fix metadata for '%s' failed: %s", filePath, err.Error())
2024-09-14 22:39:50 +08:00
return err
}
2024-09-15 08:59:07 +08:00
utils.DonePrintfln("'%s' -> '%s'", filePath, currentFile.GetDumpFilePath())
2024-09-14 22:39:50 +08:00
}
return nil
}
func main() {
var folderPath string
showHelp := flag.BoolP("help", "h", false, "Display help message")
showVersion := flag.BoolP("version", "v", false, "Display version information")
flag.StringVarP(&folderPath, "dir", "d", "", "Process all files in the directory")
flag.Parse()
if len(os.Args) == 1 {
flag.Usage()
os.Exit(0)
}
if *showHelp {
flag.Usage()
os.Exit(0)
}
if *showVersion {
fmt.Println("ncmdump version 1.5.0")
os.Exit(0)
}
if folderPath != "" {
// check if the folder exists
info, err := os.Stat(folderPath)
if err != nil {
2024-09-15 08:59:07 +08:00
utils.ErrorPrintfln("Unable to access directory: '%s'", folderPath)
2024-09-14 22:39:50 +08:00
os.Exit(1)
}
if !info.IsDir() {
2024-09-15 21:58:12 +08:00
utils.ErrorPrintfln("Not a directory: %s", folderPath)
2024-09-14 22:39:50 +08:00
os.Exit(1)
}
// dump files in the folder
files, err := os.ReadDir(folderPath)
if err != nil {
2024-09-15 08:59:07 +08:00
utils.ErrorPrintfln("Unable to read directory: '%s'", folderPath)
2024-09-14 22:39:50 +08:00
os.Exit(1)
}
for _, file := range files {
if file.IsDir() {
continue
}
filePath := filepath.Join(folderPath, file.Name())
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
continue
}
err = processFile(filePath)
if err != nil {
continue
}
}
} else {
// dump file from args
for _, filePath := range flag.Args() {
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
continue
}
err := processFile(filePath)
if err != nil {
continue
}
}
}
}