Part One - APIs - Retrieve Data From Twitter


In [ ]:
# Import everything we need

import twitter
import csv
from datetime import datetime

In [ ]:
# Use the Twitter OAuth credentials from the app you created to receive an access token to access your own data

api = twitter.Api(consumer_key='',
                  consumer_secret='',
                  access_token_key='',
                  access_token_secret='')

# Verify that your credentials are working
print(api.VerifyCredentials())

In [ ]:
# Fetch a single user's public status
statuses = api.GetUserTimeline(screen_name='rdempsey')
for s in statuses:
    print(s.text)

In [ ]:
user = api.GetUser(screen_name='rdempsey')

# Print out the entire user object
# If you're using SublimeText2, you can easily format it with this plugin: https://github.com/dzhibas/SublimePrettyJson
# print(user)

# Print out a few of the available attributes
# No prettytable here, yet
print("Name: {}".format(user.name))
print("Geo Enabled: {}".format(user.geo_enabled))
print("Description: {}".format(user.description))
print("Follower Count: {}".format(user.followers_count))
print("Friend Count: {}".format(user.friends_count))
print("Verified: {}".format(user.verified))

In [ ]:
# Print out the entire user object as a dictionary
print(user.AsDict())

In [ ]:
# Make it look nicer
for key, value in user.AsDict().items():
    print(key, value)

In [ ]:
# Getting the information for one user is great, but getting the information for many is even better
# Save everything we can do a CSV, including the date we created the record.
# You never know what you'll want for a later analysis.

profiles_to_retrieve = ['rdempsey', 'DataWranglersDC', 'DataCommunityDC', 'datasocietyco', 'boboroshi']

with open('twitter_user_data.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
    writer.writerow(['id', 'screen_name', 'name', 'description', 'location', 'lang', 'geo_enabled', 
                     'favourites_count', 'followers_count', 'friends_count', 'listed_count', 'default_profile_image',
                     'account_created_at', 'time_zone', 'url', 'statuses_count', 'profile_background_color', 
                     'profile_background_image_url', 'profile_background_tile', 'profile_banner_url', 'profile_image_url', 
                     'profile_link_color', 'profile_sidebar_fill_color', 'profile_text_color', 'protected', 'created_at'])
    for u in profiles_to_retrieve:
        user = api.GetUser(screen_name='{}'.format(u))
        writer.writerow([user.id, user.screen_name, user.name, user.description, user.location, user.lang, user.geo_enabled,
                         user.favourites_count, user.followers_count, user.friends_count, user.listed_count, user.default_profile_image,
                         user.created_at, user.time_zone, user.url, user.statuses_count, user.profile_background_color,
                         user.profile_background_image_url, user.profile_background_tile, user.profile_banner_url,
                         user.profile_image_url, user.profile_link_color, user.profile_sidebar_fill_color,
                         user.profile_text_color, user.protected, datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')])

# So we know that the CSV file was indeed created
print("CSV creation complete")

In [ ]: