Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 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. print("Digipanel Login")
  59. log_name = input('Enter Username: ')
  60. log_pass = input('Enter Password: ')
  61. driver = open_chrome()
  62. print()
  63. login(log_name, log_pass, driver)
  64.  
  65. def open_chrome():
  66. print('Opening Chrome...')
  67. driver = webdriver.Chrome()
  68. driver.get("http://rto.digitals.pw/")
  69. return driver
  70.  
  71. def login(log_name, log_pass, driver):
  72. username = driver.find_element_by_name("username")
  73. username.clear()
  74. username.send_keys(log_name)
  75.  
  76. username = driver.find_element_by_name("password")
  77. username.clear()
  78. username.send_keys(log_pass)
  79. time.sleep(1)
  80. driver.find_element_by_name("login").click()
  81.  
  82. if "login" in driver.current_url:
  83. print("Incorrect Username or Password, please try again.")
  84. print()
  85. log_name = input('Enter Username: ')
  86. log_pass = input('Enter Password: ')
  87. print("Re-trying...")
  88. login(log_name, log_pass, driver)
  89. else:
  90. print('Success!')
  91. search(driver)
  92.  
  93. def search(driver):
  94. print()
  95. print("Choose the number to update:")
  96. print("1. Kavala")
  97. print("2. Pyrgos")
  98. print("3. RPU")
  99. print("4. Sofia")
  100. option = input("")
  101. print()
  102. valid = False
  103. while valid == False:
  104. try:
  105. option = int(option)
  106.  
  107. if option == 1:
  108. option = "kavala"
  109. valid = True
  110. elif option == 2:
  111. option = "pyrgos"
  112. valid = True
  113. elif option == 3:
  114. option = "rpu"
  115. valid = True
  116. elif option == 4:
  117. option = "sofia"
  118. valid = True
  119. else:
  120. print("Invalid option, try again:")
  121. option = input()
  122. except:
  123. print("Invalid option, try again:")
  124. option = input()
  125.  
  126.  
  127. driver.get("http://rto.digitals.pw/police/interpol/{}".format(option))
  128. search = driver.find_element_by_xpath("//button[10]")
  129. search.click()
  130. print("Gathering Data...")
  131. time.sleep(15)
  132.  
  133. complete = False
  134. count = 1
  135. data = [[],[]]
  136. lastNames = []
  137.  
  138. while complete == False:
  139. try:
  140. search = driver.find_element_by_xpath("//tr[{0}]/td[1]".format(count)).text
  141. data[0].append(search)
  142. lastName = driver.find_element_by_xpath("//tr[{0}]/td[4]".format(count)).text
  143. lastNames.append(lastName)
  144. lastActive = driver.find_element_by_xpath("//tr[{0}]/td[5]".format(count)).text
  145. lastActive = lastActive[2:13]
  146. data[1].append(lastActive)
  147. count += 1
  148. except:
  149. complete = True
  150. sync(data, lastNames, option)
  151.  
  152. def sync(data, lastNames, option):
  153.  
  154. credentials = get_credentials()
  155.  
  156. service = discovery.build('sheets', 'v4', credentials=credentials)
  157.  
  158. #ANY EDITS, CHANGE SHEET ID!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  159. spreadsheet_id = '1UVe0qn9BsZgQSSYaOKh_nNIUylW6NV6ZHNE-jMz7KCE'
  160. if option == "rpu":
  161. option = option.upper()
  162. else:
  163. option = option.title()
  164. range_ = '{}!B:B'.format(option)
  165. value_render_option = 'FORMATTED_VALUE'
  166. date_time_render_option = 'SERIAL_NUMBER'
  167.  
  168.  
  169. request = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_, valueRenderOption=value_render_option, dateTimeRenderOption=date_time_render_option)
  170. response = request.execute()
  171.  
  172. count = 1
  173. print("Updating Interpol...")
  174. for i in response['values']:
  175. if "Meeting" in str(i):
  176. break
  177. else:
  178. if len(i) > 0 and 'Vacant' not in i:
  179. try:
  180. index = data[0].index(i[0])
  181. if 'Police' in lastNames[index]:
  182. value = data[1][index]
  183. values = [[str(value[1:])]]
  184. body = {'values' : values}
  185. request = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id, range='{}!N'.format(option)+str(count),
  186. valueInputOption='RAW', body=body).execute()
  187. else:
  188. pass
  189. except:
  190. pass
  191. else:
  192. pass
  193. count += 1
  194.  
  195. print("Complete.")
  196. input()
  197.  
  198. print("Last Active Updater | Version 1.1 |\nBy Tommy Smith For GrandTheftArma Police Constabulary")
  199. print()
  200. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement