def download_file(job_id, url, filename, already_processed): print(job_id, url, filename, "xxxxxxxxxxxxx") db_init = DBConnection() file_mode = 'wb' if already_processed == 0 else 'ab' response = requests.get(url, stream=True) total = response.headers.get('content-length') content_type = response.headers.get('Content-Type') file_extension = mimetypes.guess_extension(content_type) write_file_path = filename + file_extension is_break = False with open(write_file_path, file_mode) as f: if total is None: f.write(response.content) else: downloaded = 0 total = int(total) for data in response.iter_content( chunk_size=max(int(total / 1000), 1024 * 1024)): downloaded += len(data) update_query = {} update_query["job_id"] = job_id result = db_init.get_job({'job_id': job_id}) if result: status = result[0]['status'] if status in ["PAUSE", "STOP"]: update_query["status"] = status is_break = True if downloaded > already_processed: already_processed = 0 else: continue if is_break: db_init.update_job(update_query) break f.write(data) done = int(50 * downloaded / total) print( total, downloaded, ) update_query["total_file_size"] = total update_query["downloaded_size"] = downloaded update_query["remaining_size"] = total - downloaded db_init.update_job(update_query) if not is_break: update_query = {} update_query["job_id"] = job_id update_query["end_time"] = datetime.utcnow() update_query["status"] = 'COMPLETED' update_query["command"] = "Finished Download" db_init.close()
def get(self): db_init = DBConnection() job_id = request.args.get('id', None) state = request.args.get('state', None) update_query = {} update_query["status"] = state update_query["job_id"] = job_id if state in ['PAUSE', 'STOP', 'RESUME']: db_init.update_job(update_query) update_query["message"] = 'updated download status' if state == 'RESUME': message_to_publish = {} message_to_publish['job_id'] = job_id message_to_publish['status'] = 'RESUME' message_publisher(message_to_publish) else: update_query["message"] = "no contol found" db_init.close() return jsonify(update_query)