import yt_dlp
import requests
from dotenv import load_dotenv
import os


load_dotenv()

def download_video(url):
    ydl_opts = {
        'format': 'bestvideo+bestaudio',
        'outtmpl': '/downloads/%(title)s.%(ext)s',
        'noplaylist': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
      
        video_title = info_dict.get("title", "video")
        video_ext = info_dict.get("ext", "mp4")
        video_path = f'/downloads/{video_title}.{video_ext}'
        return video_path

def upload_video_to_mixdrop(file_path):
    # Get email and API key from environment variables
    email = os.getenv("MIXDROP_EMAIL")
    api_key = os.getenv("MIXDROP_API_KEY")
    
    if not email or not api_key:
        print("Error: Mixdrop email or API key not set.")
        return
    
    url = "https://ul.mixdrop.ag/api"

    with open(file_path, 'rb') as file:
        files = {'file': file}
        data = {'email': email, 'api_key': api_key}
        response = requests.post(url, files=files, data=data)
    
    if response.status_code != 200:
        print("Upload Failed!")
    else:
        print(response.json()) 


video_url = input('Enter the YouTube URL: ')
video_path = download_video(video_url)
upload_video_to_mixdrop(video_path)