Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fbfeed2csv: a tool to download all posts from a user/group/page's facebook feed to a csv file
"""
fbfeed2csv: a tool to download all posts from a user/group/page's facebook feed to a csv file
yuzawa-san
https://github.com/yuzawa-san
"""
import json
import urllib2
import time
import csv
import re
import argparse
def loadPage(url):
# delay
time.sleep(1)
# download
response = urllib2.urlopen(url)
content = response.read()
payload = ''
print "DOWNLOAD!"
try:
payload = json.loads(content)
except:
print "JSON decoding failed!"
if 'data' in payload:
out = []
for post in payload['data']:
if 'message' in post:
# make timestamp pretty
timestamp = post['created_time']
timestamp = re.sub(r'\+\d+$', '', timestamp)
timestamp = timestamp.replace('T',' ')
out.append({
'author': post['from']['name'].encode('ascii', 'ignore'),
'timestamp': timestamp,
'message': post['message'].encode('ascii', 'ignore')})
out2 = []
if 'paging' in payload:
out2 = loadPage(payload['paging']['next'])
return out + out2
return []
# entry point:
# get args
parser = argparse.ArgumentParser()
parser.add_argument('id', help='ID of Graph API resource')
parser.add_argument('-o', '--out', default="fbdump.csv", help='Output file')
parser.add_argument('-t', '--token', help='Authentication token')
args = parser.parse_args()
try:
out = loadPage("https://graph.facebook.com/%s/feed?fields=from,message,created_time&limit=1000&access_token=%s" % (args.id, args.token))
# write output to file
f = open(args.out,'wb')
w = csv.DictWriter(f,['author','timestamp','message'])
w.writerows(out)
f.close()
except urllib2.HTTPError as e:
print "Download failed:",e
error_message = e.read()
print error_message
except KeyboardInterrupt:
print "Canceled!"
@RyanMorgan

This comment has been minimized.

Copy link

RyanMorgan commented Aug 9, 2014

this was really helpful, thanks!

@pedrocam

This comment has been minimized.

Copy link

pedrocam commented Sep 17, 2015

Hi, Thanks for submitting this.
Where do I input the url to the page I want to download? Should I just replace 'url' with the url? I tried that but it didn't seem to work.
Thanks again!

@beefree1

This comment has been minimized.

Copy link

beefree1 commented Sep 25, 2015

Hi.. can someone do a screen video tutorial on how to do this... if I can do this... save my life as need to get allot of info from my facebook group... to make a faq ebook ... thx in advance

@AlfKatz2

This comment has been minimized.

Copy link

AlfKatz2 commented Sep 26, 2015

Has anyone got this to work? I go to command prompt and type in the path to the python interpreter followed by the path and name of the python script fbfeedcsv.py and I get a message
File "C\data\fbfeedcsv.py" line 23
Print "Download!"
Syntax error: Missing parentheses in call to 'print'

So is there an error in the script? Since nobody has responded to previous requests for help I'm not expecting too much but if someone could give some advice it would be much appreciated. Maybe if I were a Python expert it would be perfectly clear but I'm not and looking at the manuals it would be a daunting prospect to try and learn enough to figure it out.

Thanks

Alf

@vsaraph

This comment has been minimized.

Copy link

vsaraph commented Oct 9, 2015

@AlfKatz2
Without having looked at the actual code, that seems like a Python 3 error. The above code was written for Python 2 whereas you were probably trying to run it using a Python 3 interpreter. There are differences between print in 2 and print in 3.

@apkfx

This comment has been minimized.

Copy link

apkfx commented Mar 9, 2017

Thanks, but how to separate one post to others using the line?

@Wollo1980

This comment has been minimized.

Copy link

Wollo1980 commented Mar 12, 2017

i am looking for a code to make an moderator off an abounded Facebook group with no Admin. We can't post anything. The other moderators are not active at all.

@Filip-Rogalski

This comment has been minimized.

Copy link

Filip-Rogalski commented May 21, 2018

Could you give more detailed information about how to use it?

@felbalart

This comment has been minimized.

Copy link

felbalart commented Jun 2, 2018

Am I wrong, with the April 4th 2018 changes on graph API , this ain't working anymore? As groups API is blocked, at least without a "Reviewed App"

@lenhhoxung86

This comment has been minimized.

Copy link

lenhhoxung86 commented Nov 20, 2018

This should not work anymore because every apps need to be reviewed before being able to retrieve posts, which is very annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.