Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. import gspread
  2. import time
  3. from selenium import webdriver
  4. from selenium.webdriver.support.wait import WebDriverWait
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.chrome.options import Options
  7. from oauth2client.service_account import ServiceAccountCredentials
  8.  
  9.  
  10. ###################################################
  11. ### Chrome Options / Login Details
  12. ###################################################
  13. chrome_options = Options()
  14. chrome_options.add_argument("--headless")
  15.  
  16. ###################################################
  17. ### Global Methods
  18. ###################################################
  19. def ask_continue():
  20. option = input("would you like to search another group?: ")
  21. if option.lower() == "yes":
  22. Google.search()
  23. elif option.lower() == "no":
  24. browser.close()
  25.  
  26. ###################################################
  27. ### Variables:
  28. ###################################################
  29. group_name = "frat"
  30. college_name = "tulane"
  31. social_type = 'instagram'
  32. browser = webdriver.Chrome(chrome_options= chrome_options, executable_path= '/Users/carsonrhodes/Documents/SeleniumDrivers/chromedriver') # comment out to remove browser if isolating
  33.  
  34. ###################################################
  35. ### Classes:
  36. ###################################################
  37.  
  38. class Google: # handles tasks involving the use of Google for search queries.
  39. def __init__(self):
  40. # variables
  41.  
  42. # self.group_name = input("type in the group/frat/soriority you'd like to search: ")
  43. # self.college_name = input("type in the college to search: ") # <- change to google another ig
  44. # self.social_type = input("type in the social to search: ") # <- change to switch social your googling
  45.  
  46. self.error = "[!] There was a critical error that broke the program!"
  47.  
  48. def search(self): # a function made for the purpose sending a query to Google.
  49. print("searching...")
  50. url = "http://www.google.com/"
  51. browser.get(url)
  52. xpath = "//input[@name='q']"
  53. google_search_box = browser.find_element_by_xpath(xpath)
  54. search_term = college_name + " " + group_name + " " + social_type
  55. google_search_box.send_keys(search_term, Keys.ENTER)
  56.  
  57. def locate_instagram(self): # a function made for the purpose of locating an instagram result.
  58. print("locating instagram page...")
  59. #instagram = browser.find_element_by_xpath(xpath= "//div[contains(text(),'Instagram photos and videos')]")
  60. instagram = WebDriverWait(browser, 10).until(lambda x: x.find_element_by_xpath(xpath= "//div[contains(text(),'Instagram photos and videos')]"))
  61. instagram.click()
  62. time.sleep(2)
  63. print(browser.current_url)
  64.  
  65. class InputData:
  66. def __init__(self):
  67. # use creds to create a client to interact with the Google Drive API
  68. self.scope = ['https://spreadsheets.google.com/feeds',
  69. 'https://www.googleapis.com/auth/drive']
  70. self.creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', self.scope)
  71. self.client = gspread.authorize(self.creds)
  72. self.sheet = ""
  73. self.error = "[!] There was a critical error that broke the program!"
  74.  
  75. def import_sheet(self): # checks data on sheet to make sure that no new data touches the spreadsheet.
  76. print("checking data...")
  77. try:
  78. self.sheet = self.client.open("College Bookings itslit.org").sheet1
  79. print(self.sheet.col_values(1))
  80. except:
  81. print(self.error)
  82. print("there was an error when trying to connect to google.")
  83.  
  84. def check_if_college_exists(self): # checks if college already exists on college columun
  85. college_name_formatted = college_name.lower()
  86. college_name_formatted = college_name_formatted.capitalize()
  87. for college in self.sheet.col_values(1):
  88. if college == college_name_formatted:
  89. matched = "yes"
  90. print("match")
  91. elif college != college_name_formatted:
  92. print("no match")
  93. if matched == "yes":
  94. print("college exists!")
  95. print("moving sheets...")
  96. InputData.navigate_sheet()
  97.  
  98. def navigate_sheet(self):
  99. worksheet_list = self.sheet.worksheets()
  100. print(worksheet_list)
  101.  
  102.  
  103. ###################################################
  104. ### CODE STARTS HERE:
  105. ###################################################
  106. Google = Google()
  107. Google.search()
  108. Google.locate_instagram()
  109.  
  110. InputData = InputData()
  111. InputData.import_sheet()
  112. InputData.check_if_college_exists()
  113. ask_continue()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement