This notebook shows how to use tweepy module to download information about your Twitter followers, then how to find GPS coordinates of your followers' locations using googlemaps module, and, finally, how to plot your followers' locations on an interactive Google Map using gmaps module.
To run this code you will need Twitter API and Google API keys stored as a dictionary
in twkeys.py
file:
keys = dict(
screen_name = 'your_twitter_user_name',
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXX',
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
google_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
)
If you have more than 500 followers I recommend to split the job into several batches. Otherwise, you may hit APIs limit for the number of requests.
import tweepy # twitter API
import googlemaps # Google Maps API
import gmaps # Google maps for Jupyter notebooks
from datetime import datetime
from collections import Counter
# twkeys.py file contains Twitter API and Google API keys
# Get Twitter API keys from https://apps.twitter.com/app/new
# Get Google API key from https://developers.google.com/maps/documentation/javascript/get-api-key
from twkeys import keys
SCREEN_NAME = keys['screen_name']
CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
GOOGLE_API_KEY = keys['google_api_key']
geoloc = googlemaps.Client(key = GOOGLE_API_KEY)
gmaps.configure(api_key=GOOGLE_API_KEY)
First, we acquired followers' ids. For each of them, we request a user information (incl. the location). Totally, we have found the locations of 1820 followers.
# get list of followers' ids
followers = api.followers_ids(SCREEN_NAME)
# get locations of our followers
# warning: you might run into problems with Twitter API quotas
for i in xrange(len(followers)):
u = api.get_user(followers[i])
loc = u.location
name = u.screen_name
locations.append({'id': followers[i], "screen_name": name, "address": loc})
# how many locations do we have?
addresses = [x['address'] for x in locations if x['address'] != ""]
print(len(addresses))
addresses[:10]
1820
[u'Colorado, USA', u'Nairobi, Kenya', u'Brno, Czech Republic', u'Austin, TX', u'Kenya', u'La Coru\xf1a', u'Nepal', u'Berlin, Germany', u'Accra,Ghana', u'Chennai, India']
To find GPS coordinates corresponding to each address, we have used googlemaps module. We were able to identify GPS positions for 1724 followers coming from 96 different countries.
# needs ~10 minutes to run
for i in range(len(locations)):
if locations[i]['address'] != "":
locations[i]['gps'] = geoloc.geocode(locations[i]['address'])
else:
locations[i]['gps'] = None
# how many followers can be geolocated?
markers = [x['gps'][0]['geometry']['location'] for x in locations if x['gps'] is not None and len(x['gps']) > 0]
len(markers)
1724
# how many countries?
from collections import Counter
tmp = [x['gps'][0]['address_components'] for x in locations if x['gps'] is not None and len(x['gps'])>0]
countries = [item['long_name'] for sublist in tmp for item in sublist if item['types'][0] == "country"]
x = Counter(countries)
print(len(x))
x
96
Counter({u'Albania': 1, u'Algeria': 3, u'Angola': 2, u'Argentina': 16, u'Australia': 23, u'Austria': 6, u'Bangladesh': 51, u'Belgium': 13, u'Bolivia': 1, u'Bosnia and Herzegovina': 2, u'Brazil': 52, u'Bulgaria': 3, u'Canada': 29, u'Chile': 8, u'China': 37, u'Colombia': 14, u'Costa Rica': 1, u'Cuba': 1, u'Cyprus': 1, u'Czechia': 46, u'Denmark': 8, u'Ecuador': 4, u'Egypt': 10, u'Estonia': 1, u'Finland': 5, u'France': 40, u'Germany': 76, u'Ghana': 6, u'Greece': 5, u'Guatemala': 6, u'Hong Kong': 6, u'Hungary': 1, u'India': 173, u'Indonesia': 8, u'Iran': 13, u'Iraq': 2, u'Ireland': 11, u'Israel': 6, u'Italy': 17, u'Jamaica': 1, u'Japan': 9, u'Jordan': 1, u'Kenya': 14, u'Kuwait': 1, u'Latvia': 1, u'Lebanon': 2, u'Lithuania': 2, u'Luxembourg': 1, u'Macedonia (FYROM)': 2, u'Malaysia': 2, u'Mauritius': 1, u'Mexico': 27, u'Mongolia': 2, u'Morocco': 1, u'Nepal': 8, u'Netherlands': 26, u'New Zealand': 5, u'Nicaragua': 2, u'Nigeria': 16, u'Norway': 1, u'Pakistan': 1, u'Paraguay': 1, u'Peru': 2, u'Philippines': 8, u'Poland': 17, u'Portugal': 9, u'Puerto Rico': 1, u'Qatar': 2, u'Romania': 8, u'Russia': 23, u'Saudi Arabia': 1, u'Senegal': 2, u'Serbia': 3, u'Singapore': 7, u'Slovakia': 4, u'Slovenia': 1, u'South Africa': 11, u'South Korea': 3, u'Spain': 62, u'Sri Lanka': 1, u'Sweden': 9, u'Switzerland': 9, u'Taiwan': 10, u'Thailand': 3, u'Togo': 1, u'Tunisia': 3, u'Turkey': 16, u'Uganda': 2, u'Ukraine': 12, u'United Arab Emirates': 7, u'United Kingdom': 94, u'United States': 514, u'Uruguay': 3, u'Venezuela': 14, u'Vietnam': 12, u'Zimbabwe': 3})
# save everything to a file
import gzip, cPickle
with gzip.open('tmp.gz','wb') as f:
cPickle.dump(locations, f, 2)
# can be loaded as follows
# with gzip.open('tmp.gz','rb') as f:
# locations = cPickle.load(f)
# reshape data for Google Maps (expected nput: list of tuples)
gpspos = list()
for x in locations:
if x['gps'] is not None and len(x['gps'])>0:
gpspos.append((x['gps'][0]['geometry']['location']['lat'], x['gps'][0]['geometry']['location']['lng']))
# create the plot
fig = gmaps.Map()
followers_layer = gmaps.symbol_layer(gpspos, fill_color="green", stroke_color="green", scale=2)
fig.add_layer(followers_layer)
fig
Failed to display Jupyter Widget of type Map
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.