Guest User

Untitled

a guest
Jan 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. # Iterate through every batch in update_dictionary. Should this operation get interrupted, re-run generate_update_ids.py to generate the latest list of IDs to be updated, and run this script again.
  2. for key in update_dictionary:
  3.  
  4. # Declare a variable start_time as the current time.
  5. start_time = time()
  6.  
  7. # Declare a list to which we append the output from running the update() function.
  8. results = []
  9.  
  10. # Obtain the list of IDs corresponding to the key in the current iteration.
  11. batch = update_dictionary[key]
  12.  
  13. # Randomise sorting of IDs to make scraping less suspicious to the server.
  14. id_randomised = random.sample(batch, len(batch))
  15.  
  16. # Perform update() function on all car IDs in the list; pause for a random period of time to reduce suspicion and the frequency of get requests.
  17. for car_id in id_randomised:
  18. output = update(str(car_id))
  19. if output != None:
  20. results.append(output)
  21. sleep(random.uniform(0.05,0.1))
  22.  
  23. # Declare a list sold_list to which sold listings will be appended.
  24. sold_list = []
  25.  
  26. # Declare a list update_list to which available-for-sale listings will be appended; the latest listing data will be overwritten into the DB.
  27. update_list = []
  28.  
  29. # Using the second element in the list output of update(), we sort the sold listings and available-for-sale listings into the respective lists.
  30. # Sold listings have the list element in index 1 (second element) set as the listing ID (int(car_id)) with type() = int, while available-for-sale listings have it set as the vehicle availability (avail) with type() = str.
  31. for result in results:
  32. if type(result[1]) == int:
  33. sold_list.append(result)
  34. elif type(result[1]) == str:
  35. update_list.append(result)
  36.  
  37. # Print sold_list, update_list and their respective lengths for quick checking
  38. print(sold_list)
  39. print('Length of sold_list: ' + str(len(sold_list)))
  40. print(update_list)
  41. print('Length of update_list: ' + str(len(update_list)))
  42.  
  43. # Connect to the DB and create a Cursor object and use it to execute SQL commands.
  44. conn = sqlite3.connect('db.db')
  45. c = conn.cursor()
  46.  
  47. # Execute to DB updates for sold vehicles.
  48. try:
  49. for car in sold_list:
  50. c.execute("UPDATE cars SET avail=?, date_sold=?, last_edited=? WHERE id=?", ['Sold',car[0],datetime.now().date(),car[1]])
  51. except:
  52. None
  53.  
  54. # Execute to DB updates for available vehicles. Very tedious method used in matching the list elements to their respective columns in the DB. Was not able to find a simplified way which worked.
  55. for car in update_list:
  56. c.execute('''UPDATE cars SET make_model=?, price=?, depre=?, reg_date=?, manu_yr=?, mileage=?, tranny=?, eng_cap=?,
  57. road_tax=?, power=?, weight=?, features=?, acc=?, coe_left=?, coe_price=?, omv=?, arf=?, owners=?, veh_type=?,
  58. cat=?, date_posted=?, updated=?, tags=?, last_edited=?, desc=? WHERE id=?''',[car[2],car[3],car[4],car[5],car[6],car[7],car[8],car[9],car[10],
  59. car[11],car[12],car[13],car[14],car[15],car[16],car[17],car[18],
  60. car[19],car[20],car[21],car[22],car[23],car[24],datetime.now().date(),car[26],car[0]])
  61.  
  62. # Confirm the changes to be made to the DB and close the connection to the DB.
  63. conn.commit()
  64. conn.close()
  65.  
  66. # Calculate time taken for the whole operation
  67. elapsed = round(time() - start_time, 2)
  68. print("time elapsed = " + str(elapsed))
  69. print("time taken per page = " + str(round(elapsed/(len(batch)),2)))
Add Comment
Please, Sign In to add comment