Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver as driver # fakes human interaction
- from selenium.webdriver.common.keys import Keys as keys
- import time # waiting
- #consts
- ENTRY_POINT="https://www.facebook.com"
- USERNAME="TheDesignerPenguin@gmail.com"
- PASSWORD="LarissaLee"
- LOGIN_FAILED_URL="http://www.facebook.com/login.php"
- TIMELINE_FRIEND_CONFIRM="Friends" # Look for this text to find the friends button
- SCROLL_LOAD_TIME=0.25
- SCROLL_BREAK_POINT=50
- REQ_DELAY=0.001
- MUTUAL_FRIENDS_URL="mutual_friend"
- #selector consts
- sBACKGROUND="#mainContainer"
- sLOGIN_CSS="#email"
- sPASSWORD_CSS="#pass"
- sLOG_IN_CSS="#loginbutton input"
- sPROFILE="._2s25"
- sTIMELINE_BUTTONS="._6-6"
- sFRIEND_COUNT="div a span._gs6"
- sPERSON_NAME="#fb-timeline-cover-name"
- sSCROLL_FINISHED="#pagelet_timeline_medley_movies"
- sFRIENDS_LINK="div._6a._6b a._39g5"
- sREQUESTABLE_LINK="button:not(.hidden_elem)._42ft._4jy0.FriendRequestAdd.addButton._4jy3._4jy1.selected._51sy"
- #opens the browser at a url
- def create(url):
- browser = driver.Chrome()
- browser.get(url)
- return browser
- def go_to(browser, url):
- browser.get(url)
- def get_url(browser):
- return browser.current_url
- def find_element(browser, css, ty='css'):
- return browser.find_element(ty, css)
- def find_elements(browser, css, ty='css'):
- return browser.find_elements(ty, css)
- def enter_data(element, data):
- element.send_keys(data)
- def submit_data(element):
- element.submit()
- def click(element):
- element.click()
- def close(browser, code=0):
- browser.quit()
- print("quitting")
- exit(code)
- #generic function to scroll down to the bottom of a page
- def ScrollDownTill(selector, wait=0):
- count=0
- while count<SCROLL_BREAK_POINT: #incase the selector element doesnt exist
- #scroll down
- count += 1
- browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
- time.sleep(wait)
- #check if the done scrolling element is visible
- #slow, and I HATE try + except
- try:
- #the scroll is done
- find_element(browser, selector)
- break
- except:
- #good, keep scrolling
- continue
- #open up the browser to facebook.com
- browser = create(ENTRY_POINT)
- #log into account
- enter_data(find_element(browser, sLOGIN_CSS), USERNAME)
- enter_data(find_element(browser, sPASSWORD_CSS), PASSWORD)
- click(find_element(browser, sLOG_IN_CSS))
- #if the account login has failed, the url will be fb.com/login
- if LOGIN_FAILED_URL in get_url(browser):
- print("Login failed")
- close(browser)
- print("login successful")
- #Goto the profile
- go_to(browser, find_element(browser, sPROFILE).get_attribute('href'))
- #Because of the way facebook is designed there is no unique ID for the friends button
- #Instead it is known to be the 2nd button in the list
- #Bad code
- profile_link=None
- for l in find_elements(browser, sTIMELINE_BUTTONS):
- if TIMELINE_FRIEND_CONFIRM in l.text:
- profile_link=l
- if profile_link == None:
- print("Profile could not be found")
- close(browser)
- go_to(browser, profile_link.get_attribute('href'))
- print("navigated to friends")
- #if the login was successful, fucking facebook will ask for notifications
- #click on the background to remove
- enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
- #grab data about the person being scanned
- person_name=find_element(browser, sPERSON_NAME).text
- #the friends element is now selected, thus we can grab that and get the child
- #this child shows the number of friends
- person_friend_count_raw=find_element(browser, sFRIEND_COUNT).text
- #friend count contains the phrase 'Mutual'
- #remove it
- person_friend_count=""
- for char in person_friend_count_raw:
- if char.isdigit():
- person_friend_count += char
- print("collected personal information:")
- print("{} has {} friend".format(person_name, person_friend_count))
- # now the bot needs to collect an array of all the friends elements
- friendPageLinks=[]
- #the current page should be added
- #not it shoudlnt
- #friendPageLinks.append(get_url(browser))
- # first scroll to the bottom of the page
- ScrollDownTill(sSCROLL_FINISHED, SCROLL_LOAD_TIME)
- print("scroled down the page successfully")
- # now all 'friends links' need to be found
- for link in find_elements(browser, sFRIENDS_LINK):
- friendPageLinks.append(link.get_attribute('href'))
- print("gathered all friend links ({} of them!)".format(len(friendPageLinks)))
- #now we need to go to all those links and add all non friends
- def AddFriends(url):
- #open
- go_to(browser, url)
- enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
- #scroll down page
- ScrollDownTill(sSCROLL_FINISHED, SCROLL_LOAD_TIME)
- #loop through all non friend requestable people
- requestableIndividuals=find_elements(browser, sREQUESTABLE_LINK)
- successfulRequests=[]
- for person in requestableIndividuals:
- print(person.get_attribute('aria-label'))
- #because im kool
- try:
- click(person)
- except:
- print("mouse click simulation failed")
- continue
- successfulRequests.append(person)
- return successfulRequests
- #actually loop though all friends and 'add'' all of their friends
- requestedFriends=[]
- #Facebook will try to be cool by giving links to mutual friends,
- #just remove those links
- #Using mutation to set
- friendPageLinks[:] = [x for x in friendPageLinks if not MUTUAL_FRIENDS_URL in x]
- for friendUrl in friendPageLinks:
- oldLen=len(requestedFriends)
- data = AddFriends(friendUrl)
- for element in data:
- requestedFriends.append(element)
- time.sleep(REQ_DELAY)
- print("Requested all friends of {} ({}), in total {} requests have been made!".format(friendUrl, len(requestedFriends)-oldLen, len(requestedFriends)))
- print(requestedFriends)
- time.sleep(3)
- close(browser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement