2020-05-13 17:35:44 +02:00
|
|
|
package twitterscraper
|
2020-05-14 14:59:33 +02:00
|
|
|
|
|
|
|
|
import (
|
2020-06-12 21:31:08 +08:00
|
|
|
"context"
|
2023-04-23 17:32:28 +03:00
|
|
|
"errors"
|
2020-12-11 20:58:49 +02:00
|
|
|
"strconv"
|
2020-05-14 14:59:33 +02:00
|
|
|
)
|
|
|
|
|
|
2020-05-15 17:52:06 +02:00
|
|
|
// SearchTweets returns channel with tweets for a given search query
|
2021-04-22 21:38:49 +03:00
|
|
|
func (s *Scraper) SearchTweets(ctx context.Context, query string, maxTweetsNbr int) <-chan *TweetResult {
|
|
|
|
|
return getTweetTimeline(ctx, query, maxTweetsNbr, s.FetchSearchTweets)
|
2020-12-12 23:33:57 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 11:55:12 +03:00
|
|
|
// Deprecated: SearchTweets wrapper for default Scraper
|
2021-04-22 21:38:49 +03:00
|
|
|
func SearchTweets(ctx context.Context, query string, maxTweetsNbr int) <-chan *TweetResult {
|
2020-12-12 23:33:57 +02:00
|
|
|
return defaultScraper.SearchTweets(ctx, query, maxTweetsNbr)
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
// SearchProfiles returns channel with profiles for a given search query
|
|
|
|
|
func (s *Scraper) SearchProfiles(ctx context.Context, query string, maxProfilesNbr int) <-chan *ProfileResult {
|
|
|
|
|
return getUserTimeline(ctx, query, maxProfilesNbr, s.FetchSearchProfiles)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 11:55:12 +03:00
|
|
|
// Deprecated: SearchProfiles wrapper for default Scraper
|
2021-04-22 21:38:49 +03:00
|
|
|
func SearchProfiles(ctx context.Context, query string, maxProfilesNbr int) <-chan *ProfileResult {
|
|
|
|
|
return defaultScraper.SearchProfiles(ctx, query, maxProfilesNbr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getSearchTimeline gets results for a given search query, via the Twitter frontend API
|
|
|
|
|
func (s *Scraper) getSearchTimeline(query string, maxNbr int, cursor string) (*timeline, error) {
|
2023-04-23 17:32:28 +03:00
|
|
|
if !s.isLogged {
|
|
|
|
|
return nil, errors.New("scraper is not logged in for search")
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
if maxNbr > 50 {
|
|
|
|
|
maxNbr = 50
|
2020-12-03 21:42:16 +07:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 23:33:57 +02:00
|
|
|
req, err := s.newRequest("GET", "https://twitter.com/i/api/2/search/adaptive.json")
|
2020-05-14 14:59:33 +02:00
|
|
|
if err != nil {
|
2021-04-22 21:38:49 +03:00
|
|
|
return nil, err
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
2020-06-15 15:26:43 +03:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
q := req.URL.Query()
|
|
|
|
|
q.Add("q", query)
|
2021-04-22 21:38:49 +03:00
|
|
|
q.Add("count", strconv.Itoa(maxNbr))
|
2020-12-11 20:58:49 +02:00
|
|
|
q.Add("query_source", "typed_query")
|
|
|
|
|
q.Add("pc", "1")
|
2023-02-16 11:15:13 +02:00
|
|
|
q.Add("requestContext", "launch")
|
2020-12-11 20:58:49 +02:00
|
|
|
q.Add("spelling_corrections", "1")
|
2023-02-16 11:15:13 +02:00
|
|
|
q.Add("include_ext_edit_control", "true")
|
2020-12-11 20:58:49 +02:00
|
|
|
if cursor != "" {
|
|
|
|
|
q.Add("cursor", cursor)
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
2020-12-23 19:53:48 +02:00
|
|
|
switch s.searchMode {
|
|
|
|
|
case SearchLatest:
|
2023-03-17 04:11:02 +05:30
|
|
|
q.Add("f", "live")
|
2020-12-23 19:53:48 +02:00
|
|
|
case SearchPhotos:
|
|
|
|
|
q.Add("result_filter", "image")
|
|
|
|
|
case SearchVideos:
|
|
|
|
|
q.Add("result_filter", "video")
|
2021-04-22 21:38:49 +03:00
|
|
|
case SearchUsers:
|
|
|
|
|
q.Add("result_filter", "user")
|
2020-12-20 00:20:27 +07:00
|
|
|
}
|
|
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
req.URL.RawQuery = q.Encode()
|
2020-05-14 14:59:33 +02:00
|
|
|
|
2020-12-11 20:58:49 +02:00
|
|
|
var timeline timeline
|
2020-12-12 23:33:57 +02:00
|
|
|
err = s.RequestAPI(req, &timeline)
|
2020-05-14 14:59:33 +02:00
|
|
|
if err != nil {
|
2021-04-22 21:38:49 +03:00
|
|
|
return nil, err
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
2021-04-22 21:38:49 +03:00
|
|
|
return &timeline, nil
|
|
|
|
|
}
|
2020-05-14 14:59:33 +02:00
|
|
|
|
2021-04-22 21:38:49 +03:00
|
|
|
// FetchSearchTweets gets tweets for a given search query, via the Twitter frontend API
|
|
|
|
|
func (s *Scraper) FetchSearchTweets(query string, maxTweetsNbr int, cursor string) ([]*Tweet, string, error) {
|
|
|
|
|
timeline, err := s.getSearchTimeline(query, maxTweetsNbr, cursor)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
2021-07-16 11:08:43 +03:00
|
|
|
tweets, nextCursor := timeline.parseTweets()
|
2020-09-19 13:37:50 +03:00
|
|
|
return tweets, nextCursor, nil
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
2021-04-22 21:38:49 +03:00
|
|
|
|
|
|
|
|
// FetchSearchProfiles gets users for a given search query, via the Twitter frontend API
|
|
|
|
|
func (s *Scraper) FetchSearchProfiles(query string, maxProfilesNbr int, cursor string) ([]*Profile, string, error) {
|
|
|
|
|
timeline, err := s.getSearchTimeline(query, maxProfilesNbr, cursor)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
2021-07-16 11:08:43 +03:00
|
|
|
users, nextCursor := timeline.parseUsers()
|
2021-04-22 21:38:49 +03:00
|
|
|
return users, nextCursor, nil
|
|
|
|
|
}
|