import random import requests import os import math import sys from concurrent.futures import ThreadPoolExecutor def m_req(data, chunk): url = "https://cdn.cloudkp.com/upload" resp = requests.post(url, data=data, files={'file': chunk}) return resp.text class CloudKP: def __init__(self): self.id = ''.join(str(random.randint(0, 9)) for _ in range(20)) self.up = 0 def do_upload(self, part_index, part_count, file_size, chunk): data = { 'dzuuid': self.id, 'dzchunkindex': part_index, 'dztotalchunkcount': part_count, 'dztotalfilesize': file_size } aa = m_req(data, chunk) self.up += 1 print(f"Uploaded {math.ceil((self.up / part_count) * 100)}%", end='\r') return aa def finish_upload(self, part_count, file_name): data = { 'dzuuid': self.id, 'complete': "yes", 'file_name': file_name, 'dztotalchunkcount': part_count } return m_req(data, None) def read_in_chunks(file_object, chunk_size): while True: data = file_object.read(chunk_size) if not data: break yield data def upload_file(file_path): file_info = os.stat(file_path) file_size = file_info.st_size chunk_size = 131072 if file_size >= 786432000: chunk_size *= 4 elif file_size >= 104857600: chunk_size *= 2 part_count = math.ceil(file_size / chunk_size) part_index = 0 print(f"Uploading {file_size} bytes as {part_count} parts...") cloud_kp = CloudKP() pool = ThreadPoolExecutor(max_workers=5) with open(file_path, 'rb') as f: for chunk in read_in_chunks(f, chunk_size): pool.submit(cloud_kp.do_upload, part_index, part_count, file_size, chunk) part_index += 1 pool.shutdown(wait=True) file_name = os.path.basename(file_path) resp2 = cloud_kp.finish_upload(part_count, file_name) if "error" in resp2: return resp2 else: return "https://cloudkp.com?c="+resp2 if len(sys.argv) > 1: file_path = sys.argv[1] uploaded_file_url = upload_file(file_path) print("File link: ", uploaded_file_url) else: print("usage: python upload.py filename")