-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
281 lines (212 loc) · 8.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import logging
import os
import sys
from getpass import getpass
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, SIGNAL, QObject, pyqtSignal, pyqtSlot, pyqtProperty, QTimer
from PyQt4.QtWebKit import QWebPage, QWebView
from time import sleep
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
class Proxy(QObject):
onLogin = pyqtSignal(bool)
onEnterBlocked = pyqtSignal(bool)
onUnban = pyqtSignal(bool)
onUnbanConfirm = pyqtSignal(bool)
@pyqtSlot(str, bool)
def trigger(self, name, success):
self._active = False
getattr(self, name).emit(success)
_active = False
_expect_success = {}
_expect_failed = {}
_wait_reload = True
def _get_active(self):
return self._active
def set_active(self, value):
self._active = value
active = pyqtProperty(bool, fget=_get_active)
def _get_expect_success(self):
return self._expect_success
def set_expect_success(self, value):
self._expect_success = value
self._active = True
expect_success = pyqtProperty('QVariantMap', fget=_get_expect_success)
def _get_expect_failed(self):
return self._expect_failed
def set_expect_failed(self, value):
self._expect_failed = value
self._active = True
expect_failed = pyqtProperty('QVariantMap', fget=_get_expect_failed)
def _get_wait_reload(self):
return self._wait_reload
def set_wait_reload(self, value):
self._wait_reload = value
wait_reload = pyqtProperty(bool, fget=_get_wait_reload)
def _get_username(self):
return os.environ['FACEBOOK_USERNAME']
username = pyqtProperty(str, fget=_get_username)
def _get_password(self):
return os.environ['FACEBOOK_PASSWORD']
password = pyqtProperty(str, fget=_get_password)
@pyqtSlot(str)
def info(self, text):
log.info(text)
@pyqtSlot(str)
def debug(self, text):
log.debug(text)
@pyqtSlot(str)
def error(self, text):
log.error(text)
@pyqtSlot(str)
def warn(self, text):
log.warn(text)
class WebPage(QWebPage):
def _javaScriptConsoleMessage(self, message, lineNumber, sourceID):
sys.stderr.write('Javascript error at line number %d\n' % lineNumber)
sys.stderr.write('%s\n' % message)
sys.stderr.write('Source ID: %s\n' % sourceID)
class FacebookUnban(QApplication):
def __init__(self, argv, show_window=True):
super().__init__(argv)
self.web_page = WebPage()
st = self.web_page.settings()
st.setAttribute(st.AutoLoadImages, False)
self.web_view = QWebView()
self.web_view.setPage(self.web_page)
self.web_view.loadFinished.connect(self._on_load_finished)
if show_window:
self.web_view.show()
#self.connect(self.web_view, SIGNAL('loadFinished(bool)'),
# self._on_load_finished)
self.load_timer = QTimer()
self.load_timer.timeout.connect(self._on_load_timeout)
self.proxy = Proxy()
self.proxy.onLogin.connect(self._do_login)
self.proxy.onEnterBlocked.connect(self._do_enter_blocked)
self.proxy.onUnban.connect(self._do_unban)
self.proxy.onUnbanConfirm.connect(self._do_unban_confirm)
self.proxy.set_expect_success({
'path': '/',
'selectorExists': 'form#login_form',
'trigger': 'onLogin'})
self._start_load_timer()
def _start_load_timer(self):
self.load_timer.start(300000)
def _on_load_timeout(self):
log.warn('TIMEOUT')
self.web_page.triggerAction(QWebPage.Stop)
def _on_load_finished(self, ok):
log.info('DOM Content Loaded')
self.proxy.set_wait_reload(False)
self.frame = self.web_page.currentFrame()
self.frame.addToJavaScriptWindowObject('bot', self.proxy)
self.frame.evaluateJavaScript("""
(function()
{
function activate_expectation(expect, isSuccess) {
if (expect.trigger)
{
bot.trigger(expect.trigger, isSuccess);
}
}
function process_expectation(expect, isSuccess) {
if (expect.path)
{
bot.debug(expect.trigger);
bot.debug('"' + expect.path + '" :: "' + document.location.pathname + '"');
}
if (expect.path.match(document.location.pathname))
{
if (expect.selectorNotExists && document.querySelector(expect.selectorNotExists))
{
bot.debug('selectorNotExists: ' + expect.selectorNotExists);
return;
}
if (expect.selectorExists && !document.querySelector(expect.selectorExists))
{
bot.debug('selectorExists: ' + expect.selectorExists);
return;
}
bot.info('expectation achieved');
activate_expectation(expect, isSuccess);
}
}
setInterval(function()
{
if (!bot.active || bot.wait_reload) { return; }
bot.debug('current path: ' + document.location.pathname);
process_expectation(bot.expect_success, true);
process_expectation(bot.expect_failed, false);
}, 3000);
window.fnClickElement = function(el)
{
if (el.click)
{
el.click();
}
else
{
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
};
}());
""")
def start(self):
self.web_page.currentFrame().load(QUrl(self.home_url))
def _do_login(self, bool):
self.proxy.set_wait_reload(True)
self.frame.evaluateJavaScript("""
document.forms.login_form.querySelector('[name="email"]').value = bot.username;
document.forms.login_form.querySelector('[name="pass"]').value = bot.password;
document.forms.login_form.querySelector('input[type="submit"]').click();
""")
self._start_load_timer()
self.proxy.set_expect_success({
'path': '/',
'selectorExists': 'div[data-click="profile_icon"]',
'trigger': 'onEnterBlocked'})
def _do_enter_blocked(self, bool):
self.proxy.set_wait_reload(True)
self.frame.evaluateJavaScript('document.location.href="' + self.home_url + self.forum_path + '/blocked/"')
self._start_load_timer()
self.proxy.set_expect_success({
'path': self.forum_path + '/blocked/?',
'selectorExists': '#pagelet_group_blocked div[id^="member_"] .adminActions > a[ajaxify*="action=remove_block"]',
'trigger': 'onUnban'
})
def _do_unban(self, bool):
sleep(3)
self.frame.evaluateJavaScript("""
var el = document.querySelector('#pagelet_group_blocked div[id^="member_"] .adminActions > a[ajaxify*="action=remove_block"]');
fnClickElement(el);
""")
self.proxy.set_expect_success({
'path': self.forum_path + '/blocked/?',
'selectorExists': 'button[name="remove_block"]',
'trigger': 'onUnbanConfirm',
})
def _do_unban_confirm(self, bool):
self.proxy.set_wait_reload(True)
self.frame.evaluateJavaScript("""
document.querySelector('button[name="remove_block"]').click();
""")
self._start_load_timer()
self.proxy.set_expect_success({
'path': self.forum_path + '/blocked/?',
'selectorExists': '#pagelet_group_blocked div[id^="member_"] .adminActions > a[ajaxify*="action=remove_block"]',
'trigger': 'onUnban',
})
if __name__ == '__main__':
if not 'FACEBOOK_USERNAME' in os.environ:
os.environ['FACEBOOK_USERNAME'] = input('Enter Facebook username (email): ')
if not 'FACEBOOK_PASSWORD' in os.environ:
os.environ['FACEBOOK_PASSWORD'] = getpass('Enter Facebook password: ')
forum_url_name = input('Enter Facebook forum name (e.g just sencha.indo.admin from /groups/sencha.indo.admin): ')
bot = FacebookUnban(sys.argv)
bot.home_url = 'https://www.facebook.com'
bot.forum_path = '/groups/' + forum_url_name
bot.start()
sys.exit(bot.exec_())