2626# Excuse the ugly code. I threw this together as quickly as possible and I
2727# don't normally code in Python.
2828#
29+
30+ # In Sanders' original form, the code was using Twitter API 1.0.
31+ # Now that Twitter moved to 1.1, we had to make a few changes.
32+ # Cf. twitterauth.py for the details.
33+
2934import csv
30- import getpass
3135import json
3236import os
3337import time
3438import urllib
3539
40+ from twitterauth import CONSUMER_KEY , CONSUMER_SECRET , ACCESS_TOKEN_KEY , ACCESS_TOKEN_SECRET
41+
42+ import twitter
43+ api = twitter .Api (consumer_key = CONSUMER_KEY , consumer_secret = CONSUMER_SECRET ,
44+ access_token_key = ACCESS_TOKEN_KEY , access_token_secret = ACCESS_TOKEN_SECRET )
45+
3646
3747def get_user_params (data_path ):
3848
@@ -124,7 +134,6 @@ def download_tweets(fetch_list, raw_dir):
124134
125135 # download tweets
126136 for idx in range (0 , len (fetch_list )):
127-
128137 # current item
129138 item = fetch_list [idx ]
130139
@@ -133,9 +142,16 @@ def download_tweets(fetch_list, raw_dir):
133142 print '--> downloading tweet #%s (%d of %d) (%s left)' % \
134143 (item [2 ], idx + 1 , len (fetch_list ), trem )
135144
145+ # Old Twitter API 1.0
136146 # pull data
137- url = 'http://api.twitter.com/1/statuses/show.json?id=' + item [2 ]
138- urllib .urlretrieve (url , raw_dir + item [2 ] + '.json' )
147+ # url = 'https://api.twitter.com/1/statuses/show.json?id=' + item[2]
148+ # print url
149+ # urllib.urlretrieve(url, raw_dir + item[2] + '.json')
150+
151+ # New Twitter API 1.1
152+ json_data = api .GetStatus (item [2 ]).AsJsonString ()
153+ with open (raw_dir + item [2 ] + '.json' , "w" ) as f :
154+ f .write (json_data + "\n " )
139155
140156 # stay in Twitter API rate limits
141157 print ' pausing %d sec to obey Twitter API rate limits' % \
@@ -236,8 +252,6 @@ def main(data_path):
236252 build_output_corpus (user_params ['outList' ], user_params ['rawDir' ],
237253 total_list )
238254
239- return
240-
241255
242256if __name__ == '__main__' :
243- main (os . path . join ( ".." , " data") )
257+ main (" data" )
0 commit comments