Created
April 22, 2012 02:39
-
-
Save v/2442395 to your computer and use it in GitHub Desktop.
Python Get 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
import urllib | |
import urllib2 | |
import simplejson as json | |
_TWITTER_TRENDS_URL = 'http://api.twitter.com/1/trends.json' | |
# Grab a trends json from the twitter api | |
def _get_trends_json(): | |
try: | |
request = urllib2.Request(_TWITTER_TRENDS_URL) | |
response = urllib2.urlopen(request) | |
s = response.read(); | |
json_obj = json.loads(s) | |
return json_obj | |
except: | |
return None | |
# Get a tuple (trends, as_of) where trends is a list of objects | |
# with members name, source, and url, and as_of is a string | |
# representing the date of these trends | |
def twitter_stream_get_trends(): | |
trends_json = _get_trends_json() | |
if trends_json is None: | |
return None | |
trends = [] | |
for o in trends_json.get('trends'): | |
t = { | |
"word": o.get('name'), | |
"source": 'twitter' | |
} | |
trends.append(t) | |
return trends | |
def _main(): | |
trends = twitter_stream_get_trends() | |
for t in trends: | |
print t | |
print '' | |
if __name__ == '__main__': | |
exit(_main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment