Advertisement
Guest User

Facebook bot

a guest
Mar 1st, 2017
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.91 KB | None | 0 0
  1. from selenium import webdriver as driver # fakes human interaction
  2. from selenium.webdriver.common.keys import Keys as keys
  3. import time # waiting
  4.  
  5. #consts
  6. ENTRY_POINT="https://www.facebook.com"
  7. USERNAME="TheDesignerPenguin@gmail.com"
  8. PASSWORD="LarissaLee"
  9. LOGIN_FAILED_URL="http://www.facebook.com/login.php"
  10. TIMELINE_FRIEND_CONFIRM="Friends" # Look for this text to find the friends button
  11. SCROLL_LOAD_TIME=0.25
  12. SCROLL_BREAK_POINT=50
  13. REQ_DELAY=0.001
  14. MUTUAL_FRIENDS_URL="mutual_friend"
  15.  
  16. #selector consts
  17. sBACKGROUND="#mainContainer"
  18. sLOGIN_CSS="#email"
  19. sPASSWORD_CSS="#pass"
  20. sLOG_IN_CSS="#loginbutton input"
  21. sPROFILE="._2s25"
  22. sTIMELINE_BUTTONS="._6-6"
  23. sFRIEND_COUNT="div a span._gs6"
  24. sPERSON_NAME="#fb-timeline-cover-name"
  25. sSCROLL_FINISHED="#pagelet_timeline_medley_movies"
  26. sFRIENDS_LINK="div._6a._6b a._39g5"
  27. sREQUESTABLE_LINK="button:not(.hidden_elem)._42ft._4jy0.FriendRequestAdd.addButton._4jy3._4jy1.selected._51sy"
  28.  
  29. #opens the browser at a url
  30. def create(url):
  31.     browser = driver.Chrome()
  32.     browser.get(url)
  33.  
  34.     return browser
  35.  
  36. def go_to(browser, url):
  37.     browser.get(url)
  38.  
  39. def get_url(browser):
  40.     return browser.current_url
  41.  
  42. def find_element(browser, css, ty='css'):
  43.     return browser.find_element(ty, css)
  44.  
  45. def find_elements(browser, css, ty='css'):
  46.     return browser.find_elements(ty, css)
  47.  
  48. def enter_data(element, data):
  49.     element.send_keys(data)
  50.  
  51. def submit_data(element):
  52.     element.submit()
  53.  
  54. def click(element):
  55.     element.click()
  56.  
  57. def close(browser, code=0):
  58.     browser.quit()
  59.     print("quitting")
  60.     exit(code)
  61.  
  62. #generic function to scroll down to the bottom of a page
  63. def ScrollDownTill(selector, wait=0):
  64.     count=0
  65.    
  66.     while count<SCROLL_BREAK_POINT: #incase the selector element doesnt exist
  67.  
  68.         #scroll down
  69.         count += 1
  70.         browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  71.         time.sleep(wait)
  72.  
  73.         #check if the done scrolling element is visible
  74.         #slow, and I HATE try + except
  75.         try:
  76.             #the scroll is done
  77.             find_element(browser, selector)
  78.             break
  79.         except:
  80.             #good, keep scrolling
  81.             continue
  82.  
  83. #open up the browser to facebook.com
  84. browser = create(ENTRY_POINT)
  85.  
  86. #log into account
  87. enter_data(find_element(browser, sLOGIN_CSS), USERNAME)
  88. enter_data(find_element(browser, sPASSWORD_CSS), PASSWORD)
  89. click(find_element(browser, sLOG_IN_CSS))
  90.  
  91. #if the account login has failed, the url will be fb.com/login
  92. if LOGIN_FAILED_URL in get_url(browser):
  93.     print("Login failed")
  94.     close(browser)
  95.  
  96. print("login successful")
  97.  
  98. #Goto the profile
  99. go_to(browser, find_element(browser, sPROFILE).get_attribute('href'))
  100.  
  101. #Because of the way facebook is designed there is no unique ID for the friends button
  102. #Instead it is known to be the 2nd button in the list
  103. #Bad code
  104. profile_link=None
  105.  
  106. for l in find_elements(browser, sTIMELINE_BUTTONS):
  107.     if TIMELINE_FRIEND_CONFIRM in l.text:
  108.         profile_link=l
  109.  
  110. if profile_link == None:
  111.     print("Profile could not be found")
  112.     close(browser)
  113.        
  114. go_to(browser, profile_link.get_attribute('href'))
  115.  
  116. print("navigated to friends")
  117.  
  118. #if the login was successful, fucking facebook will ask for notifications
  119. #click on the background to remove
  120. enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
  121.  
  122. #grab data about the person being scanned
  123. person_name=find_element(browser, sPERSON_NAME).text
  124.  
  125. #the friends element is now selected, thus we can grab that and get the child
  126. #this child shows the number of friends
  127. person_friend_count_raw=find_element(browser, sFRIEND_COUNT).text
  128.  
  129. #friend count contains the phrase 'Mutual'
  130. #remove it
  131. person_friend_count=""
  132.  
  133. for char in person_friend_count_raw:
  134.     if char.isdigit():
  135.         person_friend_count += char
  136.  
  137. print("collected personal information:")
  138. print("{} has {} friend".format(person_name, person_friend_count))
  139.  
  140. # now the bot needs to collect an array of all the friends elements
  141. friendPageLinks=[]
  142.  
  143. #the current page should be added
  144. #not it shoudlnt
  145. #friendPageLinks.append(get_url(browser))
  146.  
  147. # first scroll to the bottom of the page
  148. ScrollDownTill(sSCROLL_FINISHED, SCROLL_LOAD_TIME)
  149.  
  150. print("scroled down the page successfully")
  151.  
  152. # now all 'friends links' need to be found
  153. for link in find_elements(browser, sFRIENDS_LINK):
  154.     friendPageLinks.append(link.get_attribute('href'))
  155.  
  156. print("gathered all friend links ({} of them!)".format(len(friendPageLinks)))
  157.  
  158. #now we need to go to all those links and add all non friends
  159. def AddFriends(url):
  160.  
  161.     #open
  162.     go_to(browser, url)
  163.     enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
  164.  
  165.     #scroll down page
  166.     ScrollDownTill(sSCROLL_FINISHED, SCROLL_LOAD_TIME)
  167.  
  168.     #loop through all non friend requestable people
  169.     requestableIndividuals=find_elements(browser, sREQUESTABLE_LINK)
  170.     successfulRequests=[]
  171.    
  172.     for person in requestableIndividuals:
  173.         print(person.get_attribute('aria-label'))
  174.  
  175.         #because im kool
  176.         try:
  177.             click(person)
  178.            
  179.         except:
  180.             print("mouse click simulation failed")
  181.             continue
  182.  
  183.         successfulRequests.append(person)
  184.  
  185.     return successfulRequests
  186.  
  187. #actually loop though all friends and 'add'' all of their friends
  188. requestedFriends=[]
  189.  
  190. #Facebook will try to be cool by giving links to mutual friends,
  191. #just remove those links
  192. #Using mutation to set
  193. friendPageLinks[:] = [x for x in friendPageLinks if not MUTUAL_FRIENDS_URL in x]
  194.  
  195. for friendUrl in friendPageLinks:
  196.  
  197.     oldLen=len(requestedFriends)
  198.     data = AddFriends(friendUrl)
  199.  
  200.     for element in data:
  201.         requestedFriends.append(element)
  202.         time.sleep(REQ_DELAY)
  203.  
  204.     print("Requested all friends of {} ({}), in total {} requests have been made!".format(friendUrl, len(requestedFriends)-oldLen, len(requestedFriends)))
  205.  
  206. print(requestedFriends)
  207. time.sleep(3)
  208. close(browser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement