main.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import collections
  2. import splinter
  3. import selenium.common.exceptions
  4. import util
  5. import config
  6. def remove_items(client, initial_values, secondary_values, confirm_button=None):
  7. if not isinstance(initial_values, list):
  8. initial_values = [initial_values]
  9. if not isinstance(secondary_values, list):
  10. secondary_values = [secondary_values]
  11. removed = 0
  12. while True:
  13. client.b.reload()
  14. element_ids = None
  15. for _ in range(60):
  16. util.sleep(2, 0)
  17. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  18. if element_ids:
  19. break
  20. else:
  21. client.scroll_y(200)
  22. if not element_ids:
  23. print 'All done.'
  24. return
  25. util.sleep(2, 0)
  26. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  27. done = set()
  28. while element_ids:
  29. eid = element_ids[0]
  30. element_ids = None
  31. e = client.b.find_by_id(eid)
  32. done.add(eid)
  33. if e:
  34. try:
  35. e.mouse_over()
  36. util.sleep(0, 0.2)
  37. e.click()
  38. util.sleep(0.1, 0.1)
  39. unlike_ids = util.filter_by_value(client.b.find_by_tag('a'), secondary_values)
  40. if len(unlike_ids) != 1:
  41. raise Exception('Could not find a single %s button (found %i).' % (secondary_values, len(unlike_ids)))
  42. e = unlike_ids[0]
  43. util.sleep(0, 0.2)
  44. e.mouse_over()
  45. util.sleep(0, 0.1)
  46. e.click()
  47. if confirm_button:
  48. util.sleep(1, 1)
  49. buttons = client.b.find_by_tag('button')
  50. buttons = [button for button in buttons if button.has_class('layerConfirm')]
  51. if len(buttons) != 1:
  52. raise Exception('Could not find a single confirm button (found %i).' % len(button))
  53. buttons[0].mouse_over()
  54. util.sleep(0.1, 0.2)
  55. buttons[0].click()
  56. while client.b.is_text_present(confirm_button):
  57. print 'Waiting for confirm button to disappear...'
  58. util.sleep(0.5, 0)
  59. removed += 1
  60. print 'removed %i items.' % removed
  61. util.sleep(4, 1)
  62. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  63. element_ids = [x for x in element_ids if x not in done]
  64. except selenium.common.exceptions.StaleElementReferenceException, e:
  65. print 'StaleElementReferenceException; reloading elements.'
  66. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  67. element_ids = [x for x in element_ids if x not in done]
  68. except selenium.common.exceptions.ElementNotVisibleException, e:
  69. print 'ElementNotVisibleException; scrolling, reloading elements.'
  70. client.scroll_y(500)
  71. util.sleep(0.3, 0)
  72. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  73. element_ids = [x for x in element_ids if x not in done]
  74. except selenium.common.exceptions.WebDriverException, e:
  75. if 'Element is not clickable' in str(e):
  76. print 'Not clickable: %s' % e
  77. # b.scroll_y(300)
  78. util.sleep(1, 0)
  79. element_ids = util.get_tag_ids(client.b.html, ['data-tooltip-content="%s"' % v for v in initial_values])
  80. element_ids = [x for x in element_ids if x not in done]
  81. else:
  82. print 'Unknown WebDriverException: ' + str(e)
  83. print 'Exiting.'
  84. return
  85. except Exception, e:
  86. print str(e)
  87. class FacebookBrowser(object):
  88. stats = collections.Counter()
  89. def __init__(self):
  90. self.b = splinter.Browser('chrome', executable_path=config.CHROME_PATH)
  91. self.b.visit('https://facebook.com/login')
  92. def remove_your_posts(self):
  93. es = util.filter_by_value(self.b.find_by_tag('a'), 'Your Posts')
  94. if len(es) != 1:
  95. print 'Could not find a single "Your Posts" button (found %i)' % len(es)
  96. return
  97. es[0].mouse_over()
  98. util.sleep(0, 0.2)
  99. es[0].click()
  100. remove_items(self, 'Allowed on Timeline', 'Delete', 'Delete Post')
  101. def remove_likes(self):
  102. es = util.filter_by_value(self.b.find_by_tag('a'), 'Likes and Reactions')
  103. if len(es) != 1:
  104. print 'Could not find a single "Likes" button (found %i)' % len(es)
  105. return
  106. es[0].mouse_over()
  107. util.sleep(0, 0.2)
  108. es[0].click()
  109. remove_items(self, 'Edit', ['Unlike', 'Remove Reaction'])
  110. def remove_comments(self):
  111. es = util.filter_by_value(self.b.find_by_tag('a'), 'Comments')
  112. if len(es) != 1:
  113. print 'Could not find a single "Comments" button (found %i)' % len(es)
  114. return
  115. es[0].mouse_over()
  116. util.sleep(0, 0.2)
  117. es[0].click()
  118. remove_items(self, 'Edit', 'Delete')
  119. def scroll_y(self, pixels):
  120. self.b.execute_script('window.scrollBy(0,%i);' % pixels)
  121. def scroll_to(self, e_id):
  122. self.b.execute_script('document.getElementById("%s").scrollIntoView(true); window.scrollBy(0,300);' % e_id)
  123. def activity_log(self):
  124. e = self.b.find_by_id('pageLoginAnchor')[0]
  125. util.sleep(0.5, 0.5)
  126. e.mouse_over()
  127. util.sleep(0, 0.2)
  128. e.click()
  129. util.sleep(0.5, 0.5)
  130. e = self.b.find_by_text('Activity Log')[-1]
  131. e.mouse_over()
  132. util.sleep(0, 0.2)
  133. e.click()
  134. def login(self):
  135. e = self.b.find_by_id('email')[0]
  136. util.sleep(1, 1)
  137. e.mouse_over()
  138. util.sleep(0, 0.2)
  139. e.click()
  140. util.sleep(1, 1)
  141. util.type(e, config.USER_EMAIL)
  142. e = self.b.find_by_id('pass')[0]
  143. util.sleep(0.5, 0.5)
  144. e.mouse_over()
  145. util.sleep(0, 0.2)
  146. e.click()
  147. util.sleep(1, 1)
  148. util.type(e, config.USER_PASS, delay=0.2)
  149. e = self.b.find_by_id('loginbutton')[0]
  150. util.sleep(1,1)
  151. e.mouse_over()
  152. util.sleep(0, 0.2)
  153. e.click()