Parcourir la source

initial commit

Martin il y a 10 ans
Parent
commit
9f492171ae
2 fichiers modifiés avec 251 ajouts et 0 suppressions
  1. 209 0
      main.py
  2. 42 0
      util.py

+ 209 - 0
main.py

@@ -0,0 +1,209 @@
+import os
+import collections
+
+import splinter
+import selenium.common.exceptions
+
+import util
+
+_USER_EMAIL = os.environ['FB_USER']
+_USER_PASS = os.environ['FB_PASS']
+
+def remove_items(b, value1, value2, confirm_button=None):
+    removed = 0
+
+    while True:
+        b.b.reload()
+        
+        element_ids = None
+        for _ in range(60):
+            util.sleep(2, 0)
+            element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+            if element_ids:
+                break
+            else:
+                b.scroll_y(200)
+
+        if not element_ids:
+            print 'All done.'
+            return
+
+        util.sleep(2, 0)
+        element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+
+        done = set()
+        while element_ids:
+            eid = element_ids[0]
+            element_ids = None
+            e = b.b.find_by_id(eid)
+            done.add(eid)
+
+            if e:
+                try:
+                    e.mouse_over()
+                    util.sleep(0, 0.2)
+                    e.click()
+                    
+                    unlike_ids = util.filter_by_value(b.b.find_by_tag('a'), value2)
+                    if len(unlike_ids) != 1:
+                        raise Exception('Could not find a single "%s" button (found %i).' % (value2, len(unlike_ids)))
+                    
+                    e = unlike_ids[0]
+                    util.sleep(0, 0.2)
+                    e.mouse_over()
+                    util.sleep(0, 0.1)
+                    e.click()
+                    
+                    if confirm_button:
+                        util.sleep(1, 1)
+
+                        buttons = b.b.find_by_tag('button')
+                        buttons = [button for button in buttons if button.has_class('layerConfirm')]
+
+                        if len(buttons) != 1:
+                            raise Exception('Could not find a single confirm button (found %i).' % len(button))
+                        
+                        buttons[0].mouse_over()
+                        util.sleep(0.1, 0.2)
+                        buttons[0].click()
+                        
+                        while b.b.is_text_present(confirm_button):
+                            print 'Waiting for confirm button to disappear...'
+
+                            util.sleep(0.5, 0)
+                    
+                    removed += 1
+                    print 'removed %i items.' % removed
+
+                    util.sleep(4, 1)
+
+                    element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+                    element_ids = [x for x in element_ids if x not in done]
+
+                except selenium.common.exceptions.StaleElementReferenceException, e:
+                    print 'StaleElementReferenceException; reloading elements.'
+                    element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+                    element_ids = [x for x in element_ids if x not in done]
+                except selenium.common.exceptions.ElementNotVisibleException, e:
+                    print 'ElementNotVisibleException; scrolling, reloading elements.'
+                    b.scroll_y(500)
+                    util.sleep(1, 0)
+                    element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+                    element_ids = [x for x in element_ids if x not in done]
+                except selenium.common.exceptions.WebDriverException, e:
+                    if 'Element is not clickable' in str(e):
+                        print 'Not clickable: %s' % e
+#                        b.scroll_y(300)
+                        util.sleep(1, 0)
+                        element_ids = util.get_tag_ids(b.b.html, 'data-tooltip-content="%s"' % value1)
+                        element_ids = [x for x in element_ids if x not in done]
+                    
+                    else:
+                        print 'Unknown WebDriverException: ' + str(e)
+                        print 'Exiting.'
+                        return
+                        
+                except Exception, e:
+                    print str(e)
+
+
+class FacebookBrowser(object):
+    b = None
+    stats = collections.Counter()
+    
+    def __init__(self, driver='firefox'):
+        self.b = splinter.Browser(driver)
+        self.b.visit('https://facebook.com/login')
+
+    def remove_your_posts(self):
+        es = util.filter_by_value(self.b.find_by_tag('a'), 'Your Posts')
+        
+        if len(es) != 1:
+            print 'Could not find a single "Your Posts" button (found %i)' % len(es)
+            return
+        
+        es[0].mouse_over()
+        util.sleep(0, 0.2)
+        es[0].click()
+        
+        self._remove_items(self.b, 'Allowed on Timeline', 'Delete', 'Delete Post')
+
+    def remove_likes(self):
+        es = util.filter_by_value(self.b.find_by_tag('a'), 'Likes')
+        
+        if len(es) != 1:
+            print 'Could not find a single "Likes" button (found %i)' % len(es)
+            return
+        
+        es[0].mouse_over()
+        util.sleep(0, 0.2)
+        es[0].click()
+        
+        self._remove_items('Edit', 'Unlike')
+
+    def remove_comments(self):
+        es = util.filter_by_value(self.b.find_by_tag('a'), 'Comments')
+        
+        if len(es) != 1:
+            print 'Could not find a single "Comments" button (found %i)' % len(es)
+            return
+        
+        es[0].mouse_over()
+        util.sleep(0, 0.2)
+        es[0].click()
+        
+        self._remove_items('Edit', 'Delete')
+
+    def _remove_items(self, value1, value2, confirm_button=None):
+        return remove_items(self, value1, value2, confirm_button=None)
+
+    def scroll_y(self, pixels):
+        self.b.execute_script('window.scrollBy(0,%i);' % pixels)
+
+    def scroll_to(self, e_id):
+        self.b.execute_script('document.getElementById("%s").scrollIntoView(true); window.scrollBy(0,300);' % e_id)
+
+    def activity_log(self):
+        e = self.b.find_by_id('pageLoginAnchor')[0]
+        
+        util.sleep(0.5, 0.5)
+        e.mouse_over()
+        util.sleep(0, 0.2)
+        e.click()
+
+        util.sleep(0.5, 0.5)
+        e = self.b.find_by_text('Activity Log')[-1]
+        e.mouse_over()
+        util.sleep(0, 0.2)
+        e.click()
+
+    def login(self):
+        e = self.b.find_by_id('email')[0]
+        
+        util.sleep(1, 1)
+        e.mouse_over()
+        
+        util.sleep(0, 0.2)
+        e.click()
+        
+        util.sleep(1, 1)
+        util.type(e, _USER_EMAIL)
+        
+        e = self.b.find_by_id('pass')[0]
+        
+        util.sleep(0.5, 0.5)
+        e.mouse_over()
+        
+        util.sleep(0, 0.2)
+        e.click()
+        
+        util.sleep(1, 1)
+        util.type(e, _USER_PASS, delay=0.2)
+        
+        e = self.b.find_by_id('loginbutton')[0]
+        
+        util.sleep(1,1)
+        e.mouse_over()
+        
+        util.sleep(0, 0.2)
+        e.click()

+ 42 - 0
util.py

@@ -0,0 +1,42 @@
+import time
+import random
+
+def type(e, text, delay=0.1):
+    for c in text:
+        e.type(c)
+        sleep(0, delay) 
+
+def filter_by_value(elements, value):
+    return [e for e in elements if e.value.strip() == value]
+
+def sleep(min_delay, random_delay):
+    time.sleep(min_delay + random_delay * random.random())
+
+def get_tag_ids(html, tag):
+
+    start_idx = 0
+    res = []
+    while html.find(tag, start_idx) >= 0:
+        idx = html.find(tag, start_idx)
+        
+        s = idx
+        while html[s] != '<':
+            s -= 1
+        e = idx
+        while html[e] != '>':
+            e += 1
+
+        id_start_idx = html.find('id="', s) + 4
+        id_end_idx = html.find('"', id_start_idx)
+        
+        if id_start_idx < s or id_start_idx > e or id_end_idx < s or id_end_idx > e:
+            print 'could not find id in %s' % html[s:e]
+            start_idx = idx + 1
+            continue
+            
+
+        res.append(html[id_start_idx:id_end_idx])
+        start_idx = idx + 1
+
+    return res
+