---
title: "DownloadPage"
code-fold: true
execute: 
  enabled: true
  cache: false
jupyter: python3
format:
  html:
    toc: true
    code-fold: true
    html-math-method: katex
    css: styles.css
---

# Miri's requests!

```{python}
#| echo: true

from pathlib import Path
from pytube import YouTube
from pytube import Playlist
import moviepy.editor as mp
from youtube_transcript_api import YouTubeTranscriptApi
from pathvalidate import sanitize_filepath

def convert_mp4_to_mp3(file,oppath):
    try:
    # Load the video file
        #video = mp.VideoFileClip(file)
        audio=mp.AudioFileClip(str(file))
        stem=str(file.stem)#.replace.rstrip().lstrip()

        oppath=oppath/'mp3'
        oppath.mkdir(parents=True,exist_ok=True)
        audio_path=sanitize_filepath(oppath/f'{stem}.mp3')
        print('audio_path', stem, audio_path)

        # Extract the audio from the video and save it as an MP3 file
        #video.audio.write_audiofile(audio_path, codec='pcm_s16le')
        audio.write_audiofile(str(audio_path),write_logfile=True,verbose=True)#, codec='pcm_s16le')

        # Print a success message
        print(f"Audio extracted and saved as: {audio_path}")
    except Exception as e:
        print(f"Error converting MP4 to MP3: {e}")

def convert_mp4_to_wav(file,oppath):
    try:
    # Load the video file
        #video = mp.VideoFileClip(file)
        audio=mp.AudioFileClip(str(file))
        stem=str(file.stem)#.replace.rstrip().lstrip()
        oppath=oppath/'wav'
        oppath.mkdir(parents=True,exist_ok=True)

        audio_path=sanitize_filepath(oppath/f'{stem}.wav')
        print('audio_path', stem, audio_path)
        # Extract the audio from the video and save it as an MP3 file
        #video.audio.write_audiofile(audio_path, codec='pcm_s16le')
        audio.write_audiofile(str(audio_path),codec='pcm_s16le',write_logfile=False,verbose=True)#, codec='pcm_s16le')

        # Print a success message
        print(f"Audio extracted and saved as: {audio_path}")
    except Exception as e:
        print(f"Error converting MP4 to MP3: {e}")

def download_mp4(video_url,oppath):
    # Get the YouTube object
    yt = YouTube(video_url)

    # Get video title and language
    title = yt.title
   # print (title)

    video_stream = yt.streams.filter(adaptive=True).order_by('abr')
   # print(video_stream)
    fname=sanitize_filepath(oppath/ f'{title}.mp4')
    video_stream.last().download(filename=oppath/ f'{title}.mp4')
    return fname 

def download_songlist(songs,oppath):
    
    for song in songs:
        try:
            file=download_mp4(song,oppath)
            convert_mp4_to_mp3(file,oppath)
        except Exception as e:
            print(f"Error downloading file: {e}")
            continue

def download_playlist(url_playlist,oppath):
    playlist = Playlist(url_playlist)
    ls=[]
    for urls in playlist.video_urls:
        ls.append(urls)

    download_songlist(ls,oppath)
    return 

```

fix pytube issue

```{python}

from pytube import cipher
import logging
import re

logger = logging.getLogger(__name__)

def get_throttling_function_name(js: str) -> str:
    """Extract the name of the function that computes the throttling parameter.

    :param str js:
        The contents of the base.js asset file.
    :rtype: str
    :returns:
        The name of the function used to compute the throttling parameter.
    """
    function_patterns = [
        # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
        # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
        # var Bpa = [iha];
        # ...
        # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
        # Bpa.length || iha("")) }};
        # In the above case, `iha` is the relevant function name
        r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'
        r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
        r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])\([a-z]\)',
    ]
    logger.debug('Finding throttling function name')
    for pattern in function_patterns:
        regex = re.compile(pattern)
        function_match = regex.search(js)
        if function_match:
            logger.debug("finished regex search, matched: %s", pattern)
            if len(function_match.groups()) == 1:
                return function_match.group(1)
            idx = function_match.group(2)
            if idx:
                idx = idx.strip("[]")
                array = re.search(
                    r'var {nfunc}\s*=\s*(\[.+?\]);'.format(
                        nfunc=re.escape(function_match.group(1))),
                    js
                )
                if array:
                    array = array.group(1).strip("[]").split(",")
                    array = [x.strip() for x in array]
                    return array[int(idx)]

    raise RegexMatchError(
        caller="get_throttling_function_name", pattern="multiple"
    )

cipher.get_throttling_function_name = get_throttling_function_name

```

## song list 2024-06-24

-   [Sabrina Carpenter - Please Please Please (Lyric Video)](https://www.youtube.com/watch?v=Yl_thbk40A0)
-   [Sabrina Carpenter - Espresso (Lyrics)](https://www.youtube.com/watch?v=kVC3BSBm2Dc)
-   [Kaash Paige - Love Songs (Lyrics)](https://www.youtube.com/watch?v=7P4q7Oo67-U)
-   [L’AMOUR DE MA VIE (Lyrics)](https://www.youtube.com/watch?v=RhGaivzNHps)
-   [SZA Greatest Hits Full Album 2024](https://www.youtube.com/watch?v=W6kc4k4NmWQ)
-   [Eminem - Just Lose It (Official Music Video)](https://www.youtube.com/watch?v=9dcVOmEQzKA)
-   [Rihanna New Playlist 2023](https://www.youtube.com/watch?v=5o9AQs4GOeA)
-   [Eminem - Houdini (Perfectly Clean)](https://www.youtube.com/watch?v=YMwWZXQ9xQU)
-   [Tyla - Water (Lyrics)](https://www.youtube.com/watch?v=A81ZfezsLcM)

```{python}
p=!pwd
#p[0]
#!tree {p[0]}

musicpath='~/Music/Miri'
!tree -d {musicpath}
```

```{python}

#| echo: false
#| output: false
#| eval: false

oppath=Path('/home/ys/Music/Miri/7_playlist')

list_20240624=['https://www.youtube.com/watch?v=Yl_thbk40A0',
'https://www.youtube.com/watch?v=kVC3BSBm2Dc',
'https://www.youtube.com/watch?v=7P4q7Oo67-U',
'https://www.youtube.com/watch?v=RhGaivzNHps',
'https://www.youtube.com/watch?v=W6kc4k4NmWQ',
'https://www.youtube.com/watch?v=9dcVOmEQzKA',
'https://www.youtube.com/watch?v=5o9AQs4GOeA',
'https://www.youtube.com/watch?v=YMwWZXQ9xQU',
'https://www.youtube.com/watch?v=A81ZfezsLcM']



list_20240624
download_songlist(list_20240624,oppath)


```

## song list 2024-10-05

- [sza playlist 1](https://youtu.be/455TWGwAObA?si=GZVF3c86L57d8MsF)
- [sza playlist 2](https://youtu.be/IlnxfwtCqLU?si=TjdWp9QbSG2LM2E9) #did not download
- [travis scott clean](https://youtu.be/7tcYOY50yXg?si=2zYcfSbta9WhTzGd)
- [rihanna playlist](https://youtu.be/5o9AQs4GOeA?si=I0YLFJQl0YwEx9AG) #did not download
- [weekend playlist](https://youtu.be/mPXBHFFUgzw?si=g4fhhhza2COvSnLZ) #did not download
- [travis scott clean 2](https://youtu.be/JPo3xwzupbE?si=B32de3hrHuaG283K)
- [power is power clean](https://youtu.be/iFwQcBoljoo?si=cXrK5-RDq77rvWmh)

```{python}

#| echo: false
#| output: false
#| eval: true

oppath=Path('/home/ys/Music/Miri/8_playlist')
oppath.mkdir(parents=True,exist_ok=True)


list_20241005=['https://youtu.be/455TWGwAObA?si=GZVF3c86L57d8MsF',
'https://youtu.be/IlnxfwtCqLU?si=TjdWp9QbSG2LM2E9',
'https://youtu.be/7tcYOY50yXg?si=2zYcfSbta9WhTzGd',
'https://youtu.be/5o9AQs4GOeA?si=I0YLFJQl0YwEx9AG)',
'https://youtu.be/mPXBHFFUgzw?si=g4fhhhza2COvSnLZ',
'https://youtu.be/JPo3xwzupbE?si=B32de3hrHuaG283K',
'https://youtu.be/iFwQcBoljoo?si=cXrK5-RDq77rvWmh'
]

download_songlist(list_20241005,oppath)


```

# My downloads
## song list 2024-07-13

-   [Vicente Amigo - De Mi Corazón Al Aire](https://www.youtube.com/watch?v=6RZwLta9MHg&list=OLAK5uy_lHANtM_kWgkJVkbl8w83LuIyL8QIIBMWE&index=1)

-   [Vicente Amigo - Vivencias Imaginadas](https://www.youtube.com/watch?v=cvrg4wn7vlQ&list=OLAK5uy_nl4MMy9w40xh1g4KPGbZfB7bs9_ga6Mho&index=2)

-   [Vicente Amigo - Ciudad De Las Ideas](https://www.youtube.com/watch?v=QyBNAtr44X8&list=PLlf0nWXS9c9CRKpqe08BVKtzO7FHXW6sF)

```{python}

#| echo: false
#| output: false
#| eval: false

oppath=Path('/home/ys/Music/youtube')/'Vicente Amigo'/'De Mi Corazon Al Aire'
oppath.mkdir(parents=True,exist_ok=True)
#download_playlist('https://www.youtube.com/watch?v=6RZwLta9MHg&list=OLAK5uy_lHANtM_kWgkJVkbl8w83LuIyL8QIIBMWE&index=1',oppath)



oppath=Path('/home/ys/Music/youtube')/'Vicente Amigo'/'Vivencias Imaginadas'
oppath.mkdir(parents=True,exist_ok=True)
#download_playlist('https://www.youtube.com/watch?v=cvrg4wn7vlQ&list=OLAK5uy_nl4MMy9w40xh1g4KPGbZfB7bs9_ga6Mho&index=1',oppath)

oppath=Path('/home/ys/Music/youtube')/'Vicente Amigo'/'Ciudad De Las Ideas'
oppath.mkdir(parents=True,exist_ok=True)
#download_playlist('https://www.youtube.com/watch?v=QyBNAtr44X8&list=PLlf0nWXS9c9CRKpqe08BVKtzO7FHXW6sF',oppath)

```


