Skip to content

Instantly share code, notes, and snippets.

@rob0rt
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob0rt/3fa0b55999df7280eede to your computer and use it in GitHub Desktop.
Save rob0rt/3fa0b55999df7280eede to your computer and use it in GitHub Desktop.
Delete all members of a facebook group using Selenium
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from facepy import GraphAPI
import contextlib
import time
import threading
usr = 'FB_USER'
pwd = 'FB_PASS'
group = 'FB_GROUP'
access_token = 'FB_ACCESS_TOKEN'
class deleteMembers(threading.Thread):
def __init__ (self, members):
self._members = members
threading.Thread.__init__(self)
def run(self):
print "=== STARTING DELETE ==="
sleep_time = 1
with contextlib.closing(webdriver.Firefox()) as driver:
# or you can use Firefox()
# or you can use Chrome(executable_path="/usr/bin/chromedriver")
print "=== Opening facebook ==="
driver.get("http://www.facebook.org")
assert "Facebook" in driver.title
print "=== Logging in ==="
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
time.sleep(sleep_time)
for member in self._members['data']:
if member['administrator']:
continue
driver.get("https://www.facebook.com/groups/" + group + "/members/?order=default&member_query=" + member['name'])
# Get the user's name and open the menu
elem = driver.find_elements_by_xpath("//a[contains(@class,'_42ft _4jy0 _55pi _2agf _p _4jy3 _517h _59pe')]")
if len(elem) == 0:
continue
elem[0].click()
time.sleep(sleep_time)
# Select the delete user menu item
menu_opts = driver.find_elements_by_xpath("//a[contains(@class, '_54nc')]")
menu_opts[1].click()
time.sleep(sleep_time)
# Confirm the user deletion
confirm = driver.find_elements_by_xpath("//button[contains(@class, '_42ft _42fu layerConfirm uiOverlayButton selected _42g- _42gy')]")
confirm[0].click()
time.sleep(sleep_time)
print "=== PREPARING DELETION GROUPS ==="
print "(this may take a bit)"
threads = []
graph = GraphAPI(access_token)
members = graph.get(group + '/members', limit=500)
offset = 0
while len(members['data']) > 0:
thread = deleteMembers(members)
threads.append(thread)
offset += 500
members = graph.get(group + '/members', limit=500, offset=offset)
print "=== STARTING THREADS ==="
max_threads = 10
running_threads = []
for x in range(0, max_threads):
if len(threads) > 0:
new_thread = threads.pop()
running_threads.append(new_thread)
new_thread.start()
while True:
for i,t in enumerate(running_threads):
if not t.isAlive():
del running_threads[i]
if len(threads) > 0:
new_thread = threads.pop()
running_threads.append(new_thread)
new_thread.start()
if len(running_threads) == 0:
break
@rob0rt
Copy link
Author

rob0rt commented Sep 3, 2014

From some reports of usage, it appears as if, on average, each thread deletes 9.75 people per minute. The number of total threads is controlled by max_threads, and tends to work best when max_threads = # of cores. Since all the users to be deleted are downloaded at the beginning in chucks of 500, each thread should have a separate and unique set of people to remove, allowing for high levels of parallelizability.

@sheikgc
Copy link

sheikgc commented Feb 1, 2015

do you know how can i unblock a member of my group? its very hard to scroll down and find it manually, because the search tool doesnt work :/

@rob0rt
Copy link
Author

rob0rt commented Feb 22, 2015

If you go to the members tab, in the sort options dropdown there should be a "blocked" option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment