From cbd38eceb9829be0f9aa496285928b6487c95dab Mon Sep 17 00:00:00 2001 From: shbernal Date: Fri, 30 Oct 2026 13:42:39 +0100 Subject: [PATCH] feat(search): add tag, author, URL, date window and page filters The Algolia API supports all of these, but `hn search` could only reach keywords, a start date and a points floor. Agents had to drop to raw API calls for common questions such as "what did this person say about X" or "has this URL been discussed". The query becomes optional so filters alone can browse. `--min-points N` now includes N, as its name says. --- CHANGELOG.md | 7 ++++ README.md | 13 ++++++++ internal/api/client.go | 15 ++++++++- internal/api/client_test.go | 23 ++++++++++++- internal/cmd/root.go | 66 +++++++++++++++++++++++++++---------- skills/hn/SKILL.md | 6 ++++ 6 files changed, 111 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e2b8d5..5a7771b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- `hn search` flags `--tag`, `--by`, `--before`, `--url` and `--page`. The query + is now optional, so filters alone can browse. + +### Fixed +- `--min-points N` includes stories with exactly N points. + ## [0.2.1] — 2026-08-08 ### Fixed diff --git a/README.md b/README.md index e07e4dc..1ec5065 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,19 @@ hn search "Go vs Rust" --comments # Recent stories sorted by date hn search "WASM" --sort date --after 2026-01-01 +# Search one kind of post, or one author +hn search "database" --tag show_hn +hn search "scheduling" --by dang --comments + +# Browse with filters alone, inside a date range +hn search --tag ask_hn --min-points 200 --after 2026-01-01 --before 2026-02-01 + +# Has this site or URL been discussed? +hn search "github.com/voska" --url + +# Next page of results +hn search "WASM" --page 1 + # Current front page hn front diff --git a/internal/api/client.go b/internal/api/client.go index 5dc1595..1959288 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -88,7 +88,10 @@ type SearchOptions struct { Tags string SortByDate bool NumResults int + Page int + URLOnly bool AfterTime *time.Time + BeforeTime *time.Time MinPoints int } @@ -110,12 +113,22 @@ func (c *Client) BuildURL(opts SearchOptions) string { params.Set("hitsPerPage", strconv.Itoa(opts.NumResults)) } + if opts.Page > 0 { + params.Set("page", strconv.Itoa(opts.Page)) + } + if opts.URLOnly { + params.Set("restrictSearchableAttributes", "url") + } + var numericFilters []string if opts.AfterTime != nil { numericFilters = append(numericFilters, fmt.Sprintf("created_at_i>%d", opts.AfterTime.Unix())) } + if opts.BeforeTime != nil { + numericFilters = append(numericFilters, fmt.Sprintf("created_at_i<%d", opts.BeforeTime.Unix())) + } if opts.MinPoints > 0 { - numericFilters = append(numericFilters, fmt.Sprintf("points>%d", opts.MinPoints)) + numericFilters = append(numericFilters, fmt.Sprintf("points>=%d", opts.MinPoints)) } if len(numericFilters) > 0 { params.Set("numericFilters", strings.Join(numericFilters, ",")) diff --git a/internal/api/client_test.go b/internal/api/client_test.go index 9f3fc5d..73d9e7b 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -34,7 +34,17 @@ func TestBuildURL(t *testing.T) { { name: "with min points", opts: SearchOptions{Query: "go", Tags: "story", MinPoints: 100}, - want: "https://example.com/api/v1/search?numericFilters=points%3E100&query=go&tags=story", + want: "https://example.com/api/v1/search?numericFilters=points%3E%3D100&query=go&tags=story", + }, + { + name: "with page", + opts: SearchOptions{Query: "go", Tags: "story", Page: 2}, + want: "https://example.com/api/v1/search?page=2&query=go&tags=story", + }, + { + name: "url only", + opts: SearchOptions{Query: "example.com", Tags: "story", URLOnly: true}, + want: "https://example.com/api/v1/search?query=example.com&restrictSearchableAttributes=url&tags=story", }, } @@ -61,6 +71,17 @@ func TestBuildURLWithAfterTime(t *testing.T) { } } +func TestBuildURLDateWindow(t *testing.T) { + c := NewClient("https://example.com/api/v1", http.DefaultClient) + after := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + before := time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC) + got := c.BuildURL(SearchOptions{AfterTime: &after, BeforeTime: &before}) + want := "https://example.com/api/v1/search?numericFilters=created_at_i%3E1767225600%2Ccreated_at_i%3C1769904000" + if got != want { + t.Errorf("BuildURL() = %q, want %q", got, want) + } +} + func TestSearch(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(SearchResult{ diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 284f3a1..ca97ade 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -93,40 +93,67 @@ var ( searchComments bool searchSort string searchNum int + searchPage int searchAfter string + searchBefore string searchMinPts int + searchTag string + searchBy string + searchURL bool ) +// parseDate reads a YYYY-MM-DD flag value, or returns nil when it is unset. +func parseDate(value string) (*time.Time, error) { + if value == "" { + return nil, nil + } + t, err := time.Parse("2006-01-02", value) + if err != nil { + return nil, fmt.Errorf("invalid date %q (use YYYY-MM-DD)", value) + } + return &t, nil +} + var searchCmd = &cobra.Command{ - Use: "search QUERY", + Use: "search [QUERY]", Aliases: []string{"s"}, Short: "Search HN stories or comments", - Args: cobra.MinimumNArgs(1), + Long: `Search HN stories or comments. The query is optional, so filters alone can +browse: hn search --tag show_hn --min-points 100 --sort date`, RunE: func(cmd *cobra.Command, args []string) error { query := strings.Join(args, " ") - tags := "story" + tags := []string{"story"} if searchComments { - tags = "comment" + tags = []string{"comment"} + } + if searchTag != "" { + tags = append(tags, searchTag) + } + if searchBy != "" { + tags = append(tags, "author_"+searchBy) + } + + after, err := parseDate(searchAfter) + if err != nil { + return err + } + before, err := parseDate(searchBefore) + if err != nil { + return err } - opts := api.SearchOptions{ + result, err := client().Search(api.SearchOptions{ Query: query, - Tags: tags, + Tags: strings.Join(tags, ","), SortByDate: searchSort == "date", NumResults: searchNum, + Page: searchPage, + URLOnly: searchURL, + AfterTime: after, + BeforeTime: before, MinPoints: searchMinPts, - } - - if searchAfter != "" { - t, err := time.Parse("2006-01-02", searchAfter) - if err != nil { - return fmt.Errorf("invalid date %q (use YYYY-MM-DD)", searchAfter) - } - opts.AfterTime = &t - } - - result, err := client().Search(opts) + }) if err != nil { return err } @@ -147,7 +174,12 @@ func init() { searchCmd.Flags().BoolVar(&searchComments, "comments", false, "Search comments instead of stories") searchCmd.Flags().StringVar(&searchSort, "sort", "relevance", "Sort order: relevance or date") searchCmd.Flags().IntVarP(&searchNum, "num", "n", 20, "Number of results") + searchCmd.Flags().IntVar(&searchPage, "page", 0, "Result page, starting at 0") searchCmd.Flags().StringVar(&searchAfter, "after", "", "Only results after date (YYYY-MM-DD)") + searchCmd.Flags().StringVar(&searchBefore, "before", "", "Only results before date (YYYY-MM-DD)") + searchCmd.Flags().StringVar(&searchTag, "tag", "", "Only this Algolia tag: show_hn, ask_hn, front_page") + searchCmd.Flags().StringVar(&searchBy, "by", "", "Only this author") + searchCmd.Flags().BoolVar(&searchURL, "url", false, "Match the query against story URLs only") searchCmd.Flags().IntVar(&searchMinPts, "min-points", 0, "Minimum points filter") } diff --git a/skills/hn/SKILL.md b/skills/hn/SKILL.md index 24f2d27..e7f2365 100644 --- a/skills/hn/SKILL.md +++ b/skills/hn/SKILL.md @@ -20,7 +20,13 @@ hn search "query" --comments # Search comments instead hn search "query" --sort date # Sort by date (default: relevance) hn search "query" -n 10 # Limit results hn search "query" --after 2026-01-01 # Only results after date +hn search "query" --before 2026-02-01 # Only results before date hn search "query" --min-points 100 # Minimum points filter +hn search "query" --tag show_hn # show_hn, ask_hn or front_page +hn search "query" --by USERNAME # Only this author +hn search "example.com" --url # Match story URLs only +hn search "query" --page 1 # Next page, starting at 0 +hn search --tag show_hn --sort date # The query is optional hn front # Current front page stories hn front -n 10 # Fewer results