Created
September 9, 2012 02:48
-
-
Save fabiocerqueira/3682266 to your computer and use it in GitHub Desktop.
Brazil Trends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import urllib | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
WORLDWIDE = 1 | |
BRAZIL = 23424768 | |
def get_trends_topics(woeid=WORLDWIDE): | |
URL = 'https://api.twitter.com/1/trends/%d.json' | |
data = urllib.urlopen(URL % woeid).read() | |
data_as_python = json.loads(data) | |
trends = data_as_python[0]['trends'] | |
return trends | |
def get_trend(keyword, rpp=1): | |
URL = 'https://search.twitter.com/search.json?q=%s&rpp=%d' | |
data = urllib.urlopen(URL % (keyword, rpp)).read() | |
data_as_python = json.loads(data) | |
results = data_as_python['results'] | |
tweets = [] | |
for r in results: | |
tweets.append({ | |
'tweet': r['text'], | |
'usuario': r['from_user_name'], | |
'postado_em': r['created_at'] | |
}) | |
return tweets | |
def save_json(filename, json_as_python, indent=None): | |
with open(filename, 'w') as f: | |
data_json = json.dumps(json_as_python, indent=indent) | |
f.write(data_json.encode('utf-8')) | |
if __name__ == '__main__': | |
trends = get_trends_topics(BRAZIL) | |
result = [] | |
for t in trends: | |
result.append({ | |
'trend': t['name'], | |
'tweets': get_trend(t['query'], 10) | |
}) | |
save_json('tt_br.json', result, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment