Skip to content

Commit

Permalink
Merge c164179 into 7c11ac0
Browse files Browse the repository at this point in the history
  • Loading branch information
groovili committed May 9, 2020
2 parents 7c11ac0 + c164179 commit 214dccd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 76 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v0.2.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
language: go

go:
- "1.11.x"
- 1.x
- 1.14.x
- master

install:
- export GO111MODULE=on
- go build
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover

script:
- go test -v -race ./.
- goveralls -service=travis-ci
- $GOPATH/bin/goveralls -service=travis-ci

notifications:
email: false
email: true
57 changes: 22 additions & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (
log "github.com/sirupsen/logrus"
)

const (
headerKeyAccept = "Accept"
headerKeyCookie = "Cookie"
headerKeySetCookie = "Set-Cookie"
contentTypeJSON = "application/json"
)

type gClient struct {
c *http.Client
defParams url.Values
Expand All @@ -34,71 +41,54 @@ func newGClient() *gClient {
c: http.DefaultClient,
defParams: p,
trendsCats: trendsCategories,
cookie: "",
debug: false,
}
}

func (c *gClient) do(ctx context.Context, u *url.URL) ([]byte, error) {
r := &http.Request{
URL: u,
Method: http.MethodGet,
r, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, errors.Wrap(err, errCreateRequest)
}
r = r.WithContext(ctx)

r.Header = make(http.Header)
if client.cookie != "" {
r.Header.Add("Cookie", client.cookie)
}
r.Header.Add(headerKeyAccept, contentTypeJSON)

r.Header.Add("Accept", "application/json")
if len(client.cookie) != 0 {
r.Header.Add(headerKeyCookie, client.cookie)
}

if client.debug {
log.Info("[Debug] Request with params: ", r.URL)
}

resp, err := c.c.Do(r)
if err != nil {
return nil, err
return nil, errors.Wrap(err, errDoRequest)
}
defer resp.Body.Close()

if client.debug {
log.Info("[Debug] Response: ", resp)
}

if resp.StatusCode == http.StatusTooManyRequests {
cookie := strings.Split(resp.Header.Get("Set-Cookie"), ";")
cookie := strings.Split(resp.Header.Get(headerKeySetCookie), ";")
if len(cookie) > 0 {
client.cookie = cookie[0]
r.Header.Set("Cookie", cookie[0])
r.Header.Set(headerKeyCookie, cookie[0])

resp, err = c.c.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
}
}

if resp.StatusCode != http.StatusOK {
return nil, errors.Wrapf(errors.New(errRequestFailed), errReqDataF, resp.StatusCode, resp.Status, resp.Body)
}

data, err := c.getRespData(resp)
defer resp.Body.Close()
if err != nil {
return nil, err
return nil, errors.Wrapf(ErrRequestFailed, errReqDataF, resp.StatusCode, resp.Status)
}

return data, nil
}

func (c *gClient) getRespData(resp *http.Response) ([]byte, error) {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return b, nil
return ioutil.ReadAll(resp.Body)
}

func (c *gClient) unmarshal(str string, dest interface{}) error {
Expand All @@ -110,10 +100,7 @@ func (c *gClient) unmarshal(str string, dest interface{}) error {
}

func (c *gClient) trends(ctx context.Context, path, hl, loc string, args ...map[string]string) (string, error) {
u, err := url.Parse(path)
if err != nil {
return "", err
}
u, _ := url.Parse(path)

// required params
p := client.defParams
Expand Down
17 changes: 17 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gogtrends

import "github.com/pkg/errors"

const (
errParsing = "failed to parse json"
errReqDataF = "request data: code = %d, status = %s"
errInvalidRequest = "invalid request param"
errCreateRequest = "failed to create request"
errDoRequest = "failed to perform request"
)

var (
ErrInvalidCategory = errors.New("invalid category param")
ErrRequestFailed = errors.New("failed to perform http request")
ErrInvalidWidgetType = errors.New("invalid widget type")
)
45 changes: 15 additions & 30 deletions gogtrends.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func Daily(ctx context.Context, hl, loc string) ([]*TrendingSearch, error) {
// split searches by days together
searches := make([]*TrendingSearch, 0)
for _, v := range out.Default.Searches {
for _, k := range v.Searches {
searches = append(searches, k)
}
searches = append(searches, v.Searches...)
}

return searches, nil
Expand All @@ -50,7 +48,7 @@ func Daily(ctx context.Context, hl, loc string) ([]*TrendingSearch, error) {
// Realtime represents realtime trends with included articles and sources.
func Realtime(ctx context.Context, hl, loc, cat string) ([]*TrendingStory, error) {
if !client.validateCategory(cat) {
return nil, errors.New(errInvalidCategory)
return nil, ErrInvalidCategory
}

data, err := client.trends(ctx, gAPI+gRealtime, hl, loc, map[string]string{paramCat: cat})
Expand All @@ -75,13 +73,13 @@ func ExploreCategories(ctx context.Context) (*ExploreCatTree, error) {
return client.exploreCats, nil
}

u, err := url.Parse(gAPI + gSCategories)
u, _ := url.Parse(gAPI + gSCategories)

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

b, err := client.do(ctx, u)

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

Expand All @@ -102,13 +100,13 @@ func ExploreLocations(ctx context.Context) (*ExploreLocTree, error) {
return client.exploreLocs, nil
}

u, err := url.Parse(gAPI + gSGeo)
u, _ := url.Parse(gAPI + gSGeo)

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

b, err := client.do(ctx, u)

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

Expand All @@ -132,10 +130,7 @@ func Explore(ctx context.Context, r *ExploreRequest, hl string) ([]*ExploreWidge
r.Time = strings.ReplaceAll(r.Time, "+", " ")
}

u, err := url.Parse(gAPI + gSExplore)
if err != nil {
return nil, err
}
u, _ := url.Parse(gAPI + gSExplore)

p := make(url.Values)
p.Set(paramTZ, "0")
Expand Down Expand Up @@ -169,13 +164,10 @@ func Explore(ctx context.Context, r *ExploreRequest, hl string) ([]*ExploreWidge
// InterestOverTime as list of `Timeline` dots for chart.
func InterestOverTime(ctx context.Context, w *ExploreWidget, hl string) ([]*Timeline, error) {
if w.ID != intOverTimeWidgetID {
return nil, errors.New(errInvalidWidgetType)
return nil, ErrInvalidWidgetType
}

u, err := url.Parse(gAPI + gSIntOverTime)
if err != nil {
return nil, err
}
u, _ := url.Parse(gAPI + gSIntOverTime)

p := make(url.Values)
p.Set(paramTZ, "0")
Expand Down Expand Up @@ -216,13 +208,10 @@ func InterestOverTime(ctx context.Context, w *ExploreWidget, hl string) ([]*Time
// InterestByLocation as list of `GeoMap`, with geo codes and interest values.
func InterestByLocation(ctx context.Context, w *ExploreWidget, hl string) ([]*GeoMap, error) {
if w.ID != intOverRegionID {
return nil, errors.New(errInvalidWidgetType)
return nil, ErrInvalidWidgetType
}

u, err := url.Parse(gAPI + gSIntOverReg)
if err != nil {
return nil, err
}
u, _ := url.Parse(gAPI + gSIntOverReg)

p := make(url.Values)
p.Set(paramTZ, "0")
Expand Down Expand Up @@ -261,13 +250,10 @@ func InterestByLocation(ctx context.Context, w *ExploreWidget, hl string) ([]*Ge
// Related topics or queries, list of `RankedKeyword`, supports two types of widgets.
func Related(ctx context.Context, w *ExploreWidget, hl string) ([]*RankedKeyword, error) {
if w.ID != relatedQueriesID && w.ID != relatedTopicsID {
return nil, errors.New(errInvalidWidgetType)
return nil, ErrInvalidWidgetType
}

u, err := url.Parse(gAPI + gSRelated)
if err != nil {
return nil, err
}
u, _ := url.Parse(gAPI + gSRelated)

p := make(url.Values)
p.Set(paramTZ, "0")
Expand Down Expand Up @@ -304,7 +290,6 @@ func Related(ctx context.Context, w *ExploreWidget, hl string) ([]*RankedKeyword
keywords := make([]*RankedKeyword, 0)
for _, v := range out.Default.Ranked {
keywords = append(keywords, v.Keywords...)

}

return keywords, nil
Expand Down
8 changes: 0 additions & 8 deletions vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const (
gSCategories = "/explore/pickers/category"
gSGeo = "/explore/pickers/geo"
gSRelated = "/widgetdata/relatedsearches"
gSSuggestions = "/autocomplete"
gSIntOverTime = "/widgetdata/multiline"
gSIntOverReg = "/widgetdata/comparedgeo"

Expand All @@ -27,13 +26,6 @@ const (
relatedTopicsID = "RELATED_TOPICS"

compareDataMode = "PERCENTAGES"

errParsing = "failed to parse json"
errRequestFailed = "failed to perform http request to API"
errReqDataF = "request data: code = %d, status = %s, body = %s"
errInvalidCategory = "invalid category param"
errInvalidRequest = "invalid request param"
errInvalidWidgetType = "invalid widget type"
)

var (
Expand Down

0 comments on commit 214dccd

Please sign in to comment.