<jupyter_start><jupyter_text><jupyter_code>!pip install pytube mega.py
from pytube import YouTube, Playlist
import os, time, datetime
from mega import Mega

mega = Mega()
username = ""
password = ""
m = mega.login(username, password)

def dl_mega(path):
  file = m.upload(f'{os.listdir(path)[0]}')
  return m.get_upload_link(file)


def create_dir(name=None):
  # Directory
  directory = name or input('Enter name of folder: ') 

  # Parent Directory path
  parent_dir = "/content"
    
  # Path
  path = os.path.join(parent_dir, directory)

  # Path existence verification
  if not os.path.exists(path):  
    # Create the directory
    os.mkdir(path)
  
  return path


def display_qualities():
  print('Enter:\n[0] 720p\n[1] 480p\n[2] 360p\n[3] 240p\n[4] 144p')
  num = input('Qualiy: ')
  return num


def download_fn(num):
  res = ["720p", "480p", "360p", "240p", "144p"]

  if num.isdigit() and int(num) <= 4:
    num = int(num)
    begin = time.time()

    # playlist
    if str(url).find('playlist') > 0:
      yt = Playlist(url)
      filesize = 0
      path = create_dir(yt.title)
      for video in yt.videos:
        try:
          stream = video.streams.filter(res=res[num]).first()
          print(f"Downloading selected quality: {res[num]}")
        except:
          stream = video.streams.get_highest_resolution().first()
          print(f"Downloading highest quality")
        filesize += stream.filesize 
        stream.download(path)

    # Single
    else:
      yt = YouTube(url)
      try:
        stream = yt.streams.filter(res=res[num]).first()
        print(f"Downloading selected quality: {res[num]}")
      except:
        stream = yt.streams.get_highest_resolution()
        print(f"Downloading highest quality")

      filesize = stream.filesize
      stream.download(path)

    os.chdir(path)
    print(dl_mega(path))
    
    end = time.time()
    filesize = round(filesize/(1024*1024))

    print("Total size copied: ", filesize, " MB")
    print("Elapsed Time: ",int((end-begin)//60),"min :", int((end-begin)%60), "sec")
    print(datetime.datetime.now())  
    return None

  print('ENTER 0, 1, 2, 3 or 4')
  num = display_qualities()
  download_fn(num)


path = create_dir()

url = input('Video/Playlist URL: ')

num = display_qualities()
download_fn(num)<jupyter_output>Enter name of folder: 
Video/Playlist URL: https://www.youtube.com/playlist?list=PLAYxecbGotUyJwMpFHVikK4cOUkbzWPwi
Enter:
[0] 720p
[1] 480p
[2] 360p
[3] 240p
[4] 144p
Qualiy: 0
Downloading selected quality: 720p
Downloading selected quality: 720p
Downloading selected quality: 720p
https://mega.co.nz/#!ZYRWnTiD!73ArrIUbYxHuorKsd28M1R7xJy5cP5W4EcSFZDCodBg
Total size copied:  74  MB
Elapsed Time:  0 min : 23 sec
2021-11-19 23:31:45.285776
{'fraction_non_alphanumeric': 0.13614852566436111, 'fraction_numerical': 0.044048052420822714, 'mean_word_length': 3.348101265822785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '503032', 'license': 'CC0-1.0', 'n_tokens_mistral': 1113, 'n_tokens_neox': 981, 'n_words': 222, 'path': 'yt2mega_v1_0_1.ipynb', 'repo_name': 'zavorfang/Api', 'size': '2747'}