Skip to content

Instantly share code, notes, and snippets.

@swapnildavangave
Created April 5, 2019 12:02
Show Gist options
  • Save swapnildavangave/742000240cc4e9ffd877e577645c9510 to your computer and use it in GitHub Desktop.
Save swapnildavangave/742000240cc4e9ffd877e577645c9510 to your computer and use it in GitHub Desktop.
Search and download mp3 using youtubedl.
#!/usr/bin/python3
import subprocess as sbp
import requests
from bs4 import BeautifulSoup
import argparse
command_list = ["python3",
"-m","youtube_dl", "--restrict-filenames" ,
"--ignore-errors", "-x", "--audio-format", "mp3"]
def download(tube_hash: str):
command_list.append(tube_hash)
sbp.run(command_list, capture_output=True)
def get_list(search_term):
# "https://www.youtube.com/results?search_query=maki+otsuki"
st = "+".join(search_term.split(" "))
url = "https://www.youtube.com/results?search_query="+st
response = requests.get(url)
soup = BeautifulSoup(response.content)
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
if "list" not in vid['href']:
ve = vid['href']
try:
res = ve.split("v=")[1]
except IndexError:
res = None
pass
download(res)
break
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("search")
args = parser.parse_args()
get_list(search_term=args.search)
"""
Requirements:
Python packages:
1. youtube_dl
2. bs4
3. requests
"""
"""
Usage:
user@host:~$ python3 youtube_to_mp3.py "maki otsuki memories"
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment