addet a config file
This commit is contained in:
parent
ac8eafd44c
commit
20ab5804c6
3 changed files with 58 additions and 14 deletions
32
config/config.go
Normal file
32
config/config.go
Normal file
|
@ -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
|
||||||
|
}
|
7
main.go
7
main.go
|
@ -4,10 +4,17 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"ytgo/config"
|
||||||
"ytgo/tui"
|
"ytgo/tui"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
_, err := config.LoadConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error loading configuration: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
if err := tui.Run(); err != nil {
|
if err := tui.Run(); err != nil {
|
||||||
log.Fatalf("Error running application: %v", err)
|
log.Fatalf("Error running application: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -4,10 +4,13 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// "log"
|
// "log"
|
||||||
"html"
|
"html"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"ytgo/config"
|
||||||
|
|
||||||
"google.golang.org/api/option"
|
"google.golang.org/api/option"
|
||||||
"google.golang.org/api/youtube/v3"
|
"google.golang.org/api/youtube/v3"
|
||||||
)
|
)
|
||||||
|
@ -22,9 +25,6 @@ type Video struct {
|
||||||
UploadDate string
|
UploadDate string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace with your actual API key
|
|
||||||
const API_KEY = "AIzaSyAzsihRkp8mYTOXLOkVN09yTqld9TJ4Nts"
|
|
||||||
|
|
||||||
func formatViews(count uint64) string {
|
func formatViews(count uint64) string {
|
||||||
switch {
|
switch {
|
||||||
case count >= 1000000000:
|
case count >= 1000000000:
|
||||||
|
@ -113,8 +113,13 @@ func formatUploadDate(uploadDate string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchVideos(query string) ([]Video, error) {
|
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()
|
ctx := context.Background()
|
||||||
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(API_KEY))
|
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(cfg.APIKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating YouTube client: %w", err)
|
return nil, fmt.Errorf("error creating YouTube client: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -134,10 +139,10 @@ func FetchVideos(query string) ([]Video, error) {
|
||||||
var videos []Video
|
var videos []Video
|
||||||
for _, item := range response.Items {
|
for _, item := range response.Items {
|
||||||
video := Video{
|
video := Video{
|
||||||
Title: item.Snippet.Title,
|
Title: item.Snippet.Title,
|
||||||
URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id.VideoId),
|
URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id.VideoId),
|
||||||
Channel: item.Snippet.ChannelTitle,
|
Channel: item.Snippet.ChannelTitle,
|
||||||
Thumbnail: item.Snippet.Thumbnails.Default.Url,
|
Thumbnail: item.Snippet.Thumbnails.Default.Url,
|
||||||
UploadDate: item.Snippet.PublishedAt,
|
UploadDate: item.Snippet.PublishedAt,
|
||||||
}
|
}
|
||||||
videos = append(videos, video)
|
videos = append(videos, video)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue