Last active
May 31, 2020 22:43
-
-
Save tthn0/51b150bd47238587082a56775d46d686 to your computer and use it in GitHub Desktop.
A music downloader (using YouTube link) in python. Note: This was made for Mac OS with Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
usage: | |
$ python3 music.py <link> | |
example: | |
$ python3 music.py https://www.youtube.com/watch?v=dQw4w9WgXcQ | |
""" | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium import webdriver | |
from time import sleep | |
from tqdm import tqdm | |
import requests, sys, os | |
# Define Colors (just for the aesthetic, purely optional) | |
end='\033[0m' | |
green='\033[32m' | |
cyan='\033[36m' | |
darkgrey='\033[90m' | |
yellow='\033[93m' | |
# Function to typewrite a string (just for the aesthetic, purely optional) | |
def say(string): | |
for char in string: | |
sys.stdout.write(char) | |
sys.stdout.flush() | |
sleep(.025) | |
# Song name input | |
print(green + "<enter song name>" + end) | |
name = input(darkgrey + "$ " + cyan) | |
say(end + "\ndownload in progress...") | |
# Start selenium | |
driver = webdriver.Safari() # You can change this to Chrome | |
driver.get('https://ytmp3.cc/en13/') | |
# Input link | |
input = driver.find_element_by_xpath('//*[@id="input"]') | |
input.send_keys(str(sys.argv[1]), Keys.ENTER) | |
# Wait until anchor tag is on screen | |
try: | |
a = WebDriverWait(driver, 10).until( | |
EC.presence_of_element_located((By.XPATH, '//*[@id="buttons"]/a[1]')) | |
) | |
except: | |
driver.quit() | |
# Store the href of the anchor tag | |
dl = a.get_attribute("href") | |
# Repeat until href has a link | |
while dl == "": | |
dl = a.get_attribute("href") | |
sleep(.1) | |
# Close Safari | |
os.system('killAll Safari') # (Only works in Bash Shell on Mac) | |
# Use requests to download | |
r = requests.get(dl, stream = True, allow_redirects=True) | |
chunk_size = 1024 | |
total_size = int(r.headers['content-length']) | |
# Load bar | |
print(darkgrey) | |
with open(name + '.mp3', 'wb') as f: | |
for data in tqdm(iterable = r.iter_content(chunk_size = chunk_size), total = total_size/chunk_size, unit = 'KB'): | |
f.write(data) | |
# Print Success Message | |
print(yellow + 'download successful!\n' + end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment