Skip to content

Commit

Permalink
fix: More accurate results for URL on HN
Browse files Browse the repository at this point in the history
Similar behavior to Lemmy and Lobsters: when query is URL, try to
match URL of post source.
  • Loading branch information
macie committed Nov 27, 2023
1 parent 128def9 commit 5d551ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion hackernews.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ type HackerNewsResponse struct {
//
// See: https://hn.algolia.com/api
func SearchHackerNews(ctx context.Context, client http.Client, query string) ([]Discussion, error) {
searchURL := "http://hn.algolia.com/api/v1/search?tags=story&query="
searchURL := "http://hn.algolia.com/api/v1/search?"
discussions := make([]Discussion, 0)

_, err := url.Parse(query)
switch {
case err == nil:
searchURL += "restrictSearchableAttributes=url&query="
default:
searchURL += "tags=story&query="
}

r, err := client.Get(ctx, searchURL+url.QueryEscape(query))
if err != nil {
return discussions, err
Expand Down
2 changes: 1 addition & 1 deletion hackernews_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func ExampleSearchHackerNews() {
client := http.Client{}
query := "grugbrain.dev"
query := "https://grugbrain.dev"

opinions := ensure.MustReturn(SearchHackerNews(context.TODO(), client, query))

Expand Down

0 comments on commit 5d551ac

Please sign in to comment.