Advertisement
Guest User

Untitled

a guest
May 28th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. from time import strftime
  2. import time, re, xlwt, xlrd, sys, os.path
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.support import expected_conditions as EC
  7. from selenium.common.exceptions import TimeoutException
  8. from xlutils.copy import copy
  9.  
  10. '''
  11. Tallyman Auto Excel Scraper
  12. Written By Ben Reich
  13. Version 1.0
  14.  
  15. This system takes an input (program -u username -p password -f file_location) and then
  16. logs into Tallyman and scrapes the account data for the week
  17.  
  18. TODO:
  19. - Set up the file format to be that which adheres to what the leads want
  20. - Set up templates so that it conforms to a template they desire
  21. - Monitor Mohammads queues (Catch All etc.)
  22. - Error reporting when username and password are incorrect
  23. - Log errors to file
  24. '''
  25.  
  26. i = 0
  27. username = None
  28. password = None
  29. file_location = None
  30. for arg in sys.argv:
  31. if arg.strip() == '-u':
  32. try:
  33. if sys.argv[i+1].strip() != '-p' and sys.argv[i+1].strip() != '-f':
  34. username = sys.argv[i+1]
  35. else:
  36. sys.exit('No username set')
  37. except IndexError:
  38. sys.exit('No username set')
  39. elif arg.strip() == '-p':
  40. try:
  41. if sys.argv[i+1].strip() != '-f' and sys.argv[i+1].strip() != '-u':
  42. password = sys.argv[i+1]
  43. else:
  44. sys.exit('No password set')
  45. except IndexError:
  46. sys.exit('No password set')
  47. elif arg.strip() == '-f':
  48. try:
  49. if sys.argv[i+1].strip() != '-p' and sys.argv[i+1].strip() != '-u':
  50. file_location = sys.argv[i+1]
  51. if os.path.exists(file_location) == False:
  52. sys.exit('File location is invalid')
  53. else:
  54. sys.exit('No file location set')
  55. except IndexError:
  56. sys.exit('No file location set')
  57. i += 1
  58.  
  59. if username == None:
  60. sys.exit('Username not set')
  61. if password == None:
  62. sys.exit('Password not set')
  63. if file_location == None:
  64. sys.exit('File location not set')
  65.  
  66. start_date = strftime("%d-%m-%Y", time.localtime())
  67. filename = file_location + start_date + ".xls"
  68.  
  69. def find_by_xpath(locator, driver):
  70. element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, locator)))
  71. return element
  72.  
  73. def close_windows(driver, window_title=None):
  74. handle_switch = False
  75. if window_title == None:
  76. for handle in driver.window_handles:
  77. driver.switch_to_window(handle)
  78. driver.close()
  79. else:
  80. for handle in driver.window_handles:
  81. driver.switch_to_window(handle)
  82. if window_title == driver.title:
  83. handle_switch = handle
  84. if handle_switch != False:
  85. driver.switch_to_window(handle_switch)
  86.  
  87. def wait_popup(driver):
  88. try:
  89. WebDriverWait(driver, 3).until(EC.alert_is_present())
  90. alert = driver.switch_to_alert()
  91. alert.accept()
  92. print "alert accepted"
  93. except TimeoutException:
  94. print "no alert"
  95.  
  96. driver = webdriver.Firefox()
  97. driver.get("https://10.7.148.205/collections/Login.do")
  98. wait_popup(driver)
  99.  
  100. find_by_xpath('//input[@name="user"]', driver).send_keys(username)
  101. find_by_xpath('//input[@name="password"]', driver).send_keys(password)
  102. find_by_xpath('//input[@name="btnOk"]', driver).click()
  103.  
  104. wait_popup(driver)
  105. close_windows(driver, "Tallyman")
  106.  
  107. driver.get("https://10.7.148.205/collections/ShowUserWorklists.do")
  108. wait_popup(driver)
  109. close_windows(driver, "User Work Lists")
  110.  
  111. find_by_xpath('//*[@id="showAccountCount"]', driver).click()
  112.  
  113. close_windows(driver, "User Work Lists")
  114.  
  115. html = driver.page_source
  116. matches = re.findall(r'<td class="label">([A-Za-z0-9\s\)\(]+)<\/td>([\s]+)<td>([\s]+)\(([0-9]+)', html)
  117. close_windows(driver)
  118.  
  119. lists = {'Arrangement Completed In Arrears': 0,
  120. 'Adv C1 FPD Call': 0,
  121. 'Adv C1 High Val Call': 0,
  122. 'Adv C1 HR Call': 0,
  123. 'Adv C1 LR Call': 0,
  124. 'Adv C1 MR Call': 0,
  125. 'Adv C1 Repeat Call': 0,
  126. 'Adv C1 Short Ten Call': 0,
  127. 'Adv C2 High Val Call': 0,
  128. 'Adv C2 Pre NOD': 0,
  129. 'Adv C2 HR Call': 0,
  130. 'Adv C2 MR Call': 0,
  131. 'Adv C2 Repeat Call': 0,
  132. 'Adv C2 Short Ten Call': 0,
  133. 'Adv C3 Call': 0,
  134. 'Chall C1 High Vall Call': 0,
  135. 'Chall C1 HR Call': 0,
  136. 'Chall C1 Repeat Call': 0,
  137. 'Chall C2 High Val Call': 0,
  138. 'Chall C2 Pre NOD': 0,
  139. 'Chall C2 HR Call': 0,
  140. 'Chall C2 Repeat Call': 0,
  141. 'Chall C3 Call': 0,
  142. 'Arr Broken (Collections)': 0,
  143. 'Arr Incomplete (Collections)': 0}
  144.  
  145. total_accounts = 0
  146.  
  147. for match in matches:
  148. lists[match[0].strip()] = match[3].strip()
  149. total_accounts += int(match[3].strip())
  150.  
  151. try:
  152. workbook = xlrd.open_workbook(filename, formatting_info=True)
  153. temp_sheet = workbook.sheet_by_name("Hourly Track")
  154. col_idx = temp_sheet.ncols
  155. workbook_exists = True
  156. book = copy(workbook)
  157. sheet1 = book.get_sheet(0)
  158. except IOError:
  159. book = xlwt.Workbook(encoding="utf-8")
  160. sheet1 = book.add_sheet("Hourly Track")
  161. col_idx = 1
  162. workbook_exists = False
  163.  
  164. NoBackground = xlwt.easyxf('font: name Calibri, height 220, colour black')
  165. NoBackgroundBold = xlwt.easyxf('font: name Calibri, height 220, colour black, bold on')
  166. RedBackground = xlwt.easyxf('font: name Calibri, height 220, colour black, bold on; pattern: pattern solid, fore_colour red')
  167. GreenBackground = xlwt.easyxf('font: name Calibri, height 220, colour black, bold on; pattern: pattern solid, fore_colour light_green')
  168. YellowBackground = xlwt.easyxf('font: name Calibri, height 220, colour black, bold on; pattern: pattern solid, fore_colour yellow')
  169.  
  170. sheet1.col(0).width = 4800
  171. sheet1.col(1).width = 2500
  172.  
  173. if workbook_exists == False:
  174. sheet1.write(1, 0, label = "Arr Comp in Arrs", style = NoBackground)
  175. sheet1.write(2, 0, label = "ADV C1 FPD Call", style = NoBackground)
  176. sheet1.write(3, 0, label = "ADV C1 High Val", style = NoBackground)
  177. sheet1.write(4, 0, label = "ADV C1 HR Call", style = NoBackground)
  178. sheet1.write(5, 0, label = "ADV C1 LR Call", style = NoBackground)
  179. sheet1.write(6, 0, label = "ADV C1 MR Call", style = NoBackground)
  180. sheet1.write(7, 0, label = "ADV C1 Repeat Call", style = NoBackground)
  181. sheet1.write(8, 0, label = "Adv C1 Short Ten", style = NoBackground)
  182. sheet1.write(9, 0, label = "Adv C2 High Val", style = NoBackground)
  183. sheet1.write(10, 0, label = "Adv C2 Pre NOD", style = NoBackground)
  184. sheet1.write(11, 0, label = "Adv C2 HR Call", style = NoBackground)
  185. sheet1.write(12, 0, label = "Adv C2 MR Call", style = NoBackground)
  186. sheet1.write(13, 0, label = "Adv C2 Repeat Call", style = NoBackground)
  187. sheet1.write(14, 0, label = "Adv C2 Short Ten", style = NoBackground)
  188. sheet1.write(15, 0, label = "Adv C3 Call", style = NoBackground)
  189. sheet1.write(16, 0, label = "Chall C1 High Val Call", style = NoBackground)
  190. sheet1.write(17, 0, label = "Chall C1 HR Call", style = NoBackground)
  191. sheet1.write(18, 0, label = "Chall C1 Repeat Call", style = NoBackground)
  192. sheet1.write(19, 0, label = "Chall C2 High Val Call", style = NoBackground)
  193. sheet1.write(20, 0, label = "Chall C2 Pre NOD", style = NoBackground)
  194. sheet1.write(21, 0, label = "Chall C2 HR Call", style = NoBackground)
  195. sheet1.write(22, 0, label = "Chall C2 Repeat Call", style = NoBackground)
  196. sheet1.write(23, 0, label = "Chall C3 Call", style = NoBackground)
  197. sheet1.write(24, 0, label = "Arr Broken", style = NoBackground)
  198. sheet1.write(25, 0, label = "Arr Incomplete", style = NoBackground)
  199.  
  200. def write_num(row_idx, col_idx, list_name):
  201. if col_idx > 1:
  202. if int(lists[list_name])==0 && int(temp_sheet.cell_value(row_idx, col_idx).value) > 0:
  203. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = GreenBackground)
  204. else:
  205. difference = int(temp_sheet.cell_value(row_idx, col_idx).value)-int(lists[list_name])
  206. if difference > 0:
  207. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = RedBackground)
  208. elif difference < 0:
  209. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = YellowBackground)
  210. else:
  211. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = NoBackground)
  212. else:
  213. if int(lists[list_name])==0:
  214. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = GreenBackground)
  215. else
  216. sheet1.write(row_idx, col_idx, label = int(lists[list_name]), style = NoBackground)
  217.  
  218. sheet1.write(0, col_idx, label = strftime("%H:%M", time.localtime()), style = NoBackground)
  219. write_num(1, col_idx, 'Arrangement Completed In Arrears')
  220. write_num(2, col_idx, 'Adv C1 FPD Call')
  221. write_num(3, col_idx, 'Adv C1 High Val Call')
  222. write_num(4, col_idx, 'Adv C1 HR Call')
  223. write_num(5, col_idx, 'Adv C1 LR Call')
  224. write_num(6, col_idx, 'Adv C1 MR Call')
  225. write_num(7, col_idx, 'Adv C1 Repeat Call')
  226. write_num(8, col_idx, 'Adv C1 Short Ten Call')
  227. write_num(9, col_idx, 'Adv C2 High Val Call')
  228. write_num(10, col_idx, 'Adv C2 Pre NOD')
  229. write_num(11, col_idx, 'Adv C2 HR Call')
  230. write_num(12, col_idx, 'Adv C2 MR Call')
  231. write_num(13, col_idx, 'Adv C2 Repeat Call')
  232. write_num(14, col_idx, 'Adv C2 Short Ten Call')
  233. write_num(15, col_idx, 'Adv C3 Call')
  234. write_num(16, col_idx, 'Chall C1 High Val Call')
  235. write_num(17, col_idx, 'Chall C1 HR Call')
  236. write_num(18, col_idx, 'Chall C1 Repeat Call')
  237. write_num(19, col_idx, 'Chall C2 High Val Call')
  238. write_num(20, col_idx, 'Chall C2 Pre NOD')
  239. write_num(21, col_idx, 'Chall C2 HR Call')
  240. write_num(22, col_idx, 'Chall C2 Repeat Call')
  241. write_num(23, col_idx, 'Chall C3 Call')
  242. write_num(24, col_idx, 'Arr Broken (Collections)')
  243. write_num(25, col_idx, 'Arr Incomplete (Collections)')
  244.  
  245. sheet1.write(26, col_idx, total_accounts, style = NoBackground)
  246. if workbook_exists == True:
  247. sheet1.write(27, col_idx, label = (temp_sheet.cell_value(rowx=26, colx=(col_idx-1))-total_accounts), style = NoBackground)
  248.  
  249. book.save(filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement