diff --git a/CHANGELOG.md b/CHANGELOG.md index dcd97b8..2f3f4c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ 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. + ### Changed - Anything older than a day shows its date, not "3mo ago". @@ -20,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 shortened. - Escaped markup in comments stays literal: `<p>` prints as `

`, not a line break. +- `--min-points N` includes stories with exactly N points. ## [0.2.1] — 2026-08-08 diff --git a/README.md b/README.md index 0a3a23c..515b618 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 a23309c..c0ed91d 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -126,7 +126,10 @@ type SearchOptions struct { Tags string SortByDate bool NumResults int + Page int + URLOnly bool AfterTime *time.Time + BeforeTime *time.Time MinPoints int } @@ -148,12 +151,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 d294133..34abca3 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -42,7 +42,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", }, } @@ -69,6 +79,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 ae0ddd0..075de19 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -84,40 +84,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 } @@ -138,7 +165,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