#!/usr/bin/env python # -*- coding: utf-8 -*- # # Block all facebook app!!!! \o/ # # Copyright (C) 2012 Andrea Milazzo aka mancausoft # # This Program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. import cookielib import os import urllib import urllib2 from threading import Thread import re from BeautifulSoup import BeautifulSoup from BeautifulSoup import Comment fb_username = username fb_password = password app_urls = ["https://www.facebook.com/appcenter/category/actiongames", "https://www.facebook.com/appcenter/category/entertainment/", "https://www.facebook.com/appcenter/category/adventuregames", "https://www.facebook.com/appcenter/category/boardgames", "https://www.facebook.com/appcenter/category/familygames", "https://www.facebook.com/appcenter/category/photovideo", "https://www.facebook.com/appcenter/category/sports", "https://www.facebook.com/appcenter/category/travel", "https://www.facebook.com/appcenter/category/utilities/", ] cookie_filename = "facebook.cookies" class CleanFB(object): def __init__(self, login, password): """ Start up... """ self.login = login self.password = password self.cj = cookielib.MozillaCookieJar(cookie_filename) if os.access(cookie_filename, os.F_OK): self.cj.load() self.opener = urllib2.build_opener( urllib2.HTTPRedirectHandler(), urllib2.HTTPHandler(debuglevel=0), urllib2.HTTPSHandler(debuglevel=0), urllib2.HTTPCookieProcessor(self.cj) ) self.opener.addheaders = [ ('User-agent', 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'), ] # need this twice - once to set cookies, once to log in... self.login_to_facebook() self.login_to_facebook() def login_to_facebook(self): """ Handle login. This should populate our cookie jar. """ login_data = urllib.urlencode({ 'email' : self.login, 'pass' : self.password, }) response = self.opener.open("https://www.facebook.com/login.php", login_data) return ''.join(response.readlines()) def get_app(self, app_url): ret = list() ret2 = list() #response = self.opener.open("https://www.facebook.com/appcenter/") response = self.opener.open(app_url) #return ''.join(response.readlines()) content = response.read() soup = BeautifulSoup(content) comments = ''.join(soup.findAll(text=lambda text:isinstance(text, Comment))) soup = BeautifulSoup(comments) parse = soup.findAll('li' ) for li in parse: try: data = li['data-gt'] ret.append(re.search('''"appid":"([0-9]*)"''', data).group(1)) except: pass parse = soup.findAll('span', {'class':"fwb"} ) #print parse for fwb in parse: a = fwb.findAll('a') if a: a = a[0] ret2.append(a['href']) return (ret, ret2) def delete_app(self, app, url): response = self.opener.open(url) content = response.read() response = self.opener.open("https://www.facebook.com/ajax/apps/block_app.php?app_id=" + app +"&type_index=0&source=about&confirm_id=block_app_link&causal_element=js_0&__asyncDialog=1&__user=100002698406562&__a=1") content = response.read() remove_data = urllib.urlencode({ 'confirm':'1', 'ok':'OK', '__d':'1', '__user':'100002698406562', '__a':'1', 'fb_dtsg':'AQCj0o3s', 'phstamp': '1658167106481115111567' }) print remove_data response = self.opener.open("https://www.facebook.com/ajax/apps/block_app.php?app_id="+app+"&type_index=0&action=block&source=about&confirm_id=block_app_link",remove_data) content = response.read() print content print "" print "===================================" print "" def clean(url): apps, urls = test.get_app(url) for i in xrange(len(apps)): test.delete_app(apps[i], urls[i]) test = CleanFB(fb_username, fb_password) for i in xrange(15): threads = list() for url in app_urls: th = Thread(target=clean, args=(url,)) th.start() threads.append(th) for th in threads: th.join() test.cj.save()