Repository navigation
Expand file tree
/
Copy pathfetch.go
More file actions
63 lines (53 loc) · 1.95 KB
/
Copy pathfetch.go
File metadata and controls
63 lines (53 loc) · 1.95 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
)
// fetchCmd implements the Command interface for the 'fetch' command.
type fetchCmd struct{}
func (c *fetchCmd) Execute(ctx context.Context) error {
fetchFlagSet := flag.NewFlagSet("fetch", flag.ExitOnError)
yearPtr := fetchFlagSet.Int("year", 0, "The year to filter records for (required).")
offsetPtr := fetchFlagSet.Int("offset", 0, "The starting offset for fetching records.")
batchSizePtr := fetchFlagSet.Int("batch", 1000, "The number of records to fetch per API call (the 'limit').")
dbFile := fetchFlagSet.String("db", "kcc_data.db", "optional sqlite 3 database file name")
retriesPtr := fetchFlagSet.Int("retries", 3, "Number of retries for failed HTTP requests (default 3).")
backoffPtr := fetchFlagSet.Int("backoff", 500, "Initial backoff in milliseconds for retries (default 500ms).")
fetchFlagSet.Usage = func() {
fmt.Println(c.Help())
fmt.Println("\nFlags:")
fetchFlagSet.PrintDefaults()
}
fetchFlagSet.Parse(os.Args[2:])
if *yearPtr == 0 {
fmt.Println("Error: The --year flag is required.")
fetchFlagSet.Usage()
os.Exit(1)
}
apiKey := os.Getenv(apiKeyEnvVar)
if apiKey == "" {
log.Fatalf("Error: API Key not found. Please set the %s environment variable.", apiKeyEnvVar)
}
db, err := initDB(*dbFile)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
defer db.Close()
err = fetchAllAndSaveData(ctx, db, apiKey, *yearPtr, *offsetPtr, *batchSizePtr, *retriesPtr, *backoffPtr)
if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded {
log.Println("Operation cancelled by user.")
return err
} else {
log.Fatalf("An error occurred: %v", err)
return err
}
}
return nil
}
func (c *fetchCmd) Help() string {
return "Fetches KCC transcripts from the API and saves them to a SQLite database.\nUsage: kcc fetch --year <year> [--offset <offset>] [--batch <size>] [--db <dbfile>] [--retries <count>] [--backoff <ms>]"
}