Fix SetSearchLive, SetSearchPhotos and SetSearchVideos

This commit is contained in:
Alexander Sheiko 2020-12-23 19:08:17 +02:00
parent b9ae4a1de2
commit b6887fca9c
3 changed files with 30 additions and 16 deletions

View file

@ -65,12 +65,22 @@ func main() {
The search ends if we have 50 tweets. The search ends if we have 50 tweets.
### Search tweet in realtime See [Rules and filtering](https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators) for build standard queries.
#### Search tweet in realtime
```golang ```golang
scraper.SearchLive(true) scraper.SetSearchLive(true)
``` ```
See [Rules and filtering](https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators) for build standard queries. #### Filter search
```golang
scraper.SetSearchPhotos(true)
```
or
```golang
scraper.SetSearchVideos(true)
```
### Get profile ### Get profile

View file

@ -15,7 +15,8 @@ type Scraper struct {
client *http.Client client *http.Client
guestToken string guestToken string
includeReplies bool includeReplies bool
searchMode string liveSearch bool
resultFilter string
} }
var defaultScraper *Scraper var defaultScraper *Scraper
@ -28,22 +29,22 @@ func New() *Scraper {
} }
// SetSearchLive enable/disable realtime search // SetSearchLive enable/disable realtime search
func (s *Scraper) SetSearchLive(srctype bool) *Scraper { func (s *Scraper) SetSearchLive(b bool) *Scraper {
if srctype { s.liveSearch = b
s.searchMode = "live"
}
return s return s
} }
// SetSearchLive wrapper for default SetSearchLive // SetSearchLive wrapper for default SetSearchLive
func SetSearchLive(srctype bool) *Scraper { func SetSearchLive(b bool) *Scraper {
return defaultScraper.SetSearchLive(srctype) return defaultScraper.SetSearchLive(b)
} }
// SetSearchPhotos filter search for photos only // SetSearchPhotos filter search for photos only
func (s *Scraper) SetSearchPhotos(srctype bool) *Scraper { func (s *Scraper) SetSearchPhotos(srctype bool) *Scraper {
if srctype { if srctype {
s.searchMode = "image" s.resultFilter = "image"
} else {
s.resultFilter = ""
} }
return s return s
} }
@ -56,7 +57,9 @@ func SetSearchPhotos(srctype bool) *Scraper {
// SetSearchVideos filter search for videos only // SetSearchVideos filter search for videos only
func (s *Scraper) SetSearchVideos(srctype bool) *Scraper { func (s *Scraper) SetSearchVideos(srctype bool) *Scraper {
if srctype { if srctype {
s.searchMode = "video" s.resultFilter = "video"
} else {
s.resultFilter = ""
} }
return s return s
} }

View file

@ -37,10 +37,11 @@ func (s *Scraper) FetchSearchTweets(query string, maxTweetsNbr int, cursor strin
if cursor != "" { if cursor != "" {
q.Add("cursor", cursor) q.Add("cursor", cursor)
} }
if s.searchMode == "live" { if s.liveSearch {
q.Add("tweet_search_mode", s.searchMode) q.Add("tweet_search_mode", "live")
} else { }
q.Add("result_filter", s.searchMode) if s.resultFilter != "" {
q.Add("result_filter", s.resultFilter)
} }
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()