geduldig / TwitterAPI

Minimal python wrapper for Twitter's REST and Streaming APIs
938 stars 263 forks source link

Query build and output #138

Closed BLT81 closed 4 years ago

BLT81 commented 4 years ago

I have two related questions / issues: Will this package handle more complex queries? I'm having difficulty getting to search using operators such as "has:media" and "-RT". Related to that, how can I get it to output the search term that it sends to Twitter? This would be useful in troubleshooting the first problem I'm having.

geduldig commented 4 years ago

I assume you are using Premium Search. Then your request should look something like this:

r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), {'query':'pizza -RT has:media'})

I'm not excactly sure what your second question is. What do you want to output your search term? Do you want TwitterAPI to save your query string for you?

BLT81 commented 4 years ago

Yes, I'm using premium. The second part is just to see how the package assembles the query because it seems like some operators are used outside of 'query' like dates or could they all be used as values of 'query'?

geduldig commented 4 years ago

The package doesn't "assemble" anything. It sends the params as a JSON object exactly as you pass it to the request method. This JSON object may contain any of the params documented here: https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search.

For example, to restrict your query to a date range you would do something like this:

r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), 
{
    'query':'pizza', 
    'fromDate':'201712220000', 
    'toDate':'201512220000'
})
BLT81 commented 4 years ago

Thank you! This package is great!