diff --git a/download_worker.py b/download_worker.py index 0345ae8..c57012b 100644 --- a/download_worker.py +++ b/download_worker.py @@ -20,17 +20,29 @@ con = sqlite3.connect('instance/project.db') def run(args): + # iterate through the first argument, appending the second and third to each iteration args = [([i], args[1], args[2]) for i in args[0]] - print(pool.map(download, args, 1)) + pool.map(download, args, 1) def download(args): - row = args[0][0] + # explode arguments into variable names + row, een_obj, cam = args - fname = f"videos/{row[2]}/{row[2]}_{row[3]}-{row[4]}.mp4" + row = row[0] + camera_device_id = row[2] + start = row[3] + end = row[4] + + start = datetime.fromisoformat(start) + os.makedirs(f"videos/{camera_device_id}/{start.year}/{start.month}/{start.day}/", exist_ok=True) - save_result = args[2].save_video_to_file(url=row[1], filename=fname) + fname = f"videos/{camera_device_id}/{start.year}/{start.month}/{start.day}/{camera_device_id}_{start}-{end}.mp4" + + + + save_result = cam.save_video_to_file(url=row[1], filename=fname) match save_result['response_http_status']: case 200: @@ -56,7 +68,7 @@ def download(args): if __name__ == '__main__': - pool = Pool(16) + pool = Pool(64) print("starting up...") @@ -96,32 +108,39 @@ if __name__ == '__main__': een.get_list_of_cameras() - r2d2 = een.cameras[3] - # r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*1), end_timestamp=een.time_now()) - - sql = '''SELECT - download.id, - download.url, - camera.device_id, - download.start_timestamp, - download.end_timestamp, - download.status - FROM - camera - JOIN download ON download.camera_id = camera.id - JOIN USER ON camera.user_id = user.id - WHERE - download.status == ? - AND camera.id == ? - LIMIT 2000;''' + # iterate through all the cameras this user has access to + for current_camera in een.cameras: + + # create directories if they don't already exists + os.makedirs(f"videos/{current_camera.id}/", exist_ok=True) - results = cur.execute(sql, ('new', 4)) + # r2d2.get_list_of_videos(start_timestamp=een.time_before(hours=24*1), end_timestamp=een.time_now()) - + sql = '''SELECT + download.id, + download.url, + camera.device_id, + download.start_timestamp, + download.end_timestamp, + download.status + FROM + camera + JOIN download ON download.camera_id = camera.id + JOIN USER ON camera.user_id = user.id + WHERE + download.status == ? + AND camera.device_id == ? + ORDER BY download.start_timestamp + LIMIT 10000;''' - run([results, een, r2d2]) + + results = cur.execute(sql, ('new', current_camera.id)) + + + + run([results, een, current_camera])