In [32]:
__author__ = 'Mike'
import csv
import requests
import pprint
import gspread
import sys
from time import sleep
import socket
import json
import pandas as pd
from oauth2client.client import SignedJwtAssertionCredentials

#Login to Google Drive
json_key = json.load(open('Update Script-b4827ff38372.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
gc = gspread.authorize(credentials)

#Grab FB API Key
json_fb_key = json.load(open('fb_api_key.json'))
apikey = json_fb_key['credentials']['apikey'].encode('ascii','ignore')

#Create a function that reads a Spreadsheet from Gdrive and stores the relevant column as a variable.

def getFBdataGroup(apikey,postid):
    tries = 5
    while tries >= 0:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'?access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue

def readSheetCol(sheet,tab,col):
    sheet = gc.open(sheet)
    input_sheet = sheet.worksheet(tab)
    #store the postids in a list
    values_list = input_sheet.col_values(col)
    #make sure we start processing values on the second row of the sheet
    return values_list

def getFBdataReach(apikey,postid):
    tries = 5
    while tries >= 0:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'/insights/post_impressions_unique?access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue


def getFBdataViralReach(apikey,postid):
    tries = 5
    while tries >= 5:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'/insights/post_impressions_viral_unique?access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue

def getFBdataLinkClicks(apikey,postid):
    tries = 5
    while tries >= 5:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'/insights/post_consumptions_by_type?access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue

def getFBdataComments(apikey,postid):
    tries = 5
    while tries >= 5:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'/comments?summary=true&access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue

def getFBdataLikes(apikey,postid):
    tries = 5
    while tries >= 5:
        try:
            endpoint = 'https://graph.facebook.com/v2.3/'+postid+'/likes?summary=true&access_token='+apikey
            response = requests.get(endpoint)
            fb_data = response.json()
            return fb_data
        except:
            if tries == 0:
                break
            else:
                sleep(3)
                tries -= 1
                continue

In [33]:
sheet = gc.open("Atlas Facebook Tracker")
analysis_sheet = sheet.worksheet("Analysis")
values_list = readSheetCol("Atlas Facebook Tracker","Analysis",14)
col_date = readSheetCol("Atlas Facebook Tracker","Analysis",1)
col_url = readSheetCol("Atlas Facebook Tracker","Analysis",3)
d = {'PostID' : pd.Series(values_list[1:], index=[item for item in range(len(values_list)-1)]),
        "Date" : pd.Series(col_date[1:], index=[item for item in range(len(col_date)-1)]),
        "URL" : pd.Series(col_url[1:], index=[item for item in range(len(col_url)-1)])}

In [34]:
df = pd.DataFrame(d)

In [37]:
df[2565:2575]


Out[37]:
Date PostID URL
2565 10/26/2015 103921782727_10153761860127728 http://www.atlasobscura.com/articles/the-story...
2566 10/26/2015 103921782727_10153762037632728 http://www.atlasobscura.com/articles/are-there...
2567 10/26/2015 103921782727_10153761902322728 http://www.popularmechanics.com/space/news/a17...
2568 10/26/2015 103921782727_10153761847737728 http://www.atlasobscura.com/articles/indigent-...
2569 10/26/2015 103921782727_10153761987652728 NaN
2570 10/26/2015 103921782727_10153762015397728 NaN
2571 10/26/2015 103921782727_10153762362067728 NaN
2572 10/26/2015 103921782727_10153761913682728 NaN
2573 10/26/2015 103921782727_10153762038827728 NaN
2574 10/26/2015 103921782727_10153762141402728 NaN

In [17]:



Out[17]:
0
1 103921782727_10153010028757728
2 103921782727_10153036963492728
3 103921782727_10153038480942728
4 103921782727_10153038578012728
5 103921782727_10153038621807728
6 103921782727_10153038651627728
7 103921782727_10153038938732728
8 103921782727_10153038602752728
9 103921782727_10153038669732728
10 103921782727_10153039091472728
11 103921782727_10153047430007728
12 103921782727_10153047438267728
13 103921782727_10153047415902728
14 103921782727_10153049613512728
15 103921782727_10153053319672728
16 103921782727_10153053096922728
17 103921782727_10153053122557728
18 103921782727_10153054072857728
19 103921782727_10153053851862728
20 103921782727_10153056891347728
21 103921782727_10153056929037728
22 103921782727_10153056918962728
23 103921782727_10153056942787728
24 103921782727_10153058234417728
25 103921782727_10153059068272728
26 103921782727_10153059056252728
27 103921782727_10153059083222728
28 103921782727_10153059089482728
29 103921782727_10153059982472728
30 103921782727_10153060077797728
... ...
2969 103921782727_10153807307507728
2970 103921782727_10153807309512728
2971 103921782727_10153807333942728
2972 103921782727_10153807335852728
2973 103921782727_10153807355597728
2974 103921782727_10153812840347728
2975 103921782727_10153812902467728
2976 103921782727_10153812940417728
2977 103921782727_10153812949607728
2978 103921782727_10153812933877728
2979 103921782727_10153813152702728
2980 103921782727_10153812805037728
2981 103921782727_10153813159997728
2982 103921782727_10153813380422728
2983 103921782727_10153813335807728
2984 103921782727_10153813544102728
2985 103921782727_10153813387602728
2986 103921782727_10153813260082728
2987 103921782727_10153813306987728
2988 103921782727_10153813264712728
2989 103921782727_10153813272687728
2990 103921782727_10153813268797728
2991 103921782727_10153813267892728
2992 103921782727_10153813372692728
2993 103921782727_10153814825412728
2994 103921782727_10153814883317728
2995 103921782727_10153814927152728
2996 103921782727_10153814975062728
2997 103921782727_10153815042827728
2998 103921782727_10153815166417728

2998 rows × 1 columns


In [ ]:


In [ ]: