Skip to content

Instantly share code, notes, and snippets.

@AlessandraSozzi
Last active July 3, 2016 12:17
Show Gist options
  • Save AlessandraSozzi/2f1cff4faabba611c87075128e924a70 to your computer and use it in GitHub Desktop.
Save AlessandraSozzi/2f1cff4faabba611c87075128e924a70 to your computer and use it in GitHub Desktop.
Create a graph of your Twitter Friends (based on who follows who)
import tweepy
import os
import pandas as pd
import time
import json
import codecs
# Authentication params
APP_NAME = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
# == OAuth Authentication Test ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
basePath = os.getcwd()
friends = pd.read_json(basePath + "/twitterFriends.json", orient = 'records', dtype = 'str')
friends.set_index(friends['id'].astype(long), inplace = True)
friends['idx'] = range(len(friends))
links = []
nodes = []
retry = []
def process_user(screen_name):
ids = []
try:
for page in tweepy.Cursor(api.friends_ids, screen_name = screen_name).pages():
f = set(page).intersection(set(friends.index))
ids.extend(f)
time.sleep(60)
except tweepy.TweepError:
print("Failed to run the command on that user, Skipping...")
retry.append(screen_name)
return ids
for user_id in friends.index:
user = friends.loc[user_id]
idx = user['idx']
screen_name = user['screen_name']
name = user['name']
followers_count = user['followers_count']
friends_count = user['friends_count']
verified = user['verified']
print 'Working on user: %s ' %screen_name
user_friends = process_user(screen_name)
if screen_name not in retry:
node = { 'screen_name': screen_name,
'name' : name,
'followers_count' : followers_count,
'friends_count' : friends_count,
'verified' : verified,
'id': idx }
nodes.append(node)
for u in user_friends:
link = {'source': idx, 'target': friends.loc[user_id]['idx'], 'value': 1}
links.append(link)
graph = { 'nodes': nodes, 'links' : links }
json_string = json.dumps(graph)
with codecs.open('graph.json', 'w', 'utf8') as f:
f.write(json_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment