Skip to content

Commit

Permalink
Merge pull request #20 from universe-venture/feature/search-autocomplete
Browse files Browse the repository at this point in the history
Add method search
  • Loading branch information
groovili committed May 10, 2021
2 parents 01c091d + fe578a4 commit 0aac5cc
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Please notice, when you call **Explore** method for keywords comparison, two fir

* `Realtime(ctx context.Context, hl, loc, cat string) ([]*TrendingStory, error)` - represents realtime trends with included articles and sources.

* `Search(ctx context.Context, word, hl string) ([]*KeywordTopic, error)` - Words/Topics related (5 results max) with your search.

* `Explore(ctx context.Context, r *ExploreRequest, hl string) ([]*ExploreWidget, error)` - widgets with **tokens**. Every widget is related to specific method (`InterestOverTime`, `InterestByLocation`, `Related`) and contains required **token** and request information.

* `InterestOverTime(ctx context.Context, w *ExploreWidget, hl string) ([]*Timeline, error)` - interest over time, dots for chart.
Expand Down
12 changes: 6 additions & 6 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module gogtrends/example

require (
github.com/groovili/gogtrends v1.0.0
github.com/pkg/errors v0.8.0
)
go 1.16

replace github.com/groovili/gogtrends v1.0.0 => ../
replace github.com/groovili/gogtrends => ../

go 1.13
require (
github.com/groovili/gogtrends v1.6.0
github.com/pkg/errors v0.9.1
)
27 changes: 17 additions & 10 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 13 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ func main() {
}
sg.Wait()

log.Println("Explore Search:")
keyword := "Go"

keywords, err := gogtrends.Search(ctx, keyword, langEn)
for _, v := range keywords {
log.Println(v)
if v.Type == "Language" {
keyword = v.Mid
break
}
}

log.Println("Explore trends:")
// get widgets for Golang keyword in programming category
explore, err := gogtrends.Explore(ctx, &gogtrends.ExploreRequest{
ComparisonItems: []*gogtrends.ComparisonItem{
{
Keyword: "Go",
Keyword: keyword,
Geo: locUS,
Time: "today 12-m",
},
Expand Down
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
module github.com/groovili/gogtrends

go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/json-iterator/go v1.1.5
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
github.com/json-iterator/go v1.1.10
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)

go 1.14
28 changes: 18 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions gogtrends.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gogtrends

import (
"context"
"fmt"
"net/url"
"strings"

Expand Down Expand Up @@ -294,3 +295,34 @@ func Related(ctx context.Context, w *ExploreWidget, hl string) ([]*RankedKeyword

return keywords, nil
}

// Related topics or queries, list of `RankedKeyword`, supports two types of widgets.
func Search(ctx context.Context, word, hl string) ([]*KeywordTopic, error) {
req := fmt.Sprintf("%s%s/%s", gAPI, gSAutocomplete, url.QueryEscape(word))
u, _ := url.Parse(req)

p := make(url.Values)
p.Set(paramTZ, "0")
p.Set(paramHl, hl)

u.RawQuery = p.Encode()

b, err := client.do(ctx, u)
if err != nil {
return nil, err
}

// google api returns not valid json :(
str := strings.Replace(string(b), ")]}',", "", 1)

out := new(searchOut)
if err := client.unmarshal(str, out); err != nil {
return nil, err
}

// split all keywords together
keywords := make([]*KeywordTopic, 0)
keywords = append(keywords, out.Default.Keywords...)

return keywords, nil
}
17 changes: 17 additions & 0 deletions gogtrends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gogtrends

import (
"context"
"fmt"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -586,3 +587,19 @@ func TestExploreGetWidgetsByType(t *testing.T) {
assert.True(t, strings.Contains(v.ID, string(RelatedQueriesID)))
}
}

func TestAutocomplete(t *testing.T) {
explore, err := Search(context.Background(), "Golang", langEN)
assert.NoError(t, err)

for _, v := range explore {
fmt.Println(v.Title, v.Type)
if v.Type == "Programming language" {
// result: /m/09gbxjr | Go | Programming language
assert.True(t, strings.Contains(v.Mid, "/m/09gbxjr"))
return
}
}

t.Error("failed to find topic")
}
21 changes: 15 additions & 6 deletions vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const (
gDaily = "/dailytrends"
gRealtime = "/realtimetrends"

gSExplore = "/explore"
gSCategories = "/explore/pickers/category"
gSGeo = "/explore/pickers/geo"
gSRelated = "/widgetdata/relatedsearches"
gSIntOverTime = "/widgetdata/multiline"
gSIntOverReg = "/widgetdata/comparedgeo"
gSExplore = "/explore"
gSCategories = "/explore/pickers/category"
gSGeo = "/explore/pickers/geo"
gSRelated = "/widgetdata/relatedsearches"
gSIntOverTime = "/widgetdata/multiline"
gSIntOverReg = "/widgetdata/comparedgeo"
gSAutocomplete = "/autocomplete"

paramHl = "hl"
paramCat = "cat"
Expand Down Expand Up @@ -331,6 +332,14 @@ type rankedList struct {
Keywords []*RankedKeyword `json:"rankedKeyword" bson:"keywords"`
}

type searchOut struct {
Default searchList `json:"default" bson:"default"`
}

type searchList struct {
Keywords []*KeywordTopic `json:"topics" bson:"keywords"`
}

// RankedKeyword - it's representation of related to search items
type RankedKeyword struct {
Query string `json:"query,omitempty" bson:"query"`
Expand Down

0 comments on commit 0aac5cc

Please sign in to comment.