Created
January 30, 2015 14:28
-
-
Save debrouwere/7cb6a3ba6d7fd8583d9e to your computer and use it in GitHub Desktop.
social media competitive analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tortilla | |
import tweepy | |
import facepy | |
from apiclient import discovery | |
import pandas as pd | |
twitter_credentials = tweepy.AppAuthHandler( | |
os.environ['TWITTER_CONSUMER_KEY'], | |
os.environ['TWITTER_CONSUMER_SECRET'], | |
) | |
twitter = tweepy.API(twitter_credentials) | |
facebook = facepy.GraphAPI() | |
plus = discovery.build('plus', 'v1', developerKey=os.environ['GOOGLE_API_KEY']) | |
youtube = discovery.build('youtube', 'v3', developerKey=os.environ['GOOGLE_API_KEY']) | |
instagram = tortilla.wrap('https://api.instagram.com/v1', params={ | |
'client_id': os.environ['INSTAGRAM_CLIENT_ID'] | |
}) | |
def get_twitter(screen_name): | |
user = twitter.get_user(screen_name) | |
return user.followers_count | |
def get_facebook(username): | |
return facebook.get(username)['likes'] | |
def get_plus(username): | |
user = plus.people().get(userId=username).execute() | |
return user['plusOneCount'] | |
def get_instagram(username): | |
search = instagram.users.search.get(params={'q': username}) | |
user_id = search.data[0].id | |
counts = instagram.users.get(user_id).data.counts | |
return counts.followed_by | |
def get_youtube(username): | |
response = youtube.channels().list(part='statistics', forUsername=username).execute() | |
statistics = response['items'][0]['statistics'] | |
return statistics['subscriberCount'] | |
accounts = pd.DataFrame.from_csv('competitors.csv', index_col=1) | |
followers = pd.DataFrame(columns=accounts.columns, dtype='int64') | |
followers.type = accounts.type | |
for platform in accounts.columns[1:]: | |
get_followers = globals()['get_' + platform] | |
followers[platform] = accounts[platform].dropna().map(get_followers) | |
cols = followers.columns[1:] | |
ranks = followers.copy() | |
ranks[cols] = followers[cols].rank(ascending=False) | |
followers.to_csv('followers.csv') | |
ranks.to_csv('followers-ranks.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment