Skip to content

Instantly share code, notes, and snippets.

@xiez
Created July 31, 2014 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiez/7ead0ff75f55b4dd02ec to your computer and use it in GitHub Desktop.
Save xiez/7ead0ff75f55b4dd02ec to your computer and use it in GitHub Desktop.
import time
import urllib2
import json
import oauth2
url1 = "https://api.twitter.com/1.1/search/tweets.json"
params = {
"oauth_version": "1.0",
"oauth_nonce": oauth2.generate_nonce(),
"oauth_timestamp": int(time.time())
}
consumer = oauth2.Consumer(key="1", secret="2")
token = oauth2.Token(key="3", secret="4")
prev_id = int("12345")
for i in range(1000000):
url = url1
params["q"] = ""
params["count"] = 1000000
params["geocode"] = "53.5,-8,318km"
params["lang"] = "en"
params["locale"] = ""
params["result_type"] = "" # Example Values: mixed, recent, popular
params["until"] = ""
params["since_id"] = ""
params["max_id"] = str(prev_id)
params["include_entities"] = ""
req = oauth2.Request(method="GET", url=url, parameters=params)
signature_method = oauth2.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
headers = req.to_header()
url = req.to_url()
response = urllib2.Request(url)
resp = urllib2.urlopen(response)
if int(resp.info().dict['x-rate-limit-remaining']) < 1:
print 'rate limit reached, wait for 5 secs...'
time.sleep(5)
continue
data = json.load(resp)
print data
if data["statuses"] == []:
print "end of data"
break
else:
prev_id = int(data["statuses"][-1]["id"]) - 1
print prev_id, i
f = open("outfile_" + str(i) + ".json", "w")
json.dump(data["statuses"], f)
f.close()
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment