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.
|
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
|
```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
|
### Get profile
|
||||||
|
|
||||||
|
|
|
||||||
58
scraper.go
58
scraper.go
|
|
@ -15,9 +15,25 @@ type Scraper struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
guestToken string
|
guestToken string
|
||||||
includeReplies bool
|
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
|
var defaultScraper *Scraper
|
||||||
|
|
||||||
// New creates a Scraper object
|
// New creates a Scraper object
|
||||||
|
|
@ -27,43 +43,15 @@ func New() *Scraper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSearchLive enable/disable realtime search
|
// SetSearchMode switcher
|
||||||
func (s *Scraper) SetSearchLive(srctype bool) *Scraper {
|
func (s *Scraper) SetSearchMode(mode SearchMode) *Scraper {
|
||||||
if srctype {
|
s.searchMode = mode
|
||||||
s.searchMode = "live"
|
|
||||||
}
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSearchLive wrapper for default SetSearchLive
|
// SetSearchMode wrapper for default Scraper
|
||||||
func SetSearchLive(srctype bool) *Scraper {
|
func SetSearchMode(mode SearchMode) *Scraper {
|
||||||
return defaultScraper.SetSearchLive(srctype)
|
return defaultScraper.SetSearchMode(mode)
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithReplies enable/disable load timeline with tweet replies
|
// 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 != "" {
|
if cursor != "" {
|
||||||
q.Add("cursor", cursor)
|
q.Add("cursor", cursor)
|
||||||
}
|
}
|
||||||
if s.searchMode == "live" {
|
switch s.searchMode {
|
||||||
q.Add("tweet_search_mode", s.searchMode)
|
case SearchLatest:
|
||||||
} else {
|
q.Add("tweet_search_mode", "live")
|
||||||
q.Add("result_filter", s.searchMode)
|
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()
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue