-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
248 lines (211 loc) Β· 6.84 KB
/
main.go
File metadata and controls
248 lines (211 loc) Β· 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
"github.com/weazyexe/yuki-cli/internal"
)
var (
count int
output string
level string
apiURL string
apiKey string
model string
noReview bool
noCache bool
clearCache bool
refreshCache bool
)
func main() {
rootCmd := &cobra.Command{
Use: "yuki [flags] <youtube-url|file>",
Short: "Convert YouTube videos or subtitle files to Anki flashcard decks",
Long: "CLI utility that extracts vocabulary from YouTube videos or subtitle/text files and creates Anki decks",
Args: cobra.MaximumNArgs(1),
RunE: run,
}
rootCmd.Flags().IntVarP(&count, "count", "n", 20, "Number of words to extract")
rootCmd.Flags().StringVarP(&output, "output", "o", "deck.apkg", "Output file path")
rootCmd.Flags().StringVarP(&level, "level", "l", "B1", "Language level: A2, B1, B2")
rootCmd.Flags().StringVar(&apiURL, "api-url", "http://localhost:11434/v1", "OpenAI-compatible API URL")
rootCmd.Flags().StringVar(&apiKey, "api-key", "", "API key (or env: OPENAI_API_KEY)")
rootCmd.Flags().StringVar(&model, "model", "gpt-4o-mini", "LLM model name")
rootCmd.Flags().BoolVar(&noReview, "no-review", false, "Skip interactive review, add all words")
rootCmd.Flags().BoolVar(&noCache, "no-cache", false, "Disable cache for this run")
rootCmd.Flags().BoolVar(&clearCache, "clear-cache", false, "Clear cache and exit")
rootCmd.Flags().BoolVar(&refreshCache, "refresh", false, "Re-download and re-transcribe (ignore cache)")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) error {
// Handle --clear-cache
if clearCache {
cache, err := internal.NewCache()
if err != nil {
return fmt.Errorf("failed to initialize cache: %w", err)
}
if err := cache.Clear(); err != nil {
return fmt.Errorf("failed to clear cache: %w", err)
}
fmt.Println("Cache cleared successfully")
return nil
}
// Require input for normal operation
if len(args) == 0 {
return fmt.Errorf("YouTube URL or file path required")
}
input := args[0]
// Detect input type early to provide better error messages
inputType := internal.DetectInputType(input)
if inputType == internal.InputTypeUnknown {
return fmt.Errorf("input must be a valid YouTube URL or existing file: %s", input)
}
// Start timing
startTime := time.Now()
// Validate level
validLevels := map[string]bool{"A2": true, "B1": true, "B2": true}
if !validLevels[level] {
return fmt.Errorf("invalid level: %s (must be A2, B1, or B2)", level)
}
// Get API key from flag or environment
if apiKey == "" {
apiKey = os.Getenv("OPENAI_API_KEY")
}
if apiKey == "" {
return fmt.Errorf("API key required: use --api-key flag or set OPENAI_API_KEY environment variable")
}
// Get transcript based on input type
var transcript string
var err error
switch inputType {
case internal.InputTypeYouTube:
transcript, err = processYouTube(input)
case internal.InputTypeFile:
transcript, err = processFile(input)
}
if err != nil {
return err
}
// Extract vocabulary
llmClient := internal.NewLLMClient(apiURL, apiKey, model)
vocabulary, err := llmClient.ExtractVocabulary(transcript, count, level)
if err != nil {
return fmt.Errorf("vocabulary extraction failed: %w", err)
}
// Print total time before review
totalTime := time.Since(startTime)
fmt.Printf("\nExtracted %d words\n", len(vocabulary))
fmt.Printf("Total time: %s\n", internal.FormatDuration(totalTime))
// Interactive review
if !noReview {
vocabulary = internal.ReviewVocabulary(vocabulary)
if len(vocabulary) == 0 {
fmt.Println("No words selected. Exiting.")
return nil
}
}
fmt.Println("\nGenerating Anki deck...")
deckName := filepath.Base(output)
deckName = deckName[:len(deckName)-len(filepath.Ext(deckName))]
if err := internal.GenerateAPKG(vocabulary, output, deckName); err != nil {
return fmt.Errorf("APKG generation failed: %w", err)
}
fmt.Printf("Deck saved to: %s\n", output)
return nil
}
// processYouTube handles YouTube URL input with download and transcription
func processYouTube(url string) (string, error) {
// Check external dependencies
if err := internal.CheckYouTubeDependencies(); err != nil {
return "", err
}
// Extract video ID for caching
videoID, err := internal.ExtractVideoID(url)
if err != nil {
return "", fmt.Errorf("failed to extract video ID: %w", err)
}
// Initialize cache (unless disabled)
var cache *internal.Cache
useCache := !noCache
if useCache {
cache, err = internal.NewCache()
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not initialize cache: %v\n", err)
useCache = false
}
}
var audioPath string
var transcript string
// Check cache for transcript (most valuable to cache)
if useCache && !refreshCache && cache.HasTranscript(videoID) {
fmt.Printf("Using cached transcript for %s\n", videoID)
transcript, err = cache.GetTranscript(videoID)
if err != nil {
return "", fmt.Errorf("failed to read cached transcript: %w", err)
}
return transcript, nil
}
// Need to download and/or transcribe
// Check cache for audio
if useCache && !refreshCache && cache.HasAudio(videoID) {
fmt.Printf("Using cached audio for %s\n", videoID)
audioPath = cache.AudioPath(videoID)
} else {
// Download audio
tempDir, err := os.MkdirTemp("", "yuki-*")
if err != nil {
return "", fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tempDir)
audioPath, err = internal.DownloadAudio(url, tempDir)
if err != nil {
return "", fmt.Errorf("download failed: %w", err)
}
// Cache the audio
if useCache {
if err := cache.SaveAudio(videoID, audioPath); err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not cache audio: %v\n", err)
} else {
audioPath = cache.AudioPath(videoID)
}
}
}
// Transcribe
tempDir, err := os.MkdirTemp("", "yuki-transcribe-*")
if err != nil {
return "", fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tempDir)
transcript, err = internal.Transcribe(audioPath, tempDir)
if err != nil {
return "", fmt.Errorf("transcription failed: %w", err)
}
// Cache the transcript
if useCache {
if err := cache.SaveTranscript(videoID, transcript); err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not cache transcript: %v\n", err)
}
}
return transcript, nil
}
// processFile handles file input (SRT, VTT, TXT)
func processFile(filePath string) (string, error) {
// Validate file exists and is not a directory
info, err := os.Stat(filePath)
if err != nil {
return "", fmt.Errorf("cannot access file: %w", err)
}
if info.IsDir() {
return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
}
fmt.Printf("Parsing file: %s\n", filePath)
transcript, err := internal.ParseFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to parse file: %w", err)
}
return transcript, nil
}