| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import collections
- import splinter
- import selenium.common.exceptions
- import util
- import config
- def remove_items(client, initial_values, secondary_values, confirm_button=None):
- if not isinstance(initial_values, list):
- initial_values = [initial_values]
- if not isinstance(secondary_values, list):
- secondary_values = [secondary_values]
- removed = 0
- while True:
- client.b.reload()
-
- element_ids = None
- for _ in range(60):
- util.sleep(2, 0)
- element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- if element_ids:
- break
- else:
- client.scroll_y(200)
- if not element_ids:
- print 'All done.'
- return
- util.sleep(2, 0)
- element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- done = set()
- while element_ids:
- eid = element_ids[0]
- element_ids = None
- e = client.b.find_by_id(eid)
- done.add(eid)
- if e:
- try:
- e.mouse_over()
- util.sleep(0, 0.2)
- e.click()
- util.sleep(0.1, 0.1)
-
- unlike_ids = util.filter_by_value(client.b.find_by_tag('a'), secondary_values)
- if len(unlike_ids) != 1:
- raise Exception('Could not find a single %s button (found %i).' % (secondary_values, 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 = client.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 client.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(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- 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(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- element_ids = [x for x in element_ids if x not in done]
- except selenium.common.exceptions.ElementNotVisibleException, e:
- print 'ElementNotVisibleException; scrolling, reloading elements.'
- client.scroll_y(500)
- util.sleep(0.3, 0)
- element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- 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(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
- 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):
- stats = collections.Counter()
-
- def __init__(self):
- self.b = splinter.Browser('chrome', executable_path=config.CHROME_PATH)
- 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()
-
- remove_items(self, 'Allowed on Timeline', 'Delete', 'Delete Post')
- def remove_likes(self):
- es = util.filter_by_value(self.b.find_by_tag('a'), 'Likes and Reactions')
-
- 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()
-
- remove_items(self, 'Edit', ['Unlike', 'Remove Reaction'])
- 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()
-
- remove_items(self, 'Edit', 'Delete')
- 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, config.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, config.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()
|