refactor: major project restructure
This commit is contained in:
+35
-210
@@ -1,231 +1,56 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/flytam/filenamify"
|
||||
"github.com/mathismqn/godeez/internal/config"
|
||||
"github.com/mathismqn/godeez/internal/db"
|
||||
"github.com/mathismqn/godeez/internal/deezer"
|
||||
"github.com/mathismqn/godeez/internal/tags"
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/mathismqn/godeez/internal/downloader"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
outputDir string
|
||||
quality string
|
||||
)
|
||||
var opts downloader.Options
|
||||
|
||||
var downloadCmd = &cobra.Command{
|
||||
Use: "download",
|
||||
Short: "Download songs from Deezer",
|
||||
}
|
||||
|
||||
type bpmResult struct {
|
||||
tempo, key string
|
||||
err error
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(downloadCmd)
|
||||
downloadCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "output directory (default is $HOME/Music/GoDeez)")
|
||||
downloadCmd.PersistentFlags().StringVarP(&quality, "quality", "q", "", "download quality [mp3_128, mp3_320, flac, best] (default is best)")
|
||||
|
||||
downloadCmd.PersistentFlags().StringVarP(&opts.OutputDir, "output", "o", "", "output directory (default is $HOME/Music/GoDeez)")
|
||||
downloadCmd.PersistentFlags().StringVarP(&opts.Quality, "quality", "q", "", "download quality [mp3_128, mp3_320, flac, best] (default is best)")
|
||||
|
||||
downloadCmd.AddCommand(
|
||||
newDownloadCmd("album"),
|
||||
newDownloadCmd("playlist"),
|
||||
)
|
||||
}
|
||||
|
||||
func validateInput() {
|
||||
if outputDir == "" {
|
||||
outputDir = appDir
|
||||
func newDownloadCmd(resourceType string) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: fmt.Sprintf("%s [%s_id...]", resourceType, resourceType),
|
||||
Short: fmt.Sprintf("Download songs from one or more %ss", resourceType),
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return opts.Validate(appCtx.AppDir)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
dl := downloader.New(appCtx, resourceType)
|
||||
|
||||
if err := dl.Run(ctx, opts, args); err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
if quality == "" {
|
||||
quality = "best"
|
||||
}
|
||||
|
||||
validQualities := map[string]bool{
|
||||
"mp3_128": true,
|
||||
"mp3_320": true,
|
||||
"flac": true,
|
||||
"best": true,
|
||||
}
|
||||
if !validQualities[quality] {
|
||||
fmt.Fprintf(os.Stderr, "Error: invalid quality option: %s\n", quality)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func downloadContent(contentType string, args []string) {
|
||||
session, err := deezer.Authenticate(config.Cfg.ArlCookie)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not authenticate: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
nArgs := len(args)
|
||||
separator := "--------------------------------------------------"
|
||||
|
||||
for i, id := range args {
|
||||
fmt.Println(separator)
|
||||
fmt.Printf("[%d/%d] Getting data for %s %s...", i+1, nArgs, contentType, id)
|
||||
|
||||
var resource deezer.Resource
|
||||
var songs []*deezer.Song
|
||||
|
||||
switch contentType {
|
||||
case "album":
|
||||
album := &deezer.Album{}
|
||||
if err := session.GetData(album, id); err != nil {
|
||||
fmt.Printf("\r[%d/%d] Getting data for album %s... FAILED\n", i+1, nArgs, id)
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get album data: %v\n", err)
|
||||
|
||||
continue
|
||||
}
|
||||
resource = album
|
||||
songs = album.GetSongs()
|
||||
case "playlist":
|
||||
playlist := &deezer.Playlist{}
|
||||
if err := session.GetData(playlist, id); err != nil {
|
||||
fmt.Printf("\r[%d/%d] Getting data for playlist %s... FAILED\n", i+1, nArgs, id)
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get playlist data: %v\n", err)
|
||||
|
||||
continue
|
||||
}
|
||||
if playlist.Results.Data.Status == 1 && playlist.Results.Data.CollabKey == "" {
|
||||
fmt.Printf("\r[%d/%d] Getting data for playlist %s... FAILED\n", i+1, nArgs, id)
|
||||
fmt.Fprintf(os.Stderr, "Error: playlist is private and no valid arl cookie was provided\n")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
resource = playlist
|
||||
songs = playlist.GetSongs()
|
||||
}
|
||||
|
||||
fmt.Printf("\r[%d/%d] Getting data for %s %s... DONE\n", i+1, nArgs, contentType, id)
|
||||
|
||||
output := resource.GetOutputPath(outputDir)
|
||||
if err := utils.EnsureDir(output); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create output directory: %v\n", err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
title := resource.GetTitle()
|
||||
fmt.Printf("Starting download of %s: %s\n", contentType, title)
|
||||
|
||||
for _, song := range songs {
|
||||
songTitle := song.Title
|
||||
if song.Version != "" {
|
||||
songTitle = fmt.Sprintf("%s %s", song.Title, song.Version)
|
||||
}
|
||||
|
||||
media, err := song.GetMediaData(session.LicenseToken, quality)
|
||||
if err != nil {
|
||||
fmt.Printf(" Downloading %s... FAILED\n", songTitle)
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get media data: %v\n", err)
|
||||
if err.Error() == "invalid license token" {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if len(media.Data) == 0 || len(media.Data[0].Media) == 0 || len(media.Data[0].Media[0].Sources) == 0 {
|
||||
fmt.Printf(" Downloading %s... FAILED\n", songTitle)
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get media sources\n")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
url := media.Data[0].Media[0].Sources[0].URL
|
||||
for _, source := range media.Data[0].Media[0].Sources {
|
||||
if source.Provider == "ak" {
|
||||
url = source.URL
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
ext := "mp3"
|
||||
if media.Data[0].Media[0].Format == "FLAC" {
|
||||
ext = "flac"
|
||||
}
|
||||
trackNumber := ""
|
||||
if contentType == "album" {
|
||||
trackNumber = song.TrackNumber + "."
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%s %s - %s.%s", trackNumber, songTitle, strings.Join(song.Contributors.MainArtists, ", "), ext)
|
||||
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{})
|
||||
filePath := path.Join(output, fileName)
|
||||
|
||||
if existing, err := db.Get(song.ID); err == nil && existing.Quality == media.Data[0].Media[0].Format {
|
||||
if utils.FileExists(existing.Path) {
|
||||
fmt.Printf(" Skipping %s (already downloaded at %s)\n", songTitle, existing.Path)
|
||||
continue
|
||||
}
|
||||
if existing.Hash != "" {
|
||||
if foundPath, err := utils.FindFileByHash(appDir, existing.Hash); err == nil && foundPath != "" {
|
||||
existing.Path = foundPath
|
||||
_ = existing.Save()
|
||||
fmt.Printf(" Recovered %s at %s, skipping download\n", songTitle, foundPath)
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf(" Downloading %s...", songTitle)
|
||||
|
||||
bpmCh := make(chan bpmResult, 1)
|
||||
go func() {
|
||||
t, k, e := song.GetTempoAndKey()
|
||||
bpmCh <- bpmResult{tempo: t, key: k, err: e}
|
||||
}()
|
||||
|
||||
err = media.Download(url, filePath, song.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("\r Downloading %s... FAILED\n", songTitle)
|
||||
fmt.Fprintf(os.Stderr, "Error: could not download song: %v\n", err)
|
||||
utils.DeleteFile(filePath)
|
||||
|
||||
continue
|
||||
}
|
||||
fmt.Printf("\r Downloading %s... DONE\n", songTitle)
|
||||
|
||||
res := <-bpmCh
|
||||
if res.err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not get tempo/key for %s: %v\n", songTitle, res.err)
|
||||
} else {
|
||||
fmt.Printf(" Tempo: %s\n", res.tempo)
|
||||
fmt.Printf(" Key: %s\n", res.key)
|
||||
}
|
||||
|
||||
if err := tags.AddTags(resource, song, filePath, res.tempo, res.key); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not add tags to song: %v\n", err)
|
||||
}
|
||||
|
||||
hash, err := utils.GetFileHash(filePath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not get file hash: %v\n", err)
|
||||
}
|
||||
|
||||
info := &db.DownloadInfo{
|
||||
SongID: song.ID,
|
||||
Quality: media.Data[0].Media[0].Format,
|
||||
Path: filePath,
|
||||
Hash: hash,
|
||||
Downloaded: time.Now(),
|
||||
}
|
||||
if err := info.Save(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not save download info: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(separator)
|
||||
fmt.Println("All downloads completed")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var albumCmd = &cobra.Command{
|
||||
Use: "album [album_id...]",
|
||||
Short: "Download songs from one or more albums",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
validateInput()
|
||||
downloadContent("album", args)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
downloadCmd.AddCommand(albumCmd)
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var playlistCmd = &cobra.Command{
|
||||
Use: "playlist [playlist_id...]",
|
||||
Short: "Download songs from one or more playlists",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
validateInput()
|
||||
downloadContent("playlist", args)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
downloadCmd.AddCommand(playlistCmd)
|
||||
}
|
||||
+12
-48
@@ -1,63 +1,27 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/config"
|
||||
"github.com/mathismqn/godeez/internal/db"
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/mathismqn/godeez/internal/app"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgDir string
|
||||
musicDir string
|
||||
appDir string
|
||||
cfgFile string
|
||||
cfgPath string
|
||||
appCtx *app.Context
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "godeez",
|
||||
Short: "GoDeez is a tool to download music from Deezer",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
Use: "godeez",
|
||||
Short: "GoDeez is a tool to download music from Deezer",
|
||||
SilenceUsage: true,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
appCtx, err = app.NewContext(cfgPath)
|
||||
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
|
||||
cobra.OnInitialize(func() {
|
||||
initDirs()
|
||||
config.Init(cfgFile, cfgDir)
|
||||
db.Init(cfgDir)
|
||||
})
|
||||
}
|
||||
|
||||
func initDirs() {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfgDir = filepath.Join(home, ".godeez")
|
||||
if err := utils.EnsureDir(cfgDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create app directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
musicDir = filepath.Join(home, "Music")
|
||||
if err := utils.EnsureDir(musicDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create music directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
appDir = path.Join(musicDir, "GoDeez")
|
||||
if err := utils.EnsureDir(appDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create GoDeez directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
RootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "config file (default is $HOME/.godeez)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user