main.go (2265B)
1 // This program looks up words fromm Wiktionary, and creates Anki flashcards 2 // from them. 3 package main 4 5 import ( 6 "database/sql" 7 "flag" 8 "log" 9 "net/http" 10 "time" 11 12 tea "github.com/charmbracelet/bubbletea" 13 _ "github.com/mattn/go-sqlite3" 14 ) 15 16 func main() { 17 rawDict := flag.String( 18 "rawDictionary", 19 "raw-wiktextract-data.jsonl", 20 "Path to the raw wiktionary data. You can get this by downloading and gunzipping https://kaikki.org/frwiktionary/raw-wiktextract-data.jsonl.gz (for French).", 21 ) 22 dict := flag.String( 23 "dictionary", 24 "dictionary.sqlite3", 25 "Path to the parsed dictionary data. This will be generated from rawDictionary.", 26 ) 27 deckName := flag.String( 28 "deck", 29 "", 30 "Name of the deck where new Anki cards will be created.", 31 ) 32 modelName := flag.String( 33 "model", 34 "", 35 "Name of the card type ('model') for new Anki cards.", 36 ) 37 apiURL := flag.String( 38 "apiURL", 39 "http://localhost:8765", 40 "Base URL to access the anki-connect plugin API.", 41 ) 42 initialWord := flag.String("initialWord", 43 "", 44 "Optional: first word to look up on program launch.", 45 ) 46 47 flag.Parse() 48 49 if *dict == "" { 50 log.Fatal( 51 "The -dictionary flag cannot be empty. (Defaults to dictionary.sqlite3.)", 52 ) 53 } 54 if *rawDict == "" { 55 log.Fatal( 56 "The -rawDictionary flag cannot be empty. (Defaults to raw-wiktextract-data.jsonl.)", 57 ) 58 } 59 if *deckName == "" { 60 log.Fatal("The -deck flag must be provided (name of the deck where Anki cards will be created).") 61 } 62 if *modelName == "" { 63 log.Fatal( 64 "The -model flag must be provided. This is the name of the card type ('model') for any Anki cards created by this program. This appears under 'Type' on the dialog for creating new cards in Anki.", 65 ) 66 } 67 68 db, err := sql.Open("sqlite3", *dict) 69 if err != nil { 70 log.Fatalf("Failed to create or open dictionary at '%s': %s", *dict, err) 71 } 72 defer db.Close() 73 if err = setupTables(db); err != nil { 74 log.Fatalf("Failed to create database tables in dictionary '%s': %s", *dict, err) 75 } 76 77 c := http.DefaultClient 78 c.Timeout = 5 * time.Second 79 80 p := tea.NewProgram(initialModel(c, db, *apiURL, *deckName, *modelName, *rawDict, *initialWord)) 81 if _, err := p.Run(); err != nil { 82 log.Fatalf("Unexpected error encountered while running program: %s", err) 83 } 84 }