Advertisement
PanzerFaustNL

Facebook request handler

Mar 13th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. # selenium imports
  2. import time
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. from selenium.webdriver.support.expected_conditions import *
  8.  
  9. # filehandling #
  10. import os.path
  11.  
  12. # date time #
  13. from datetime import date
  14.  
  15.  
  16. # check if the html element is available
  17. # Marco de Boer (c) 2020
  18. # version 1 : new
  19. def isAvailable(available):
  20.     if len(driver.find_elements_by_xpath(available)) > 0:
  21.         return True
  22.     else:
  23.         return False
  24.  
  25.  
  26. # doTheJob, the title is clear I think, it does the job
  27. # Marco de Boer (c) 2020
  28. # version 1 : new
  29. def doTheJob():
  30.     driver.implicitly_wait(10)
  31.     createLogFile('doTheJob started : ' + str(date.today()))
  32.     amountXPath = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  33.                   '2]/div/div/div/div/div[2]/div/div[1]/div/div[1]/div/span '
  34.     time.sleep(10)
  35.     sAmount = driver.find_element_by_xpath(amountXPath).text
  36.     sAmount = sAmount.split()[0]
  37.     amount = int(sAmount)
  38.     print('Amount of requests : ' + str(amount))
  39.     print('===========================')
  40.     try:
  41.         if amount > 0:
  42.             while amount > 0:
  43.                 requesterName = '/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]/div[3]/div[' \
  44.                                 '1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]/ul[1]/li[1]/div[1]/div[' \
  45.                                 '2]/div[1]/div[1]/div[2]/a[1] '
  46.                 available = isAvailable(requesterName)
  47.                 Available(available, requesterName, 'Requester name : ')
  48.                 whyPython = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  49.                             '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[4]/ul/li[1]/text '
  50.                 available = isAvailable(whyPython)
  51.                 Available(available, whyPython, 'Why python : ')
  52.  
  53.                 experience = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  54.                              '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[4]/ul/li[2] '
  55.                 available = isAvailable(experience)
  56.                 Available(available, experience, 'Experience : ')
  57.  
  58.                 approve = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  59.                           '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[5] '
  60.                 available = isAvailable(approve)
  61.                 Available(available, approve, 'Ok to the rules : ')
  62.  
  63.                 if approve == 'Akkoord':
  64.                     ok = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  65.                          '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[1]/button[1] '
  66.                     driver.find_element_by_xpath(ok).click()
  67.                 else:
  68.                     cancel = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
  69.                              '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[1]/button[2] '
  70.                     driver.find_element_by_xpath(cancel).click()
  71.                 amount -= 1
  72.                 amount = checkAmount(amount, amountXPath)
  73.         else:
  74.             checkAmount(amount, amountXPath)
  75.     except Exception as e2:
  76.         print('doTheJob : \n')
  77.         print(e2)
  78.     finally:
  79.         doTheJob()
  80.  
  81.  
  82. # The element is avialable or if it is available, lets print it to the console
  83. # Marco de Boer (c) 2020
  84. # version 1 : new
  85. def Available(available, something, tekst):
  86.     if available:
  87.         something = driver.find_element_by_xpath(something).text
  88.         print(tekst + something)
  89.  
  90.  
  91. # Check the amount of requests
  92. # Marco de Boer (c) 2020
  93. # version 1 : new
  94. def checkAmount(amount, amountXPath):
  95.     global i
  96.     if amount == 0:
  97.         print('No requests anymore, lets check if there are new requests')
  98.         driver.refresh()
  99.         sAmount = driver.find_element_by_xpath(amountXPath).text
  100.         sAmount = sAmount.split()[0]
  101.         amount = int(sAmount)
  102.         print('New requests : ' + str(amount))
  103.         print('round : ' + str(i))
  104.         i += 1
  105.     return amount
  106.  
  107.  
  108. # starting point and the inlog
  109. # Marco de Boer (c) 2020
  110. # version 1 : new
  111. def main():
  112.     global driver
  113.     global i
  114.     i = 0
  115.     url = 'https://www.facebook.com/login/?next=https%3A%2F%2Fwww.facebook.com%2Fgroups%2F1237479913012214%2Frequests/'
  116.     driver = webdriver.Firefox()
  117.     driver.get(url)
  118.     driver.implicitly_wait(10)
  119.     driver.find_element(By.ID, 'email').click()
  120.     driver.find_element(By.ID, 'email').send_keys('**********')
  121.     driver.find_element(By.ID, 'pass').send_keys('**********')
  122.     driver.find_element(By.ID, 'pass').send_keys(Keys.ENTER)
  123.     try:
  124.         while True:
  125.             driver.implicitly_wait(10)
  126.             doTheJob()
  127.     except Exception as e:
  128.         print('main :\n ')
  129.         createLogFile('main : \n======================\n')
  130.         print(e + '\n=======================\n')
  131.         # driver.quit()
  132.     finally:
  133.         doTheJob()
  134.  
  135.  
  136. def createLogFile(tekst):
  137.     global f
  138.     if os.path.isfile('requests.txt'):
  139.         f = open('requests.txt', 'a+')
  140.         f.write(tekst + '\n')
  141.     else:
  142.         f = open('requests.txt', 'w+')
  143.         f.write(tekst + '\n')
  144.  
  145.  
  146. # main
  147. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement