gogtrends

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 8 Imported by: 0

README

Go Report Card

License

gogtrends is API wrapper which allows to get reports from Google Trends.

All contributions, updates and issues are warmly welcome.

Installation

Go modules support is required to use this package, also all dependencies can be found in go.sum file.

export GO111MODULE=on

Add github.com/groovili/gogtrends as import and run go build or manually require in go.mod file.

Debug

To see request-response details use gogtrends.Debug(true)

Usage

Daily and Realtime trends used as it is. For both methods user interface language are required. For Realtime trends category is required param, list of available categories - TrendsCategories.

Please notice that Realtime trends are available only for limited list of locations.

For InterestOverTime, InterestByLocation and Related - widget and user interface language are required.

To get widget you should call Explore methods first, it will return constant list of available widgets, every widget corresponds to methods above.

Widget includes request params and unique token for every method.

Also Explore method supports single and multiple items for comparision. Please take a look at ExploreRequest input. It supports search by multiple categories and locations which you can get as tree structure by ExploreCategories and ExploreLocations.

Please notice, when you call Explore method for keywords comparison, two first widgets would be for all of compared items, next widgets would be for each of individual items.

Available methods
  • Daily(ctx context.Context, hl, loc string) ([]*TrendingSearch, error) - daily trends descending ordered by days and articles corresponding to it.

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

  • 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.

  • InterestByLocation(ctx context.Context, w *ExploreWidget, hl string) ([]*GeoMap, error) - interest by location, list for map with geo codes and interest values.

  • Related(ctx context.Context, w *ExploreWidget, hl string) ([]*RankedKeyword, error) - related topics or queries, supports two types of widgets.

  • TrendsCategories() map[string]string - available categories for Realtime trends.

  • ExploreCategories(ctx context.Context) (*ExploreCatTree, error) - tree of categories for explore and comparison. Called once, then returned from cache.

  • ExploreLocations(ctx context.Context) (*ExploreLocTree, error) - tree of locations for explore and comparison. Called once, then returned from cache.

Parameters
  • hl - string, user interface language

  • loc - string, uppercase location (geo) country code, example "US" - United States

  • cat - string, lowercase category for real time trends, example "all" - all categories

  • exploreReq - ExploreRequest struct, represents search or comparison items.

  • widget - ExploreWidget struct, specific for every method, can be received by Explore method.

Examples

Working detailed examples for all methods and cases can be found in example folder. Short version below.

// Daily trends
ctx := context.Background()
dailySearches, err := gogtrends.Daily(ctx, "EN", "US")
// Get available trends categories and realtime trends
cats := gogtrends.TrendsCategories()
realtime, err := gogtrends.Realtime(ctx, "EN", "US", "all")
// Explore available widgets for keywords and get all available stats for it
explore, err := gogtrends.Explore(ctx, 
	    &gogtrends.ExploreRequest{
            ComparisonItems: []*gogtrends.ComparisonItem{
                {
                    Keyword: "Go",
                    Geo:     "US",
                    Time:    "today+12-m",
                },
            },
            Category: 31, // Programming category
            Property: "",
        }, "EN")

// Interest over time
overTime, err := gogtrends.InterestOverTime(ctx, explore[0], "EN")

// Interest by location
byLoc, err := gogtrends.InterestByLocation(ctx, explore[1], "EN")

// Related topics for keyword
relT, err := gogtrends.Related(ctx, explore[2], "EN")

// Related queries for keyword
relQ, err := gogtrends.Related(ctx, explore[3], "EN")

// Compare keywords interest
compare, err := gogtrends.Explore(ctx, 
	    &gogtrends.ExploreRequest{
            ComparisonItems: []*gogtrends.ComparisonItem{
                {
                    Keyword: "Go",
                    Geo:     "US",
                    Time:    "today+12-m",
                },
                {
                    Keyword: "Python",
                    Geo:     "US",
                    Time:    "today+12-m",
                },
                {
                    Keyword: "PHP",
                    Geo:     "US",
                    Time:    "today+12-m",
                },                               
            },
            Category: 31, // Programming category
            Property: "",
        }, "EN")

Licence

Package is distributed under MIT Licence.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(debug bool)

Debug allows to see request-response details.

func TrendsCategories

func TrendsCategories() map[string]string

TrendsCategories return list of available categories for Realtime method as [param]description map.

Types

type ComparisonItem

type ComparisonItem struct {
	Keyword string `json:"keyword" bson:"keyword"`
	Geo     string `json:"geo" bson:"geo"`
	Time    string `json:"time" bson:"time"`
}

type ExploreCatTree

type ExploreCatTree struct {
	Name     string            `json:"name" bson:"name"`
	ID       int               `json:"id" bson:"id"`
	Children []*ExploreCatTree `json:"children" bson:"children"`
}

func ExploreCategories

func ExploreCategories(ctx context.Context) (*ExploreCatTree, error)

ExploreCategories gets available categories for explore and comparison and caches it in client.

type ExploreLocTree

type ExploreLocTree struct {
	Name     string            `json:"name" bson:"name"`
	ID       string            `json:"id" bson:"id"`
	Children []*ExploreLocTree `json:"children" bson:"children"`
}

func ExploreLocations

func ExploreLocations(ctx context.Context) (*ExploreLocTree, error)

ExploreLocations gets available locations for explore and comparison and caches it in client.

type ExploreRequest

type ExploreRequest struct {
	ComparisonItems []*ComparisonItem `json:"comparisonItem" bson:"comparison_items"`
	Category        int               `json:"category" bson:"category"`
	Property        string            `json:"property" bson:"property"`
}

type ExploreWidget

type ExploreWidget struct {
	Token   string          `json:"token" bson:"token"`
	Type    string          `json:"type" bson:"type"`
	Title   string          `json:"title" bson:"title"`
	ID      string          `json:"id" bson:"id"`
	Request *WidgetResponse `json:"request" bson:"request"`
}

func Explore

func Explore(ctx context.Context, r *ExploreRequest, hl string) ([]*ExploreWidget, error)

Explore list of widgets with tokens. Every widget is related to specific method (`InterestOverTime`, `InterestOverLoc`, `RelatedSearches`, `Suggestions`) and contains required token and request information.

type GeoMap

type GeoMap struct {
	GeoCode        string   `json:"geoCode" bson:"geo_code"`
	GeoName        string   `json:"geoName" bson:"geo_name"`
	Value          []int    `json:"value" bson:"value"`
	FormattedValue []string `json:"formattedValue" bson:"formatted_value"`
	MaxValueIndex  int      `json:"maxValueIndex" bson:"max_value_index"`
	HasData        []bool   `json:"hasData" bson:"has_data"`
}

func InterestByLocation

func InterestByLocation(ctx context.Context, w *ExploreWidget, hl string) ([]*GeoMap, error)

InterestByLocation as list of `GeoMap`, with geo codes and interest values.

type KeywordRestriction

type KeywordRestriction struct {
	Type  string `json:"type" bson:"type"`
	Value string `json:"value" bson:"value"`
}

type KeywordTopic

type KeywordTopic struct {
	Mid   string `json:"mid" bson:"mid"`
	Title string `json:"title" bson:"title"`
	Type  string `json:"type" bson:"type"`
}

type KeywordsRestriction

type KeywordsRestriction struct {
	Keyword []*KeywordRestriction `json:"keyword" bson:"keyword"`
}

type RankedKeyword

type RankedKeyword struct {
	Query          string       `json:"query,omitempty" bson:"query"`
	Topic          KeywordTopic `json:"topic,omitempty" bson:"topic"`
	Value          int          `json:"value" bson:"value"`
	FormattedValue string       `json:"formattedValue" bson:"formatted_value"`
	HasData        bool         `json:"hasData" bson:"has_data"`
	Link           string       `json:"link" bson:"link"`
}
func Related(ctx context.Context, w *ExploreWidget, hl string) ([]*RankedKeyword, error)

Related topics or queries, list of `RankedKeyword`, supports two types of widgets.

type RequestOptions

type RequestOptions struct {
	Property string `json:"property" bson:"property"`
	Backend  string `json:"backend" bson:"backend"`
	Category int    `json:"category" bson:"category"`
}

type SearchArticle

type SearchArticle struct {
	Title   string       `json:"title" bson:"title"`
	TimeAgo string       `json:"timeAgo" bson:"time_ago"`
	Source  string       `json:"source" bson:"source"`
	Image   *SearchImage `json:"image" bson:"image"`
	URL     string       `json:"url" bson:"url"`
	Snippet string       `json:"snippet" bson:"snippet"`
}

type SearchImage

type SearchImage struct {
	NewsURL  string `json:"newsUrl" bson:"news_url"`
	Source   string `json:"source" bson:"source"`
	ImageURL string `json:"imageUrl" bson:"image_url"`
}

type SearchTitle

type SearchTitle struct {
	Query string `json:"query" bson:"query"`
}

type Timeline

type Timeline struct {
	Time              string   `json:"time" bson:"time"`
	FormattedTime     string   `json:"formattedTime" bson:"formatted_time"`
	FormattedAxisTime string   `json:"formattedAxisTime" bson:"formatted_axis_time"`
	Value             []int    `json:"value" bson:"value"`
	HasData           []bool   `json:"hasData" bson:"has_data"`
	FormattedValue    []string `json:"formattedValue" bson:"formatted_value"`
}

func InterestOverTime

func InterestOverTime(ctx context.Context, w *ExploreWidget, hl string) ([]*Timeline, error)

InterestOverTime as list of `Timeline` dots for chart.

type TrendingArticle

type TrendingArticle struct {
	Title   string `json:"articleTitle" bson:"title"`
	URL     string `json:"url" bson:"url"`
	Source  string `json:"source" bson:"source"`
	Time    string `json:"time" bson:"time"`
	Snippet string `json:"snippet" bson:"snippet"`
}

type TrendingSearch

type TrendingSearch struct {
	Title            *SearchTitle     `json:"title" bson:"title"`
	FormattedTraffic string           `json:"formattedTraffic" bson:"formatted_traffic"`
	Image            *SearchImage     `json:"image" bson:"image"`
	Articles         []*SearchArticle `json:"articles" bson:"articles"`
}

func Daily

func Daily(ctx context.Context, hl, loc string) ([]*TrendingSearch, error)

Daily gets daily trends descending ordered by days and articles corresponding to it.

type TrendingStory

type TrendingStory struct {
	Title    string             `json:"title" bson:"title"`
	Image    *SearchImage       `json:"image" bson:"image"`
	Articles []*TrendingArticle `json:"articles" bson:"articles"`
}

func Realtime

func Realtime(ctx context.Context, hl, loc, cat string) ([]*TrendingStory, error)

Realtime represents realtime trends with included articles and sources.

type WidgetComparisonItem

type WidgetComparisonItem struct {
	Geo                            map[string]string   `json:"geo,omitempty" bson:"geo"`
	Time                           string              `json:"time,omitempty" bson:"time"`
	ComplexKeywordsRestriction     KeywordsRestriction `json:"complexKeywordsRestriction,omitempty" bson:"complex_keywords_restriction"`
	OriginalTimeRangeForExploreURL string              `json:"originalTimeRangeForExploreUrl,omitempty" bson:"original_time_range_for_explore_url"`
}

type WidgetResponse

type WidgetResponse struct {
	Geo                interface{}             `json:"geo,omitempty" bson:"geo"`
	Time               string                  `json:"time,omitempty" bson:"time"`
	Resolution         string                  `json:"resolution,omitempty" bson:"resolution"`
	Locale             string                  `json:"locale,omitempty" bson:"locale"`
	Restriction        WidgetComparisonItem    `json:"restriction" bson:"restriction"`
	CompItem           []*WidgetComparisonItem `json:"comparisonItem" bson:"comparison_item"`
	RequestOpt         RequestOptions          `json:"requestOptions" bson:"request_option"`
	KeywordType        string                  `json:"keywordType" bson:"keyword_type"`
	Metric             []string                `json:"metric" bson:"metric"`
	Language           string                  `json:"language" bson:"language"`
	TrendinessSettings map[string]string       `json:"trendinessSettings" bson:"trendiness_settings"`
	DataMode           string                  `json:"dataMode,omitempty" bson:"data_mode"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL