File size: 3,216 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import httpx
import asyncio
import aiofiles
import json
from typing import List, Dict, Any, Optional

TEMP_DIR = "./temp"


class YouTubeAPI:
    def __init__(self):
        self.base_url = "https://honox-five.vercel.app"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
        }

    async def search(self, query: str) -> list[dict[str, Any]]:
        """
        Searches for videos based on a query.
        Returns a list of results containing title, id, duration, etc.
        """
        async with httpx.AsyncClient(headers=self.headers, timeout=20.0) as client:
            response = await client.get(f"{self.base_url}/search", params={"q": query})
            response.raise_for_status()
            data = response.json()
            return data.get("results", [])

    async def get_download_info(self, video_id: str) -> dict[str, Any]:
        """
        Retrieves the download metadata and direct URL for a specific video ID.
        """
        async with httpx.AsyncClient(headers=self.headers, timeout=20.0) as client:
            response = await client.get(
                f"{self.base_url}/download", params={"id": video_id}
            )
            response.raise_for_status()
            return response.json()

    async def download(
        self, download_url: str, filename: str, output_folder: str = TEMP_DIR
    ) -> str:
        """
        Downloads the audio file from the provided direct URL.
        """
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        # Clean filename to prevent OS errors
        clean_name = "".join(
            [c for c in filename if c.isalnum() or c in (" ", ".", "_")]
        ).rstrip()
        file_path = os.path.join(output_folder, f"{clean_name}.mp3")

        print(f"[Status] Starting download: {filename}...")

        async with httpx.AsyncClient(headers=self.headers, timeout=None) as client:
            async with client.stream("GET", download_url) as response:
                response.raise_for_status()

                async with aiofiles.open(file_path, mode="wb") as f:
                    async for chunk in response.aiter_bytes(chunk_size=8192):
                        await f.write(chunk)

        print(f"[Success] Downloaded to: {file_path}")
        return file_path

    async def quick_download(self, query: str):
        """
        A helper method that searches for the first result and downloads it immediately.
        """
        # 1. Search
        results = await self.search(query)
        if not results:
            print("No results found.")
            return

        video = results[0]
        video_id = video["id"]
        title = video["title"]

        # 2. Get Direct Link
        info = await self.get_download_info(video_id)
        dl_url = info.get("downloadUrl")

        if dl_url:
            # 3. Download
            await self.download(dl_url, title)
        else:
            print("Failed to retrieve download URL.")


# async def main():
# yt = YouTubeDL()
# r = await yt.search("mehrama")
# print(json.dumps(r, indent=2))

# if __name__ == "__main__":
# asyncio.run(main())