Blog Post
عنوان المحادثة: يعني سكريبت بسيط يحمل الفيديوهات من لينك مباشر ثم يرفعها جوجل درايف
التاريخ: 22.10.2025
التصنيف: 🎮 الألعاب والرسوميات والوسائط
إجمالي الرسائل: 3 | ياسر: 2 | M: 1
M
import osimport pickleimport yt_dlpfrom googleapiclient.discovery import buildfrom googleapiclient.http import MediaFileUpload, MediaIoBaseUploadfrom google_auth_oauthlib.flow import InstalledAppFlowfrom google.auth.transport.requests import RequestSCOPES = ['https://www.googleapis.com/auth/drive.file']def authenticate_drive(): creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file('credintionals.json', SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) return servicedef upload_file_to_drive(file_path): service = authenticate_drive() file_size = os.path.getsize(file_path) size_mb = file_size / (1024 * 1024) print(f"📦 حجم الملف: {size_mb:.2f} MB") if size_mb > 1024: print("⚠️ الملف حجمه كبير جدًا! تأكدي إن الإنترنت عندك قوي.") file_metadata = { 'name': os.path.basename(file_path) } media = MediaFileUpload(file_path, resumable=True) try: request = service.files().create(body=file_metadata, media_body=media, fields='id') response = None while response is None: status, response = request.next_chunk() if status: print(f"⬆️ تم الرفع: {int(status.progress() * 100)}%") print(f"✅ تم رفع الملف بنجاح! ID: {response.get('id')}") return response.get('id') except Exception as e: print(f"❌ خطأ أثناء رفع الملف: {e}") return Nonedef download_video(url, output_path="downloads/"): ydl_opts = { 'outtmpl': f'{output_path}%(title)s.%(ext)s', 'format': 'bestvideo+bestaudio/best', 'merge_output_format': 'mp4' } os.makedirs(output_path, exist_ok=True) with yt_dlp.YoutubeDL(ydl_opts) as ydl: try: info = ydl.extract_info(url, download=True) video_filename = ydl.prepare_filename(info).replace(".webm", ".mp4").replace(".mkv", ".mp4") print(f"✅ تم تحميل الفيديو: {video_filename}") return video_filename except Exception as e: print(f"❌ خطأ في تحميل الفيديو: {e}") return Nonedef download_and_upload_video(url): video_path = download_video(url) if video_path and os.path.exists(video_path): upload_file_to_drive(video_path) else: print("❌ لم يتم العثور على الفيديو لتحميله.")# مثال تشغيل:download_and_upload_video("https://www.dailymotion.com/video/x9j6s4i")
22.10.2025 01:23