Karl Dailey
Karl Dailey

Reputation: 118

how do i pull geotaged tweets in python

I had a python file that I used to pull geotagged tweets from twitter, but now it requires OAuth. I'm trying to work with 1.1 but I'm not sure what to do next. My code below is from the Twitter API documentation, with some minor changes since the documentation is wrong (force_auth_header=True was removed and set body="").

I searched around, but either the posts are outdated or confusing. I think I'm close, but not sure where to go next. Ultimately I'd like to use something like search_url3, but I know that format is incorrect because I can tell it is getting a 401 error.

In the mean time, if I can get the results in a json format, that'd be great.

I used to use something like this:

search = urllib.urlopen("http://search.twitter.com/search.json?q=&rpp=100&geocode=39.95256,-75.164,2mi)
for result in j["results"]:
    ...

My current code:

import oauth2 as oauth

consumer_key="123"
consumer_secret="345"
Access_token="567"
Access_token_secret="890"

def oauth_req(url, key, secret, http_method="GET",http_headers=None):
     consumer = oauth.Consumer(key, secret)
     token = oauth.Token(Access_token,Access_token_secret)
     client = oauth.Client(consumer, token)
     resp, content = client.request(url,method=http_method,body="",
          headers=http_headers)

     print resp

return content

 search_url2="https://api.twitter.com/1.1/search/tweets.json?q=&geocode=39.95256,-75.164,2mi"
 search_url3="https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4"
 home_timeline = oauth_req(search_url3,  consumer_key,  consumer_secret)

Upvotes: 0

Views: 206

Answers (1)

alonisser
alonisser

Reputation: 12068

Basically, unless this is somekind of homework, you are doing it wrong. there are terrific (and not so terrific) python-twitter libs and wrappers, unless building one is your core mission than use one of them:

Etc..

Upvotes: 1

Related Questions