Skip to content

Instantly share code, notes, and snippets.

@deathweaselx86
Created December 11, 2017 15:52
Show Gist options
  • Save deathweaselx86/020f2f509927c1e82b8ebc7ef9270fb9 to your computer and use it in GitHub Desktop.
Save deathweaselx86/020f2f509927c1e82b8ebc7ef9270fb9 to your computer and use it in GitHub Desktop.
the end of some twitter stuff
import twitter
import json
import urllib2
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
def authenticate():
CONSUMER_KEY = 'secret'
CONSUMER_SECRET = 'supersecret'
OAUTH_TOKEN = 'mytoken'
OAUTH_TOKEN_SECRET = 'mytokensecret'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
return twitter.Twitter(auth=auth)
def get_trends(twitter_api, where=US_WOE_ID, full_records=False):
trends = twitter_api.trends.place(_id=where)
if full_records:
return trends
else:
return [t for t in trends[0]['trends']]
if __name__ == '__main__':
twitter_api = authenticate()
if twitter_api:
print "Successfully authenticated."
else:
exit(1)
query = "#Python"
search_results = twitter_api.search.tweets(q=query, count=1000)
we_care_about = ["text", "favorite_count", "created_at"]
print json.dumps(search_results["search_metadata"], indent=1)
status_file = open("statuses.json", "w")
status_file.write('{\n')
while True:
statuses = search_results["statuses"]
for status in statuses:
mini_dict = {}
[mini_dict.setdefault(key, status[key]) for key in we_care_about]
json.dump(mini_dict, status_file, indent=1, sort_keys=True)
status_file.write(',')
next_results = search_results["search_metadata"].get("next_results")
if not next_results:
break
kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ])
search_results = twitter_api.search.tweets(**kwargs)
status_file.write('\n}')
status_file.close()
"""
trends = get_trends(twitter_api, full_records=False)
trend_file = open("trends.json", "w")
print "Current trends:", json.dumps(trends, indent=1)
print "Adding trends to trend_file.",
json.dump(trends, trend_file, indent=1, sort_keys=True)
trend_file.close()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment