Skip to content

Commit

Permalink
feat ✨: fix config init, feat set download dir, remove badly written …
Browse files Browse the repository at this point in the history
…tests

Signed-off-by: Victor Hang <[email protected]>
  • Loading branch information
Banh-Canh committed Sep 22, 2024
1 parent abb69ac commit 3a95b6b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 290 deletions.
16 changes: 12 additions & 4 deletions cmd/history.go → cmd/query_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/Banh-Canh/ytui/pkg/config"
"github.com/Banh-Canh/ytui/pkg/download"
"github.com/Banh-Canh/ytui/pkg/player"
"github.com/Banh-Canh/ytui/pkg/utils"
"github.com/Banh-Canh/ytui/pkg/youtube"
Expand Down Expand Up @@ -55,10 +57,16 @@ will be stored in there.`,
os.Exit(0)
}
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
player.RunMPV(videoURL)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
youtube.FeedHistory(selectedVideo)
if downloadFlag {
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := viper.GetString("download_dir")
download.RunYTDLP(videoURL, downloadDir)
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
player.RunMPV(videoURL)
youtube.FeedHistory(selectedVideo)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
}
},
}

Expand Down
17 changes: 16 additions & 1 deletion cmd/query_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package cmd

import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/Banh-Canh/ytui/pkg/config"
"github.com/Banh-Canh/ytui/pkg/download"
"github.com/Banh-Canh/ytui/pkg/player"
"github.com/Banh-Canh/ytui/pkg/utils"
Expand All @@ -31,6 +34,17 @@ Press enter to run any of the videos.`,
}
query := args[0]
utils.Logger.Info("Search command initiated.", zap.String("query", query))
configDir, err := config.GetConfigDirPath()
if err != nil {
utils.Logger.Fatal("Failed to get config path.", zap.Error(err))
os.Exit(1)
}
utils.Logger.Debug("Config directory retrieved.", zap.String("config_dir", configDir))
configPath := filepath.Join(configDir, "config.yaml")
if err := config.ReadConfig(configPath); err != nil {
utils.Logger.Fatal("Failed to read config.", zap.Error(err))
}
utils.Logger.Debug("Config file read successfully.", zap.String("config_file", configPath))

result, err := youtube.SearchVideos(query, false)
if err != nil {
Expand All @@ -52,7 +66,8 @@ Press enter to run any of the videos.`,
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
if downloadFlag {
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
download.RunYTDLP(videoURL)
downloadDir := viper.GetString("download_dir")
download.RunYTDLP(videoURL, downloadDir)
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
player.RunMPV(videoURL)
Expand Down
3 changes: 2 additions & 1 deletion cmd/query_subscribed.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ It will also only pick from the 50 most relevants subscribed channels in your Yo
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
if downloadFlag {
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
download.RunYTDLP(videoURL)
downloadDir := viper.GetString("download_dir")
download.RunYTDLP(videoURL, downloadDir)
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
player.RunMPV(videoURL)
Expand Down
5 changes: 2 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ func initConfig() {
configPath := filepath.Join(configDir, "config.yaml")
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("failed to read config file: %v", err)
fmt.Fprintf(os.Stderr, "error, couldn't read config file: %v\n", err)
os.Exit(1)
}

// Check if the logLevelFlag has been set, if not, fallback to config
var logLevelStr string
if logLevelFlag != "" {
logLevelStr = logLevelFlag // Use the flag if set
} else {
logLevelStr = viper.GetString("logLevel") // Use config value if flag is not set
logLevelStr = viper.GetString("loglevel") // Use config value if flag is not set
}

logLevel := zapcore.InfoLevel //nolint:all
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ type Config struct {
func CreateDefaultConfigFile(filePath string) {
// Struct with empty channels list
utils.Logger.Info("Config file set.", zap.String("filePath", filePath))
// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
utils.Logger.Error("Failed to get home directory.", zap.Error(err))
return
}
downloadDir := filepath.Join(homeDir, "Videos", "YouTube")
viper.SetDefault("download_dir", downloadDir)
viper.SetDefault("logLevel", "info")
viper.SetDefault("invidious", map[string]interface{}{
"instance": "invidious.jing.rocks",
Expand Down
71 changes: 0 additions & 71 deletions pkg/config/config_test.go

This file was deleted.

14 changes: 3 additions & 11 deletions pkg/download/yt-dlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ import (
"github.com/Banh-Canh/ytui/pkg/utils"
)

func RunYTDLP(videoPath string) {
func RunYTDLP(videoPath, outputPath string) {
utils.Logger.Debug("Downloading the video with yt-dlp...")

// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
utils.Logger.Error("Failed to get home directory.", zap.Error(err))
return
}

outputDir := filepath.Join(homeDir, "Videos", "YouTube")
err = os.MkdirAll(outputDir, os.ModePerm)
err := os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
utils.Logger.Error("Failed to create output directory.", zap.Error(err))
return
Expand All @@ -30,7 +22,7 @@ func RunYTDLP(videoPath string) {
"--format=bestvideo[ext=mp4][height<=?2160]+bestaudio[ext=m4a]",
"--mark-watched",
"--cookies-from-browser=firefox",
"-o", filepath.Join(outputDir, "%(title)s.%(ext)s"), // Set output path dynamically
"-o", filepath.Join(outputPath, "%(title)s.%(ext)s"), // Set output path dynamically
videoPath, // URL or video path
}
cmd := exec.Command("yt-dlp", args...)
Expand Down
61 changes: 0 additions & 61 deletions pkg/youtube/fzf_test.go

This file was deleted.

98 changes: 0 additions & 98 deletions pkg/youtube/history_test.go

This file was deleted.

Loading

0 comments on commit 3a95b6b

Please sign in to comment.