Guest User

Facebook bot

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