Use enum search mode
This commit is contained in:
parent
1f25820556
commit
0c484a61b1
3 changed files with 45 additions and 42 deletions
16
README.md
16
README.md
|
|
@ -65,12 +65,22 @@ func main() {
|
|||
|
||||
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.
|
||||
|
||||
|
||||
#### Set search mode
|
||||
|
||||
```golang
|
||||
scraper.SearchLive(true)
|
||||
scraper.SetSearchMode(twitterscraper.SearchLatest)
|
||||
```
|
||||
|
||||
See [Rules and filtering](https://developer.twitter.com/en/docs/tweets/rules-and-filtering/overview/standard-operators) for build standard queries.
|
||||
Options:
|
||||
|
||||
* `twitterscraper.SearchTop` - default mode
|
||||
* `twitterscraper.SearchLatest` - live mode
|
||||
* `twitterscraper.SearchPeople` - user mode
|
||||
* `twitterscraper.SearchPhotos` - image mode
|
||||
* `twitterscraper.SearchVideos` - video mode
|
||||
|
||||
### Get profile
|
||||
|
||||
|
|
|
|||
58
scraper.go
58
scraper.go
|
|
@ -15,9 +15,25 @@ type Scraper struct {
|
|||
client *http.Client
|
||||
guestToken string
|
||||
includeReplies bool
|
||||
searchMode string
|
||||
searchMode SearchMode
|
||||
}
|
||||
|
||||
// SearchMode type
|
||||
type SearchMode int
|
||||
|
||||
const (
|
||||
// SearchTop - default mode
|
||||
SearchTop SearchMode = iota
|
||||
// SearchLatest - live mode
|
||||
SearchLatest
|
||||
// SearchPeople - user mode
|
||||
SearchPeople
|
||||
// SearchPhotos - image mode
|
||||
SearchPhotos
|
||||
// SearchVideos - video mode
|
||||
SearchVideos
|
||||
)
|
||||
|
||||
var defaultScraper *Scraper
|
||||
|
||||
// New creates a Scraper object
|
||||
|
|
@ -27,43 +43,15 @@ func New() *Scraper {
|
|||
}
|
||||
}
|
||||
|
||||
// SetSearchLive enable/disable realtime search
|
||||
func (s *Scraper) SetSearchLive(srctype bool) *Scraper {
|
||||
if srctype {
|
||||
s.searchMode = "live"
|
||||
}
|
||||
// SetSearchMode switcher
|
||||
func (s *Scraper) SetSearchMode(mode SearchMode) *Scraper {
|
||||
s.searchMode = mode
|
||||
return s
|
||||
}
|
||||
|
||||
// SetSearchLive wrapper for default SetSearchLive
|
||||
func SetSearchLive(srctype bool) *Scraper {
|
||||
return defaultScraper.SetSearchLive(srctype)
|
||||
}
|
||||
|
||||
// SetSearchPhotos filter search for photos only
|
||||
func (s *Scraper) SetSearchPhotos(srctype bool) *Scraper {
|
||||
if srctype {
|
||||
s.searchMode = "image"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SetSearchPhotos wrapper for default SetSearchPhotos
|
||||
func SetSearchPhotos(srctype bool) *Scraper {
|
||||
return defaultScraper.SetSearchPhotos(srctype)
|
||||
}
|
||||
|
||||
// SetSearchVideos filter search for videos only
|
||||
func (s *Scraper) SetSearchVideos(srctype bool) *Scraper {
|
||||
if srctype {
|
||||
s.searchMode = "video"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SetSearchVideos wrapper for default SetSearchVideos
|
||||
func SetSearchVideos(srctype bool) *Scraper {
|
||||
return defaultScraper.SetSearchVideos(srctype)
|
||||
// SetSearchMode wrapper for default Scraper
|
||||
func SetSearchMode(mode SearchMode) *Scraper {
|
||||
return defaultScraper.SetSearchMode(mode)
|
||||
}
|
||||
|
||||
// WithReplies enable/disable load timeline with tweet replies
|
||||
|
|
|
|||
13
search.go
13
search.go
|
|
@ -37,10 +37,15 @@ func (s *Scraper) FetchSearchTweets(query string, maxTweetsNbr int, cursor strin
|
|||
if cursor != "" {
|
||||
q.Add("cursor", cursor)
|
||||
}
|
||||
if s.searchMode == "live" {
|
||||
q.Add("tweet_search_mode", s.searchMode)
|
||||
} else {
|
||||
q.Add("result_filter", s.searchMode)
|
||||
switch s.searchMode {
|
||||
case SearchLatest:
|
||||
q.Add("tweet_search_mode", "live")
|
||||
case SearchPeople:
|
||||
q.Add("result_filter", "user")
|
||||
case SearchPhotos:
|
||||
q.Add("result_filter", "image")
|
||||
case SearchVideos:
|
||||
q.Add("result_filter", "video")
|
||||
}
|
||||
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue