Skip to content

Instantly share code, notes, and snippets.

@cyberplant
Created April 16, 2014 08:20
Show Gist options
  • Save cyberplant/10831249 to your computer and use it in GitHub Desktop.
Save cyberplant/10831249 to your computer and use it in GitHub Desktop.
This simple and quick scripts allows you to remove comments with a special text on it. In this case, I used it to remove all comments in photos with the text "the_real_sims" on them, they were created by a virus of a couple of friends.
# coding: utf-8
# To use this, you have to get a token from here: https://developers.facebook.com/tools/explorer/
# With publish_actions privileges (needed to be able to delete comments).
ACCESS_TOKEN = ""
# Main structure taken from:
# http://nbviewer.ipython.org/github/ptwobrussell/Mining-the-Social-Web-2nd-Edition/blob/master/ipynb/Chapter%202%20-%20Mining%20Facebook.ipynb
import requests
import json
import sys
base_url = 'https://graph.facebook.com/me'
fields = 'id,name,albums'
url = '%s?fields=%s&access_token=%s' % (base_url, fields, ACCESS_TOKEN,)
# Interpret the response as JSON and convert back
# to Python data structures
content = requests.get(url).json()
if not "albums" in content:
print content
sys.exit(1)
# Code taken from:
# http://code.activestate.com/recipes/576602-safe-print/
def my_print(*args):
"""Safely print the given string.
If you want to see the code points for unprintable characters then you
can use `errors="xmlcharrefreplace"`.
"""
errors = "replace"
for u in args:
s = u.encode(sys.stdout.encoding or "iso8859-1", errors)
print(s),
print ""
f = open("comments-deleted.txt","a")
albums = content["albums"]["data"]
for album in albums:
print "-"*35
print "Album:", album["name"]
url = 'https://graph.facebook.com/%s/photos?&access_token=%s' % (album["id"], ACCESS_TOKEN,)
resp = requests.get(url).json()
pictures = resp["data"]
for p in pictures:
if "comments" in p:
comments = p["comments"]
for comment in comments["data"]:
msg = comment["message"]
if "the_real_sims" in msg:
print "VIRUS DETECTED in picture", p["id"], "!"*40
print comment
f.write("%s\n" % comment)
delete_url = 'https://graph.facebook.com/%s?&access_token=%s' % (comment["id"], ACCESS_TOKEN,)
delete_result = requests.delete(delete_url).json()
if delete_result is True:
print "Comment deleted successfully."
else:
print "Error, unable to delete comment", delete_result
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment