Improved Wikipedia IA to be lang specific
Some checks failed
Run Integration Tests / test (push) Failing after 40s

This commit is contained in:
partisan 2025-07-05 11:54:40 +02:00
parent 89264b0f87
commit 41f7eb4357
2 changed files with 10 additions and 21 deletions

View file

@ -56,7 +56,7 @@ func detectInstantAnswer(query string) *InstantAnswer {
} }
// Try Wikipedia search // Try Wikipedia search
if title, text, link, ok := getWikipediaSummary(query); ok { if title, text, link, ok := getWikipediaSummary(query, settings.SiteLanguage); ok {
return &InstantAnswer{ return &InstantAnswer{
Type: "wiki", Type: "wiki",
Title: title, Title: title,
@ -71,29 +71,17 @@ func detectInstantAnswer(query string) *InstantAnswer {
} }
func initExchangeRates() { func initExchangeRates() {
// Initial synchronous load // Initial sync
if err := UpdateExchangeRates(); err != nil { if err := UpdateExchangeRates(); err != nil {
printErr("Initial exchange rate update failed: %v", err) printErr("Initial exchange rate update failed: %v", err)
} else {
PrecacheAllCurrencyPairs()
} }
// Pre-cache common wiki terms in background // Periodic update only
go func() {
commonTerms := []string{"United States", "Europe", "Technology", "Science", "Mathematics"}
for _, term := range commonTerms {
getWikipediaSummary(term)
}
}()
// Periodically update cache
ticker := time.NewTicker(30 * time.Minute) ticker := time.NewTicker(30 * time.Minute)
go func() { go func() {
for range ticker.C { for range ticker.C {
if err := UpdateExchangeRates(); err != nil { if err := UpdateExchangeRates(); err != nil {
printWarn("Periodic exchange rate update failed: %v", err) printWarn("Periodic exchange rate update failed: %v", err)
} else {
PrecacheAllCurrencyPairs()
} }
} }
}() }()

View file

@ -21,17 +21,18 @@ type WikipediaResponse struct {
} }
// Get Wikipedia summary // Get Wikipedia summary
func getWikipediaSummary(query string) (title, text, link string, ok bool) { func getWikipediaSummary(query, lang string) (title, text, link string, ok bool) {
// Clean and prepare query // Clean inputs
query = strings.TrimSpace(query) query = strings.TrimSpace(query)
if query == "" { lang = strings.ToLower(strings.TrimSpace(lang))
if query == "" || lang == "" {
return "", "", "", false return "", "", "", false
} }
// API request // Construct Wikipedia API URL with lang
apiURL := fmt.Sprintf( apiURL := fmt.Sprintf(
"https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro&explaintext&redirects=1&titles=%s", "https://%s.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro&explaintext&redirects=1&titles=%s",
url.QueryEscape(query), lang, url.QueryEscape(query),
) )
resp, err := http.Get(apiURL) resp, err := http.Get(apiURL)