简体   繁体   中英

Using twitter search api with python

What I am trying to accomplish is to get the tweets around a specific geocode using twitter's search api. I am using python OAuth2 (I was using python-twitter but it was buggy).

My hacky code is as follows

class Herp:
    def oauth_req(self,url, http_method="GET", post_body=None, http_headers=None):
        consumer = oauth.Consumer(key=credentials.CONSUMER_KEY, secret=credentials.CONSUMER_SECRET)
        token = oauth.Token(key=credentials.ACCESS_TOKEN, secret=credentials.ACCESS_TOKEN_SECRET)
        client = oauth.Client(consumer, token)

        resp, content = client.request(
        url,
        method=http_method
        )
        return content

    def search(self):
        self.searchResults = self.oauth_req('https://api.twitter.com/1.1/search/tweets.json?q=geocode="42.3581,-71.0636,10mi"&count=100&type=recent')

    def __init__(self):
        self.client = None
        self.SearchResults = None

The request works and from there I am given a response in the format I expected, however there are no tweets included.

Here is the JSON response:

{"statuses":[],"search_metadata":{"completed_in":0.013,"max_id":445673138282442752,"max_id_str":"445673138282442752","query":"goeocode%3D%2242.3581%2C-71.0636%2C10mi%22","refresh_url":"?since_id=445673138282442752&q=goeocode%3D%2242.3581%2C-71.0636%2C10mi%22&include_entities=1","count":100,"since_id":0,"since_id_str":"0"}}

Sorry about the horrible formatting, but you can see the first thing it has is "statuses":[] This is supposed to be an array of tweet response objects from twitter. See here for details -> https://dev.twitter.com/docs/api/1.1/get/search/tweets

Quick note, I know that there should be tweets because of the "count":100 metadata given in the response object. (I have also tried limiting my responses to 1 tweet, but still none are returned)

I don't think this is a timeout issue, and I haven't found any relevant help on the web so I'm out of ideas. Any help would be appreciated!

Update:

I fixed it thanks to Drewness pointing my eye at the url. I realized that the required query string param "q" was not set and therefore my query string was incorrect.

q=geocode="42.3581,-71.0636,10mi"&count=100&type=recent

Needed to be changed to

q=""&geocode="42.3581,-71.0636,10mi"&count=100&type=recent

However, I can't answer my own question within 8 hours of asking it..

Look like you have some letters mixed up:

...
goeocode=
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM