[3] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | # |
---|
| 4 | # Block all facebook app!!!! \o/ |
---|
| 5 | # |
---|
| 6 | # Copyright (C) 2012 Andrea Milazzo aka mancausoft <me@mancausoft.org> |
---|
| 7 | # |
---|
| 8 | # This Program is free software. It comes without any warranty, to |
---|
| 9 | # the extent permitted by applicable law. You can redistribute it |
---|
| 10 | # and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 11 | # To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 12 | # http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 13 | |
---|
| 14 | import cookielib |
---|
| 15 | import os |
---|
| 16 | import urllib |
---|
| 17 | import urllib2 |
---|
| 18 | from threading import Thread |
---|
| 19 | |
---|
| 20 | import re |
---|
| 21 | |
---|
| 22 | from BeautifulSoup import BeautifulSoup |
---|
| 23 | from BeautifulSoup import Comment |
---|
| 24 | |
---|
| 25 | fb_username = username |
---|
| 26 | fb_password = password |
---|
| 27 | |
---|
| 28 | app_urls = ["https://www.facebook.com/appcenter/category/actiongames", |
---|
| 29 | "https://www.facebook.com/appcenter/category/entertainment/", |
---|
| 30 | "https://www.facebook.com/appcenter/category/adventuregames", |
---|
| 31 | "https://www.facebook.com/appcenter/category/boardgames", |
---|
| 32 | "https://www.facebook.com/appcenter/category/familygames", |
---|
| 33 | "https://www.facebook.com/appcenter/category/photovideo", |
---|
| 34 | "https://www.facebook.com/appcenter/category/sports", |
---|
| 35 | "https://www.facebook.com/appcenter/category/travel", |
---|
| 36 | "https://www.facebook.com/appcenter/category/utilities/", |
---|
| 37 | ] |
---|
| 38 | |
---|
| 39 | cookie_filename = "facebook.cookies" |
---|
| 40 | |
---|
| 41 | class CleanFB(object): |
---|
| 42 | |
---|
| 43 | def __init__(self, login, password): |
---|
| 44 | """ Start up... """ |
---|
| 45 | self.login = login |
---|
| 46 | self.password = password |
---|
| 47 | |
---|
| 48 | self.cj = cookielib.MozillaCookieJar(cookie_filename) |
---|
| 49 | if os.access(cookie_filename, os.F_OK): |
---|
| 50 | self.cj.load() |
---|
| 51 | self.opener = urllib2.build_opener( |
---|
| 52 | urllib2.HTTPRedirectHandler(), |
---|
| 53 | urllib2.HTTPHandler(debuglevel=0), |
---|
| 54 | urllib2.HTTPSHandler(debuglevel=0), |
---|
| 55 | urllib2.HTTPCookieProcessor(self.cj) |
---|
| 56 | ) |
---|
| 57 | self.opener.addheaders = [ |
---|
| 58 | ('User-agent', 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'), |
---|
| 59 | ] |
---|
| 60 | |
---|
| 61 | # need this twice - once to set cookies, once to log in... |
---|
| 62 | self.login_to_facebook() |
---|
| 63 | self.login_to_facebook() |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def login_to_facebook(self): |
---|
| 67 | """ |
---|
| 68 | Handle login. This should populate our cookie jar. |
---|
| 69 | """ |
---|
| 70 | login_data = urllib.urlencode({ |
---|
| 71 | 'email' : self.login, |
---|
| 72 | 'pass' : self.password, |
---|
| 73 | }) |
---|
| 74 | response = self.opener.open("https://www.facebook.com/login.php", login_data) |
---|
| 75 | return ''.join(response.readlines()) |
---|
| 76 | |
---|
| 77 | def get_app(self, app_url): |
---|
| 78 | ret = list() |
---|
| 79 | ret2 = list() |
---|
| 80 | #response = self.opener.open("https://www.facebook.com/appcenter/") |
---|
| 81 | response = self.opener.open(app_url) |
---|
| 82 | |
---|
| 83 | #return ''.join(response.readlines()) |
---|
| 84 | content = response.read() |
---|
| 85 | |
---|
| 86 | soup = BeautifulSoup(content) |
---|
| 87 | comments = ''.join(soup.findAll(text=lambda text:isinstance(text, Comment))) |
---|
| 88 | soup = BeautifulSoup(comments) |
---|
| 89 | parse = soup.findAll('li' ) |
---|
| 90 | for li in parse: |
---|
| 91 | try: |
---|
| 92 | data = li['data-gt'] |
---|
| 93 | ret.append(re.search('''"appid":"([0-9]*)"''', data).group(1)) |
---|
| 94 | except: |
---|
| 95 | pass |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | parse = soup.findAll('span', {'class':"fwb"} ) |
---|
| 100 | #print parse |
---|
| 101 | for fwb in parse: |
---|
| 102 | a = fwb.findAll('a') |
---|
| 103 | if a: |
---|
| 104 | a = a[0] |
---|
| 105 | ret2.append(a['href']) |
---|
| 106 | |
---|
| 107 | return (ret, ret2) |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | def delete_app(self, app, url): |
---|
| 112 | response = self.opener.open(url) |
---|
| 113 | content = response.read() |
---|
| 114 | 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") |
---|
| 115 | |
---|
| 116 | content = response.read() |
---|
| 117 | |
---|
| 118 | remove_data = urllib.urlencode({ |
---|
| 119 | 'confirm':'1', |
---|
| 120 | 'ok':'OK', |
---|
| 121 | '__d':'1', |
---|
| 122 | '__user':'100002698406562', |
---|
| 123 | '__a':'1', |
---|
| 124 | 'fb_dtsg':'AQCj0o3s', |
---|
| 125 | 'phstamp': '1658167106481115111567' |
---|
| 126 | }) |
---|
| 127 | print remove_data |
---|
| 128 | 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) |
---|
| 129 | content = response.read() |
---|
| 130 | print content |
---|
| 131 | |
---|
| 132 | print "" |
---|
| 133 | print "===================================" |
---|
| 134 | print "" |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | def clean(url): |
---|
| 138 | apps, urls = test.get_app(url) |
---|
| 139 | for i in xrange(len(apps)): |
---|
| 140 | test.delete_app(apps[i], urls[i]) |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | test = CleanFB(fb_username, fb_password) |
---|
| 144 | |
---|
| 145 | for i in xrange(15): |
---|
| 146 | threads = list() |
---|
| 147 | for url in app_urls: |
---|
| 148 | th = Thread(target=clean, args=(url,)) |
---|
| 149 | th.start() |
---|
| 150 | threads.append(th) |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | for th in threads: |
---|
| 154 | th.join() |
---|
| 155 | |
---|
| 156 | test.cj.save() |
---|