From 20ab5804c6a59d8e2630c9cb00ae9cfe822a7196 Mon Sep 17 00:00:00 2001 From: pika Date: Wed, 11 Dec 2024 00:11:58 +0100 Subject: [PATCH] addet a config file --- config/config.go | 32 ++++++++++++++++++++++++++++++++ main.go | 7 +++++++ scraper/scraper.go | 33 +++++++++++++++++++-------------- 3 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..06cdce2 --- /dev/null +++ b/config/config.go @@ -0,0 +1,32 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/BurntSushi/toml" +) + +type Config struct { + APIKey string `toml:"api_key"` +} + +func LoadConfig() (*Config, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, fmt.Errorf("could not determine home directory: %w", err) + } + + configPath := filepath.Join(homeDir, ".config", "ytgo", "config.toml") + var config Config + if _, err := toml.DecodeFile(configPath, &config); err != nil { + return nil, fmt.Errorf("could not read config file: %w", err) + } + + if config.APIKey == "" { + return nil, fmt.Errorf("API key is missing in config file") + } + + return &config, nil +} diff --git a/main.go b/main.go index 0b8f341..712ca13 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,17 @@ import ( "log" "os" + "ytgo/config" "ytgo/tui" ) func main() { + _, err := config.LoadConfig() + if err != nil { + log.Fatalf("Error loading configuration: %v", err) + os.Exit(1) + } + if err := tui.Run(); err != nil { log.Fatalf("Error running application: %v", err) os.Exit(1) diff --git a/scraper/scraper.go b/scraper/scraper.go index 71e5730..8d58b2a 100644 --- a/scraper/scraper.go +++ b/scraper/scraper.go @@ -4,10 +4,13 @@ import ( "context" "fmt" "time" + // "log" "html" "strings" + "ytgo/config" + "google.golang.org/api/option" "google.golang.org/api/youtube/v3" ) @@ -22,9 +25,6 @@ type Video struct { UploadDate string } -// Replace with your actual API key -const API_KEY = "AIzaSyAzsihRkp8mYTOXLOkVN09yTqld9TJ4Nts" - func formatViews(count uint64) string { switch { case count >= 1000000000: @@ -41,16 +41,16 @@ func formatViews(count uint64) string { func formatDuration(duration string) string { // Remove PT from the start duration = strings.TrimPrefix(duration, "PT") - + var result strings.Builder - + // Handle hours if i := strings.Index(duration, "H"); i != -1 { result.WriteString(duration[:i]) result.WriteString(":") duration = duration[i+1:] } - + // Handle minutes if i := strings.Index(duration, "M"); i != -1 { minutes := duration[:i] @@ -63,7 +63,7 @@ func formatDuration(duration string) string { } else if result.Len() > 0 { result.WriteString("00:") } - + // Handle seconds if i := strings.Index(duration, "S"); i != -1 { seconds := duration[:i] @@ -74,7 +74,7 @@ func formatDuration(duration string) string { } else { result.WriteString("00") } - + return result.String() } @@ -89,7 +89,7 @@ func formatUploadDate(uploadDate string) string { days := int(diff.Hours() / 24) formattedDate := t.Format("02-01-2006") - + // If video is less than 30 days old, add "X days ago" if days < 30 { var timeAgo string @@ -113,8 +113,13 @@ func formatUploadDate(uploadDate string) string { } func FetchVideos(query string) ([]Video, error) { + cfg, err := config.LoadConfig() + if err != nil { + return nil, fmt.Errorf("error loading config: %w", err) + } + ctx := context.Background() - youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(API_KEY)) + youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(cfg.APIKey)) if err != nil { return nil, fmt.Errorf("error creating YouTube client: %w", err) } @@ -134,10 +139,10 @@ func FetchVideos(query string) ([]Video, error) { var videos []Video for _, item := range response.Items { video := Video{ - Title: item.Snippet.Title, - URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id.VideoId), - Channel: item.Snippet.ChannelTitle, - Thumbnail: item.Snippet.Thumbnails.Default.Url, + Title: item.Snippet.Title, + URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id.VideoId), + Channel: item.Snippet.ChannelTitle, + Thumbnail: item.Snippet.Thumbnails.Default.Url, UploadDate: item.Snippet.PublishedAt, } videos = append(videos, video)