Skip to content

Instantly share code, notes, and snippets.

@norsez
Last active August 25, 2017 06:48
Show Gist options
  • Select an option

  • Save norsez/28dbf7e5df44e7d76599001c97ab1fa9 to your computer and use it in GitHub Desktop.

Select an option

Save norsez/28dbf7e5df44e7d76599001c97ab1fa9 to your computer and use it in GitHub Desktop.
Dumping a Facebook post's comments and likes into CSV files
# Loading post comments and likes by Norsez Orankijanan norsez@gmail.com
# based on example by James Thornton's script https://gist.github.com/jkuruzovich/b8485a368f80a3b88df46326cf54bbce
# 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
#HOW TO USE
#edit ACCESS_TOKEN to your current access token from the link above
#edit POST_ID to the post id you are interested in
#edit run this script
import io
import json
import urllib
import urllib.request
import pprint
import time
import csv
# get Facebook access token from environment variable
ACCESS_TOKEN = "EAACEdEose0cBAHgTFfnBKmy7WcMzXgm1TbxVLUDuOoE7pCIf4vzLxU1jZCk4o8znZC2orBZCBGCOQr3z7i1ost1b1lhOYlTeIZAZBr0PiC5x5V527qc1n05ojdsQcNhl7xzrYBj69KSqNi4iMJ53ofJ73EP1ufw8RrCmTKTZA8fg5JwjYp3L5pCtLUCtE4UPoZD"
POST_ID = "1056303127838075"
# 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" + 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" + 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