image

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package image provides image search functionality to find representative images for Bulgarian words from various APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadImage

func DownloadImage(ctx context.Context, searcher ImageSearcher, url string, outputPath string) error

DownloadImage is a utility function to download an image to a file

Types

type DownloadOptions

type DownloadOptions struct {
	OutputDir         string // Directory to save images
	OverwriteExisting bool   // Whether to overwrite existing files
	CreateDir         bool   // Create output directory if it doesn't exist
	FileNamePattern   string // Pattern for file naming (e.g., "{word}_{source}")
	MaxSizeBytes      int64  // Maximum file size to download (0 = no limit)
}

DownloadOptions configures image download behavior

func DefaultDownloadOptions

func DefaultDownloadOptions() *DownloadOptions

DefaultDownloadOptions returns sensible defaults for image downloads

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader handles image downloads from search results

func NewDownloader

func NewDownloader(searcher ImageSearcher, options *DownloadOptions) *Downloader

NewDownloader creates a new image downloader

func (*Downloader) DownloadBestMatch

func (d *Downloader) DownloadBestMatch(ctx context.Context, query string) (*SearchResult, string, error)

DownloadBestMatch downloads the best matching image for a query

func (*Downloader) DownloadBestMatchWithOptions added in v0.2.0

func (d *Downloader) DownloadBestMatchWithOptions(ctx context.Context, opts *SearchOptions) (*SearchResult, string, error)

DownloadBestMatchWithOptions downloads the best matching image for given search options

func (*Downloader) DownloadImage

func (d *Downloader) DownloadImage(ctx context.Context, result *SearchResult, outputPath string) error

DownloadImage downloads a single image to the specified path

type ImageSearcher

type ImageSearcher interface {
	// Search performs an image search with the given options
	Search(ctx context.Context, opts *SearchOptions) ([]SearchResult, error)

	// Download downloads an image from the given URL
	Download(ctx context.Context, url string) (io.ReadCloser, error)

	// GetAttribution returns the required attribution text for an image
	GetAttribution(result *SearchResult) string

	// Name returns the name of the search provider
	Name() string
}

ImageSearcher defines the interface for image search providers

type OpenAIClient

type OpenAIClient struct {

	// PromptCallback is called when the prompt is generated, before the image is created
	PromptCallback func(prompt string)
	// contains filtered or unexported fields
}

OpenAIClient implements ImageSearcher for OpenAI DALL-E image generation

func NewOpenAIClient

func NewOpenAIClient(config *OpenAIConfig) *OpenAIClient

NewOpenAIClient creates a new OpenAI DALL-E client

func (*OpenAIClient) Download

func (c *OpenAIClient) Download(ctx context.Context, url string) (io.ReadCloser, error)

Download downloads an image from the given URL

func (*OpenAIClient) GetAttribution

func (c *OpenAIClient) GetAttribution(result *SearchResult) string

GetAttribution returns the required attribution text

func (*OpenAIClient) GetLastPrompt added in v0.2.0

func (c *OpenAIClient) GetLastPrompt() string

GetLastPrompt returns the last prompt used for image generation

func (*OpenAIClient) Name

func (c *OpenAIClient) Name() string

Name returns the name of the provider

func (*OpenAIClient) Search

func (c *OpenAIClient) Search(ctx context.Context, opts *SearchOptions) ([]SearchResult, error)

Search generates an image for the Bulgarian word using DALL-E

func (*OpenAIClient) SetPromptCallback added in v0.7.1

func (c *OpenAIClient) SetPromptCallback(callback func(prompt string))

SetPromptCallback sets a callback function that will be called when the prompt is generated

type OpenAIConfig

type OpenAIConfig struct {
	APIKey  string
	Model   string
	Size    string
	Quality string
	Style   string
}

OpenAIConfig holds configuration for the OpenAI image provider

type RateLimitError

type RateLimitError struct {
	Provider     string
	RetryAfter   int // Seconds to wait before retry
	LimitPerHour int
	LimitPerDay  int
}

RateLimitError indicates that the API rate limit has been exceeded

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

type SearchError

type SearchError struct {
	Provider string
	Code     string
	Message  string
}

SearchError represents an error from an image search provider

func (*SearchError) Error

func (e *SearchError) Error() string

type SearchOptions

type SearchOptions struct {
	Query        string // Search query (Bulgarian word)
	Translation  string // English translation (if already available)
	Language     string // Language code (default: "bg")
	SafeSearch   bool   // Enable safe search filtering
	PerPage      int    // Number of results per page
	Page         int    // Page number (1-based)
	ImageType    string // Type: "photo", "illustration", "vector", "all"
	Orientation  string // Orientation: "horizontal", "vertical", "all"
	CustomPrompt string // Custom prompt for AI image generation (OpenAI)
}

SearchOptions configures the image search

func DefaultSearchOptions

func DefaultSearchOptions(query string) *SearchOptions

DefaultSearchOptions returns sensible defaults for Bulgarian word searches

type SearchResult

type SearchResult struct {
	ID           string // Unique identifier
	URL          string // Direct URL to the image
	ThumbnailURL string // URL to thumbnail version
	Width        int    // Image width in pixels
	Height       int    // Image height in pixels
	Description  string // Image description or tags
	Attribution  string // Attribution text if required
	Source       string // Source provider (e.g., "pixabay", "unsplash")
}

SearchResult represents a single image search result

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL