client.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package google
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "time"
  8. "git.linuxforward.com/byom/byom-trends/config"
  9. "git.linuxforward.com/byom/byom-trends/logger"
  10. "github.com/sirupsen/logrus"
  11. )
  12. type Client struct {
  13. httpClient *http.Client
  14. apiKey string
  15. baseURL string
  16. logger *logrus.Entry
  17. }
  18. type TrendResult struct {
  19. Keyword string `json:"keyword"`
  20. TimeRange string `json:"time_range"`
  21. Interest []InterestPoint `json:"interest"`
  22. RelatedTopics []RelatedTopic `json:"related_topics"`
  23. RegionalInterest map[string]int `json:"regional_interest"`
  24. }
  25. type InterestPoint struct {
  26. Date time.Time `json:"date"`
  27. Value int `json:"value"`
  28. }
  29. type RelatedTopic struct {
  30. Title string `json:"title"`
  31. Type string `json:"type"`
  32. Value int `json:"value"`
  33. }
  34. func NewClient(cfg *config.GoogleConfig) *Client {
  35. return &Client{
  36. httpClient: &http.Client{
  37. Timeout: time.Second * 10,
  38. },
  39. apiKey: cfg.TrendsAPIKey,
  40. baseURL: cfg.BaseURL,
  41. logger: logger.NewLogger("google-trends-client"),
  42. }
  43. }
  44. func (c *Client) GetTrends(keyword string, timeRange string) (*TrendResult, error) {
  45. params := url.Values{}
  46. params.Add("keyword", keyword)
  47. params.Add("time_range", timeRange)
  48. params.Add("key", c.apiKey)
  49. url := fmt.Sprintf("%s/dailytrends?%s", c.baseURL, params.Encode())
  50. req, err := http.NewRequest("GET", url, nil)
  51. if err != nil {
  52. return nil, fmt.Errorf("create request: %w", err)
  53. }
  54. resp, err := c.httpClient.Do(req)
  55. if err != nil {
  56. return nil, fmt.Errorf("do request: %w", err)
  57. }
  58. defer resp.Body.Close()
  59. if resp.StatusCode != http.StatusOK {
  60. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  61. }
  62. var result TrendResult
  63. if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
  64. return nil, fmt.Errorf("decode response: %w", err)
  65. }
  66. return &result, nil
  67. }
  68. func (c *Client) CompareKeywords(keywords []string, timeRange string) (map[string]*TrendResult, error) {
  69. results := make(map[string]*TrendResult)
  70. for _, keyword := range keywords {
  71. result, err := c.GetTrends(keyword, timeRange)
  72. if err != nil {
  73. return nil, fmt.Errorf("get trends for %s: %w", keyword, err)
  74. }
  75. results[keyword] = result
  76. }
  77. return results, nil
  78. }
  79. func (c *Client) GetRelatedTopics(keyword string) ([]RelatedTopic, error) {
  80. params := url.Values{}
  81. params.Add("keyword", keyword)
  82. params.Add("key", c.apiKey)
  83. url := fmt.Sprintf("%s/relatedTopics?%s", c.baseURL, params.Encode())
  84. req, err := http.NewRequest("GET", url, nil)
  85. if err != nil {
  86. return nil, fmt.Errorf("create request: %w", err)
  87. }
  88. resp, err := c.httpClient.Do(req)
  89. if err != nil {
  90. return nil, fmt.Errorf("do request: %w", err)
  91. }
  92. defer resp.Body.Close()
  93. if resp.StatusCode != http.StatusOK {
  94. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  95. }
  96. var topics []RelatedTopic
  97. if err := json.NewDecoder(resp.Body).Decode(&topics); err != nil {
  98. return nil, fmt.Errorf("decode response: %w", err)
  99. }
  100. return topics, nil
  101. }
  102. func (c *Client) GetRegionalInterest(keyword string, region string) (map[string]int, error) {
  103. params := url.Values{}
  104. params.Add("keyword", keyword)
  105. params.Add("region", region)
  106. params.Add("key", c.apiKey)
  107. url := fmt.Sprintf("%s/regionalInterest?%s", c.baseURL, params.Encode())
  108. req, err := http.NewRequest("GET", url, nil)
  109. if err != nil {
  110. return nil, fmt.Errorf("create request: %w", err)
  111. }
  112. resp, err := c.httpClient.Do(req)
  113. if err != nil {
  114. return nil, fmt.Errorf("do request: %w", err)
  115. }
  116. defer resp.Body.Close()
  117. if resp.StatusCode != http.StatusOK {
  118. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  119. }
  120. var interest map[string]int
  121. if err := json.NewDecoder(resp.Body).Decode(&interest); err != nil {
  122. return nil, fmt.Errorf("decode response: %w", err)
  123. }
  124. return interest, nil
  125. }