Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2015 01:27
Show Gist options
  • Save anonymous/c553949a795d96795bbf to your computer and use it in GitHub Desktop.
Save anonymous/c553949a795d96795bbf to your computer and use it in GitHub Desktop.
export retweets info to csv
import json
import datetime
from csv import writer
now = datetime.datetime.now()
"""
CONSUMER_KEY = 'XXX'
CONSUMER_SECRET = 'XXX'
OAUTH_TOKEN = 'XXX'
OAUTH_TOKEN_SECRET = 'XXX'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
# this is rate-limited, we want at least 100 of these retweets
_retweets = twitter_api.statuses.retweets(id=560598457371881472, count=100)
"""
# test dataset
fake_retweets = [{"screen_name": "some_bot",
"followers_count":9000,
"friends_count": -1,
"profile_image_url": "hello.jpg",
"created_at": "end of the universe"},
{"screen_name": "real_dude",
"followers_count": 5,
"friends_count": 42,
"profile_image_url": "happyface.jpg",
"created_at": "before it was cool"},
]
outfn = "twitter_retweet_data_%i.%i.%i.txt" % (now.month, now.day, now.year)
#NAMES FOR HEADER ROW IN OUTPUT FILE
json_fields = "id screen_name name created_at url followers_count friends_count \
statuses_count profile_image_url".split()
csv_fields = ["screen_name", "followers_count", "friends_count", "profile_image_url", "created_at"]
csv_file = open(outfn, "w")
csv_file.write(", ".join(csv_fields) + "\n") # header
csv_writer = writer(csv_file)
for entry in fake_retweets:
# ====== CSV ========
# this is called list comprehension
values = [unicode(entry[field]) for field in csv_fields]
# equivalently:
"""
values = []
for field in csv_fields:
values.append(unicode(entry[field]))
"""
#WRITE ROW WITH DATA IN LIST
csv_writer.writerow(values)
print "writing:", values
csv_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment