Files
go-fa-api/search_parser.go
2026-05-25 22:27:18 +02:00

39 lines
1.2 KiB
Go

package fa
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
// parseSearchResults walks /search/ output, returning each result and
// whether a "Next" page link exists.
//
// FA renders results inside <section id="gallery-search-results"> with the
// same <figure id="sid-…"> shape as gallery/browse/inbox, so the per-item
// extraction reuses parseGalleryFigure. Pagination is page-numbered (GET
// ?page=N) and the Next anchor is in <div class="pagination"> at the top
// (and bottom) of the results.
//
// useJSON controls the experimental JSON-first merge see parseGalleryPage.
func parseSearchResults(doc *goquery.Document, useJSON bool) (items []*Submission, hasNext bool) {
var jsonData listingJSONMap
if useJSON {
jsonData = readListingJSON(doc)
}
doc.Find("section#gallery-search-results figure[id^=sid-]").Each(func(_ int, sel *goquery.Selection) {
if s := parseGalleryFigure(sel, jsonData); s != nil {
items = append(items, s)
}
})
doc.Find("div.pagination a.button").EachWithBreak(func(_ int, a *goquery.Selection) bool {
if strings.EqualFold(trimText(a), "next") {
hasNext = true
return false
}
return true
})
return items, hasNext
}