Last active
August 29, 2015 14:14
-
-
Save shshank/ac9d34b3204ccc7293bb to your computer and use it in GitHub Desktop.
Clean your Facebook page by deleting comments that contain certain phrases.
This file contains 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
import json | |
import requests | |
BAD_PHRASES = ['kutta'] | |
PAGE_NAME = '' | |
access_token = "" #need manage_pages and publish_action permision | |
burl = "https://graph.facebook.com/v2.2/" | |
access_str = "access_token=%s"%(access_token) | |
def get_posts(url=None): | |
if not url: | |
url = burl+ PAGE_NAME + "?fields=posts{status_type}&limit=20&" + access_str | |
print url | |
resp = requests.get(url) | |
body = json.loads(resp.content) | |
data = [] | |
next_page = None | |
print body | |
if body.get('error'): | |
raise Exception(body['error']['message']) | |
if body.get('posts'): | |
data = body['posts']['data'] | |
next_page = body['posts']['paging'].get('next') | |
ids = [] | |
for item in data: | |
if item.get('status_type'): | |
ids.append(item['id']) | |
print '.', | |
return ids, next_page | |
def get_all_posts(count=10): | |
ids, next_page = get_posts() | |
print "Getting Posts" | |
while next_page and len(ids)<count: | |
more_ids, next_page = get_posts(next_page) | |
ids.extend(more_ids) | |
print len(ids), "Posts found, Fetching more..." | |
print "No more to fetch." | |
return ids | |
def get_comments(post_id, after=''): | |
url = burl+ post_id+'/comments?limit=20&after='+after+ '&' + access_str | |
resp = requests.get(url) | |
body = json.loads(resp.content) | |
data = body['data'] | |
if data: | |
after = body['paging'].get('after', '') | |
print '.', | |
return data, after | |
def get_all_comments(post_id, count=100): | |
print "Fetching comments for post_id", post_id | |
all_comments, after = get_comments(post_id) | |
while after and len(comments)<count: | |
more_comments, after = get_comments(post_id, after=after) | |
all_comments.extend(more_comments) | |
print len(all_comments), "Comments found." | |
return all_comments | |
def is_bad_comment(comment, bad_phrases=BAD_PHRASES): | |
message = comment['message'].lower() | |
for phrase in BAD_PHRASES: | |
if phrase.lower() in message: | |
return True | |
return False | |
def delete_comment(comment_id): | |
url = burl + comment_id + '?' + access_str | |
resp = requests.delete(url) | |
body = json.loads(resp.content) | |
if body.get('error'): | |
raise Exception(body['error']['message']) | |
return body.get('success') | |
if __name__ == '__main__': | |
posts = get_all_posts(100) | |
for p in posts: | |
comments = get_all_comments(p, 5000) | |
comments_to_delete = filter(lambda c: is_bad_comment(c), comments) | |
map(lambda c:delete_comment(c['id']), comments_to_delete) | |
print len(comments_to_delete), "Comments deleted." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment