העלאת קבצים גדולים בapi
-
בגלל המגבלה של 50 מ''ב ההעלאה לapi - עשיתי את הפיצול של הקבצים כדי שמעלה גם קבצים גדולים.
משום מה זה עובד לי רק בקבצים של 70-80 מ''ב,
אבל קבצים גדולים יותר הוא מעלה לשלוחה קובץ תקול.זה הקוד שעשיתי, אשמח מאוד אם מישהו יוכל לבדוק אם יש לי כאן טעות
def upload_to_ymot(file_path):
file_size = os.path.getsize(file_path)if file_size <= 50 * 1024 * 1024: # 🔹 העלאה רגילה with open(file_path, "rb") as f: files = {"file": (os.path.basename(file_path), f, "audio/wav")} data = { "token": YMOT_TOKEN, "path": YMOT_PATH, "convertAudio": "1", "autoNumbering": "true" } response = requests.post(UPLOAD_URL, data=data, files=files) print("📞 תגובת ימות:", response.text) else: # 🔹 Chunk Upload qquuid = str(uuid.uuid4()) total_parts = math.ceil(file_size / CHUNK_SIZE) filename = os.path.basename(file_path) with open(file_path, "rb") as f: for part_index in range(total_parts): chunk = f.read(CHUNK_SIZE) byte_offset = part_index * CHUNK_SIZE files = {"qqfile": (filename, chunk, "application/octet-stream")} data = { "token": YMOT_TOKEN, "path": YMOT_PATH, "convertAudio": "1", "autoNumbering": "true", "qquuid": qquuid, "qqpartindex": part_index, # מתחיל מ-0 "qqpartbyteoffset": byte_offset, "qqchunksize": len(chunk), "qqtotalparts": total_parts, # סה"כ חלקים (לא פחות 1) "qqtotalfilesize": file_size, "qqfilename": filename, "uploader": "yemot-admin" } response = requests.post(UPLOAD_URL, data=data, files=files) print(f"⬆️ חלק {part_index+1}/{total_parts} הועלה:", response.text) # 🔹 בקשת סיום data = { "token": YMOT_TOKEN, "path": YMOT_PATH, "convertAudio": "1", "autoNumbering": "true", "qquuid": qquuid, "qqfilename": filename, "qqtotalfilesize": file_size, "qqtotalparts": total_parts } response = requests.post(UPLOAD_URL + "?done", data=data) print("✅ סיום העלאה:", response.text) -
לפי הלוג זה נראה שהבעיה שלי היא בבקשת סיום.
יש פה מישהו שיודע לומר מה לא כתוב לי נכון? -
@שלמה-צובל מצורף קוד תקין
import json import requests import os from uuid import uuid4 def read_in_chunks(file, chunk_size=5000000): while True: data = file.read(chunk_size) if not data: break yield data def upload_file(file_path, path, token_yemot): if os.path.getsize(file_path) > 5000000: content_name = str(os.path.basename(file_path)) content_path = os.path.abspath(file_path) content_size = os.stat(content_path).st_size qquuid = str(uuid4()) file = open(content_path, 'rb') chunks = [chunk for chunk in read_in_chunks(file)] file.close() offset = 0 for chunk in chunks: data = { 'token': token_yemot, 'path': path, 'qquuid': qquuid, 'convertAudio': 0, 'autoNumbering': 'true', 'uploader': 'yemot-admin', 'qqfilename': content_name, 'qqtotalfilesize': content_size, 'qqtotalparts': len(chunks), 'qqchunksize': len(chunk), 'qqpartbyteoffset': offset, 'qqpartindex': chunks.index(chunk), } files = { 'qqfile': chunk, } res = requests.post('https://www.call2all.co.il/ym/api/UploadFile', data=data, files=files, ) offset += len(chunk) response = requests.post('https://www.call2all.co.il/ym/api/UploadFile?done',{ 'token': token_yemot, 'path': path, 'uploader': 'yemot-admin', 'convertAudio': 0, 'autoNumbering': 'true', 'qquuid': qquuid, 'qqfilename': content_name, 'qqtotalfilesize': content_size, 'qqtotalparts': len(chunks), }) responses = response.text.split("}{") for idx, response in enumerate(responses): if idx < len(responses) - 1: response = f"{response}" else: response = f"{response}" try: data = json.loads(response) print(response.json()) return data except json.JSONDecodeError as e: print("JSON decoding error:", e) else: url = 'https://www.call2all.co.il/ym/api/UploadFile' params = { 'token': token_yemot, 'path': path, 'convertAudio': int(1), 'autoNumbering': int(1), } files = {'file': open(file_path, 'rb')} response = requests.post(url, params=params, files=files) print(response.json()) return response.json() -
@צדיק-תמים
עובד!
תודה רבה!
שלום! נראה שהשיחה הזו מעניינת אותך, אבל עדיין אין לך חשבון.
נמאס לכם לגלול בין אותם הפוסטים בכל ביקור? כשנרשמים לחשבון, תמיד תחזרו בדיוק למקום שבו הייתם קודם, ותוכלו לבחור לקבל התראות על תגובות חדשות (בין אם במייל, ובין אם בהתראת פוש). תוכלו גם לשמור סימניות ולפרגן ב-upvote לפוסטים כדי להביע הערכה לחברי קהילה אחרים.
בעזרת התרומה שלך, הפוסט הזה יכול להיות אפילו טוב יותר 💗
הרשמה התחברות