aboutsummaryrefslogtreecommitdiffstats
path: root/src/youload_playlist.py
blob: 1d62ee5614a9ae2de98a5e789af23e20c6b27155 (plain) (blame)
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
from pytube import Playlist
import os

def make_alpha_numeric(string: str) -> str:
    return "".join(char for char in string if char.isalnum())

class YouLoadPlayList:
    """A class for download and handling youtube playlists"""

    def __init__(self, link: str):
        self.yt_playlist = Playlist(link)
        self.video_count = len(self.yt_playlist.videos)
        self.folder_name = make_alpha_numeric(self.yt_playlist.title)

    def generate_video_info(self, video_num: int) -> str:
        """Returns information on video in playlist at 'video_num'"""

        video = self.yt_playlist.videos[video_num]
        video_size = video.streams.get_highest_resolution().filesize // 1048576
        return f"Title: {video.title}, Size: {video_size} MB"

    def prepare_for_download(self) -> None:
        """Gets the playlist ready for download. Creates the output folder and that stuff."""
        os.mkdir(self.folder_name)

    def download_video(self, video_num: int) -> str:
        """Download video at 'video_num'"""
        video = self.yt_playlist.videos[video_num]
        video_size = video.streams.get_highest_resolution().filesize
        video.streams.get_highest_resolution().download(output_path=self.folder_name)

        return f"Title: {video.title}, Size: {video_size // (1024 ** 2)} MB"