Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. from __future__ import print_function
  2. from pprint import pprint
  3. from googleapiclient import discovery
  4. import httplib2
  5. import os
  6. from apiclient import discovery
  7. from oauth2client import client
  8. from oauth2client import tools
  9. from oauth2client.file import Storage
  10. import time, os
  11. from selenium import webdriver
  12. from selenium.webdriver.common.keys import Keys
  13. from selenium.webdriver.common.by import By
  14. from selenium.webdriver.common.service import Service
  15.  
  16.  
  17. try:
  18. import argparse
  19. flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
  20. except ImportError:
  21. flags = None
  22.  
  23. # If modifying these scopes, delete your previously saved credentials
  24. # at ~/.credentials/sheets.googleapis.com-python-quickstart.json
  25. SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/spreadsheets.readonly']
  26. CLIENT_SECRET_FILE = 'client_secret.json'
  27. APPLICATION_NAME = 'Digipanel Bot'
  28.  
  29.  
  30. def get_credentials():
  31. """Gets valid user credentials from storage.
  32.  
  33. If nothing has been stored, or if the stored credentials are invalid,
  34. the OAuth2 flow is completed to obtain the new credentials.
  35.  
  36. Returns:
  37. Credentials, the obtained credential.
  38. """
  39. home_dir = os.path.expanduser('~')
  40. credential_dir = os.path.join(home_dir, '.credentials')
  41. if not os.path.exists(credential_dir):
  42. os.makedirs(credential_dir)
  43. credential_path = os.path.join(credential_dir, 'sheets.googleapis.com-DigipanelBot.json')
  44.  
  45. store = Storage(credential_path)
  46. credentials = store.get()
  47. if not credentials or credentials.invalid:
  48. flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  49. flow.user_agent = APPLICATION_NAME
  50. if flags:
  51. credentials = tools.run_flow(flow, store, flags)
  52. else: # Needed only for compatibility with Python 2.6
  53. credentials = tools.run(flow, store)
  54. print('Storing credentials to ' + credential_path)
  55. return credentials
  56.  
  57. def start():
  58. log_name = input('Enter Username: ')
  59. log_pass = input('Enter Password: ')
  60. driver = open_chrome()
  61. print()
  62. login(log_name, log_pass, driver)
  63.  
  64. def open_chrome():
  65. print('Opening Chrome...')
  66. driver = webdriver.Chrome()
  67. driver.get("http://rto.digitals.pw/")
  68. return driver
  69.  
  70. def login(log_name, log_pass, driver):
  71. username = driver.find_element_by_name("username")
  72. username.clear()
  73. username.send_keys(log_name)
  74.  
  75. username = driver.find_element_by_name("password")
  76. username.clear()
  77. username.send_keys(log_pass)
  78. time.sleep(1)
  79. driver.find_element_by_name("login").click()
  80.  
  81. if "login" in driver.current_url:
  82. print("Incorrect Username or Password, please try again.")
  83. print()
  84. log_name = input('Enter Username: ')
  85. log_pass = input('Enter Password: ')
  86. login(log_name, log_pass, driver)
  87. else:
  88. search(driver)
  89.  
  90. def search(driver):
  91. driver.get("http://rto.digitals.pw/police/interpol/kavala")
  92. search = driver.find_element_by_xpath("//button[10]")
  93. search.click()
  94. print("Gathering Data...")
  95. time.sleep(15)
  96.  
  97. complete = False
  98. count = 1
  99. data = [[],[]]
  100. lastNames = []
  101.  
  102. while complete == False:
  103. try:
  104. search = driver.find_element_by_xpath("//tr[{0}]/td[1]".format(count)).text
  105. data[0].append(search)
  106. lastName = driver.find_element_by_xpath("//tr[{0}]/td[4]".format(count)).text
  107. lastNames.append(lastName)
  108. lastActive = driver.find_element_by_xpath("//tr[{0}]/td[5]".format(count)).text
  109. lastActive = lastActive[2:13]
  110. data[1].append(lastActive)
  111. count += 1
  112. except:
  113. complete = True
  114. sync(data, lastNames)
  115.  
  116. def sync(data, lastNames):
  117.  
  118. credentials = get_credentials()
  119.  
  120. service = discovery.build('sheets', 'v4', credentials=credentials)
  121.  
  122. #ANY EDITS, CHANGE SHEET ID!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  123. spreadsheet_id = '19MN7jyO6RzGAMFIjv65Oh6Z7BR2dxNYouvaml7y44xQ'
  124. range_ = 'Kavala!B:B'
  125. value_render_option = 'FORMATTED_VALUE'
  126. date_time_render_option = 'SERIAL_NUMBER'
  127.  
  128.  
  129. request = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_, valueRenderOption=value_render_option, dateTimeRenderOption=date_time_render_option)
  130. response = request.execute()
  131.  
  132. count = 1
  133. print("Updating Interpol...")
  134. for i in response['values']:
  135. if "Meeting" in str(i):
  136. break
  137. else:
  138. if len(i) > 0 and 'Vacant' not in i:
  139. try:
  140. index = data[0].index(i[0])
  141. if 'Police' in lastNames[index]:
  142. value = data[1][index]
  143. values = [[str(value[1:])]]
  144. body = {'values' : values}
  145.  
  146. request = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id, range='Kavala!N'+str(count),
  147. valueInputOption='RAW', body=body).execute()
  148. else:
  149. pass
  150. except:
  151. pass
  152. else:
  153. pass
  154. count += 1
  155.  
  156. print("Complete.")
  157. input()
  158. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement