fix: use structural selectors for DDG weather to handle advisory banners
All checks were successful
CI / build (pull_request) Successful in 1m11s
CI / vet (pull_request) Successful in 1m12s
CI / test (pull_request) Successful in 1m17s

The weather extractor used positional CSS selectors (div:first-child,
div:nth-child(2)) to locate the header and hourly container within the
widget section. When DuckDuckGo inserts advisory banners (e.g. wind
advisory), the extra div shifts positions and breaks extraction of
current temp, hourly data, humidity, and wind.

Replace with structural selectors:
- div:not(:has(ul)) for the header (first div without a list)
- div:has(> ul) for the hourly container (div with direct ul child)

These match elements by their content structure rather than position,
so advisory banners no longer break extraction.

Fixes #64

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-20 18:22:53 +00:00
parent 65cf6b027f
commit 8c2848246b
2 changed files with 175 additions and 8 deletions

View File

@@ -86,8 +86,10 @@ func extractWeather(doc extractor.Node) (*WeatherData, error) {
}
// Header: condition and location
// Structure: section > div:first-child > [div(toggle), p(condition), p(location)]
header := section.SelectFirst("div:first-child")
// Structure: section > div > [div(toggle), p(condition), p(location)]
// Use :not(:has(ul)) to skip the hourly container div and avoid breaking
// when advisory banners (e.g. wind advisory) insert extra divs.
header := section.SelectFirst("div:not(:has(ul))")
if header != nil {
ps := header.Select("p")
if len(ps) >= 2 {
@@ -99,8 +101,10 @@ func extractWeather(doc extractor.Node) (*WeatherData, error) {
}
// Hourly forecast and details
// Structure: section > div:nth-child(2) > [ul(hourly items), div(humidity/wind)]
hourlyContainer := section.SelectFirst("div:nth-child(2)")
// Structure: section > div > [ul(hourly items), div(humidity/wind)]
// Use :has(> ul) to find the div containing the hourly list, regardless of
// position. This avoids breaking when advisory banners insert extra divs.
hourlyContainer := section.SelectFirst("div:has(> ul)")
if hourlyContainer != nil {
_ = hourlyContainer.ForEach("ul > li", func(n extractor.Node) error {
var hour HourlyForecast