twitter-scrapper/trends.go

56 lines
1.1 KiB
Go
Raw Normal View History

2020-02-12 10:45:19 +02:00
package twitterscraper
import (
2020-08-13 17:39:32 +03:00
"fmt"
2020-12-03 21:42:16 +07:00
"net"
2020-08-13 17:39:32 +03:00
"net/http"
"strings"
2020-12-03 21:42:16 +07:00
"time"
2020-08-13 17:39:32 +03:00
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
// GetTrends return list of trends.
2020-02-12 10:45:19 +02:00
func GetTrends() ([]string, error) {
2020-12-03 21:42:16 +07:00
client := http.DefaultClient
if HTTPProxy != nil {
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(HTTPProxy),
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
},
}
}
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-12-03 21:42:16 +07:00
resp, err := client.Do(req)
2020-08-13 17:39:32 +03:00
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
}