2020-02-12 10:45:19 +02:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-13 17:39:32 +03:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2020-02-12 10:45:19 +02:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-13 17:39:32 +03:00
|
|
|
const trendsURL = "https://mobile.twitter.com/trends"
|
2020-02-12 10:45:19 +02:00
|
|
|
|
2020-05-14 21:52:55 +03:00
|
|
|
// GetTrends return list of trends.
|
2020-02-12 10:45:19 +02:00
|
|
|
func GetTrends() ([]string, error) {
|
2020-08-13 17:39:32 +03:00
|
|
|
req, err := http.NewRequest("GET", trendsURL, nil)
|
2020-02-12 10:45:19 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-13 17:39:32 +03:00
|
|
|
req.Header.Set("Accept-Language", "en-US")
|
2020-02-12 10:45:19 +02:00
|
|
|
|
2020-08-13 17:39:32 +03:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if resp == nil {
|
2020-02-12 10:45:19 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-13 17:39:32 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
return nil, fmt.Errorf("response status: %s", resp.Status)
|
|
|
|
|
}
|
2020-02-12 10:45:19 +02:00
|
|
|
|
2020-08-13 17:39:32 +03:00
|
|
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
2020-02-12 10:45:19 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trends []string
|
2020-08-13 17:39:32 +03:00
|
|
|
doc.Find("li.topic").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
trends = append(trends, strings.TrimSpace(s.Text()))
|
2020-02-12 10:45:19 +02:00
|
|
|
})
|
|
|
|
|
return trends, nil
|
|
|
|
|
}
|