Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 55 lines (46 sloc) 1.422 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# Fetch Facebook page metrics via Social Graph API into a SQLite DB
# Grabs the number of likes and "talking about" numbers

import requests
import sqlite3
import os
from datetime import datetime

# These are the accounts for which you will fetch data
names_list = [
    'fallingskies',
    'usatoday'
]

# API base URL
base_url = 'https://graph.facebook.com/'


# Function to add row to accounts table
def insert_db(handle, likes, talking):
    conn = sqlite3.connect('social_data.db')
    cur = conn.cursor()
    cur.execute('''
INSERT INTO fbaccounts VALUES (?,?,?,?);
''', (datetime.now(), handle, likes, talking))
    conn.commit()
    conn.close()

# Create the database if it doesn't exist
if not os.path.exists('social_data.db'):
    conn = sqlite3.connect('social_data.db')
    conn.close()
else:
    pass

# Create the table if it's not in the db
conn = sqlite3.connect('social_data.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS fbaccounts
(FetchDate Date, Handle Text, Likes Integer, Talking Integer)
''')
conn.commit()
conn.close()

# Iterate over handles and hit the API with each
for user in names_list:
    url = base_url + user
    print 'Fetching ' + user
    response = requests.get(url)
    profile = response.json()
    handle = profile['name']
    likes = profile['likes']
    talking = profile['talking_about_count']
    insert_db(handle, likes, talking)
Something went wrong with that request. Please try again.