Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (46 sloc) 1.39 KB
# 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 = [
# API base URL
base_url = ''
# Function to add row to accounts table
def insert_db(handle, likes, talking):
conn = sqlite3.connect('social_data.db')
cur = conn.cursor()
INSERT INTO fbaccounts VALUES (?,?,?,?);
''', (, handle, likes, talking))
# Create the database if it doesn't exist
if not os.path.exists('social_data.db'):
conn = sqlite3.connect('social_data.db')
# 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)
# 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.