anki-generator/anki_generator/main.py

64 lines
2.1 KiB
Python

import os
import argparse
from datetime import datetime
from typing import List, Tuple
from pathlib import Path
from anki_generator.clients.llm import AnthropicClient
from anki_generator.clients.unsplash import UnsplashClient
from anki_generator.card_generator import CardGenerator, GermanDeckPackage
def read_word_list(file_path: str) -> List[Tuple[str, str]]:
"""Read word list from file"""
words = []
print(f"Reading file: {file_path}")
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
print(f"Processing line: {line.strip()}")
# Expected format: word,source
word, source = line.strip().split(',', 1)
words.append((word.strip(), source.strip()))
print(f"Added word: {word.strip()} with source: {source.strip()}")
return words
def main():
parser = argparse.ArgumentParser(description='Generate Anki cards for German vocabulary')
parser.add_argument('--input', required=True, help='Input file with words and sources')
parser.add_argument('--output', help='Output file name (optional)')
parser.add_argument('--deck-name', default='German Vocabulary', help='Name of the Anki deck')
args = parser.parse_args()
print("Loading program")
# Create media directory
os.makedirs('media', exist_ok=True)
# Initialize clients
llm_client = AnthropicClient()
unsplash_client = UnsplashClient()
# Initialize card generator
generator = CardGenerator(llm_client, unsplash_client)
# Read word list
words = read_word_list(args.input)
# Generate deck
deck, media_files = generator.create_deck(words, args.deck_name)
# Generate output filename if not provided
if not args.output:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_file = f"german_vocab_{timestamp}.apkg"
else:
output_file = args.output
# Save deck
package = GermanDeckPackage(deck, media_files)
package.write_to_file(output_file)
print(f"Deck saved as: {output_file}")
if __name__ == "__main__":
main()