Skip to content

Instantly share code, notes, and snippets.

@Hio-Been
Last active December 9, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hio-Been/d301d3c51b84c23ec7483dbf05126b13 to your computer and use it in GitHub Desktop.
Save Hio-Been/d301d3c51b84c23ec7483dbf05126b13 to your computer and use it in GitHub Desktop.
Python script for automatic mp3 download from Youtube clip
songnames = [ 'Anne-Marie/2002',
'Aaron Sheer/Searching',
'The Clash/Should I Stay Or Should I Go',
'Jessie Reyez/Figures, a Reprise (Feat. Daniel Caesar)',
'Usher/Love In This Club (Feat. Young Jeezy)',
"Jason Mraz/I'm Yours",
"Jason Mraz/I Won't Give Up",
"Justin Bieber/Love Yourself",
"Anne-Marie/FRIENDS",
"The Chainsmokers/Closer (Feat.Halsey)",
"Goody Grace/Two Shots (Feat. gnash)",
"The Chainsmokers/Paris",
"Lukas Graham/Love Someone",
"Lukas Graham/Drunk In the Morning",
"Eminem/Lose Yourself",
"Eminem/Stan",
"Billie Eilish/bad guy",
"Pink/Just Give Me A Reason (Feat. Nate Ruess)",
"Kendrick Lamar/LOVE. (Feat. Zacari)",
"Ledisi/Lose Control",
"Ariana Grande/Best Mistake",
"Dua Lipa/Don't Start Now"
"Ariana Grande/Focus (Feat. Jamie Foxx)"
"Estelle/American Boy"
"Big Sean/One Man Can Change The World (Feat. Kanye West)"
]
from selenium import webdriver
import requests, time
import urllib.request
def hb_download( url, file_fullname, chunk_size = 1024 ):
r = requests.get(mp3_url, stream = True)
with open(file_fullname, "wb") as file:
for block in r.iter_content(chunk_size=chunk_size):
if block: file.write(block)
""" Part 1. Set Environment """
path = '/Users/hiobeen/Desktop/pytube_example/'
print('Step 1. Initializing Chrome driver ... path: [%s]'%(path+'chromedriver'))
driver = webdriver.Chrome(path+'chromedriver')
# Pause time (unit: second)
pause_short = 3
pause_long = 30
for input_songname in songnames:
""" Part 2. Get Youtube URL """
artist, song = input_songname.split('/')
search_query= '%s %s'%(artist, song)
print('Step 2. Searching Youtube for [%s]'%search_query)
search_url = 'https://www.youtube.com/results?search_query=%s+lyrics'%search_query
driver.get(search_url)
time.sleep(pause_short)
em = driver.find_element_by_xpath('//*[@id="video-title"]')
youtube_url = em.get_property('href')
""" Part 3. Request convert """
print('Step 3. Requesting mp3 url ...')
convert_url = 'https://www.onlinevideoconverter.com/ko/youtube-to-mp3'
driver.get(convert_url)
time.sleep(pause_short)
url_input = driver.find_element_by_name("texturl")
url_input.send_keys(youtube_url)
cvt_button = driver.find_element_by_xpath("//*[@id='convert1']")
cvt_button.click()
time.sleep(pause_long)
""" Part 4. Get mp3 file url """
print('Step 4. mp3 file url -> [%s]'%mp3_url)
download_button = driver.find_element_by_xpath("//*[@id='downloadq']")
time.sleep(pause_short)
mp3_url = download_button.get_property('href')
""" Part 5. Write mp3 file """
fname_save = '%s%s - %s.mp3'%(path+'downloads/', artist, song)
print('Step 5. File downloading ... [%s]'%fname_save)
hb_download(mp3_url, fname_save)
time.sleep(pause_short)
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment