video_downloader_service/src/core/ydl.py

37 lines
1.4 KiB
Python
Raw Normal View History

2023-08-12 13:06:41 +03:00
from __future__ import unicode_literals
2023-08-14 17:47:15 +03:00
import os
2023-08-12 13:06:41 +03:00
from yt_dlp import YoutubeDL
2023-08-12 13:06:41 +03:00
class VideoDownloader:
2023-08-14 17:47:15 +03:00
SUPPORTING_WEBSITES = [
"ok.ru", "vk.com", "www.youtube.com", "livejournal.com"
2023-08-14 17:47:15 +03:00
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DOWNLOAD_DIR = os.path.join(BASE_DIR, "downloads")
BASE_YOUTUBE_DIR = os.path.join(BASE_DOWNLOAD_DIR, "Youtube")
2023-08-12 13:06:41 +03:00
def __init__(self, link: str, ydl_opts: dict = None, username: str = None, password: str = None):
self.link = link
self.ydl_opts = ydl_opts
self.username = username
self.password = password
2023-09-22 13:35:00 +03:00
self.info = None
2023-08-12 13:06:41 +03:00
def get_info(self):
with YoutubeDL(self.ydl_opts if self.ydl_opts else {}) as ydl:
2023-09-22 13:35:00 +03:00
self.info = ydl.extract_info(self.link, download=False)
2023-08-12 13:06:41 +03:00
def download(self):
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
base_download_dir = os.path.join(base, os.pardir, "downloads", self.info['extractor_key'])
for root, dirs, files in os.walk(base_download_dir):
for file in files:
if file.find(self.info['id']) != -1 and file.find('.part') != -1:
os.remove(base_download_dir + f"/{file}")
with YoutubeDL(self.ydl_opts if self.ydl_opts else {}) as ydl:
2023-08-12 13:06:41 +03:00
ydl.download([self.link])
2023-09-22 13:35:00 +03:00
return self.info