Last active
August 25, 2017 06:48
-
-
Save norsez/28dbf7e5df44e7d76599001c97ab1fa9 to your computer and use it in GitHub Desktop.
Dumping a Facebook post's comments and likes into CSV files
This file contains hidden or 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
# Loading post comments and likes by Norsez Orankijanan [email protected] | |
# based on example by James Thornton, http://jamesthornton.com | |
# Facebook API Docs | |
# https://developers.facebook.com/docs/graph-api/using-graph-api#reading | |
# Get Your Facebook Access Token Here... | |
# https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me | |
# Before running this script... | |
# Set your Facebook Access Token as an environment variable in your terminal: | |
#export ACCESS_TOKEN="EAACEdEose0cBAHZCLwgcdvTYfXqyahiPi7zEO3qWVBf6Yde3MIYEAa0L0IhvKUzJ2djfbf0XEkUumFdo7TMBSTwScul7RXlshMwn2H60uzjtZC6DpsJgpfOAH4YTXaaKrcoWzSs89IyeMUwNaiIOoT7WQR3tVP5ybnVRjC6H5WG9Ez1973MY8BZBAs8AooZD" | |
import io | |
import json | |
import urllib | |
import urllib.request | |
import pprint | |
import time | |
import csv | |
# get Facebook access token from environment variable | |
ACCESS_TOKEN = "EAACEdEose0cBAHuxmvyPjbqaZAPEmJjziRHbQhPEqnHz9Sffpddz9yw43V0qms0vS5f3WvhfXFSqxCXTvyXrY1ZAFLeNZAedmrfTDZAmiqdxm7ZBerrCVAu5QgBtIZA5NLXrZAPx8LB6y9AggxTvgF1BQSUEsOHdO03dxFGYr06Fuh30IBFVLQTbCtVULJOEVQZD" | |
POST_ID = "1059511694183885" | |
# build the URL for the API endpoint | |
host = "https://graph.facebook.com" | |
params = urllib.parse.urlencode({"access_token": ACCESS_TOKEN}) | |
def query(url): | |
# open the URL and read the response | |
resp = urllib.request.urlopen(url).read() | |
# convert the returned JSON string to a Python datatype | |
me = json.loads(resp) | |
# display the result | |
#pprint.pprint(me) | |
return me | |
def loadComments(): | |
path = "/v2.10/{}/comments".format(POST_ID) | |
url = "{host}{path}?{params}".format(host=host, path=path, params=params) | |
nextURL = url | |
fname = "output_facebook_comments_" + POST_ID + "_" + str(int(time.time())) + ".csv" | |
total = 0 | |
with io.open(fname,'w',encoding='utf8') as f: | |
rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
while nextURL != '': | |
#print("loading page: " + nextURL) | |
data = query(nextURL) | |
for r in data['data']: | |
from_name = r['from']['name'] | |
from_id = r['from']['id'] | |
rowWriter.writerow([r['created_time'], | |
from_name, | |
from_id, | |
r['message'], | |
r['id']]) | |
total = total + len(data['data']) | |
if 'next' in data['paging']: | |
nextURL = data['paging']['next'] | |
else: | |
nextURL = '' | |
print("number of comments loaded: {}".format(total)) | |
print("total comments downloaded: {}".format(total)) | |
def loadLikes(): | |
path = "/v2.10/{}/likes".format(POST_ID) | |
url = "{host}{path}?{params}".format(host=host, path=path, params=params) | |
nextURL = url | |
fname = "output_facebook_likes_" + POST_ID + "_" + str(int(time.time())) + ".csv" | |
total = 0 | |
with io.open(fname,'w',encoding='utf8') as f: | |
rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
while nextURL != '': | |
#print("loading page: " + nextURL) | |
data = query(nextURL) | |
for r in data['data']: | |
rowWriter.writerow([ | |
r['name'], | |
r['id'] | |
]) | |
total = total + len(data['data']) | |
if 'next' in data['paging']: | |
nextURL = data['paging']['next'] | |
else: | |
nextURL = '' | |
print("number of likes loaded: {}".format(total)) | |
print("total likes downloaded: {}".format(total)) | |
# | |
# | |
loadLikes() | |
loadComments() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment