-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3-downloader.py
56 lines (50 loc) · 1.71 KB
/
mp3-downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from bs4 import BeautifulSoup
from requests_html import HTMLSession
from pathlib import Path
import youtube_dl
import requests
import pandas
import os
def DownloadVideosFromTitles(titles):
id_list = []
for index, title in enumerate(titles):
vid_id = ScrapeVidId(title)
id_list += [vid_id] # try with .append()
print("Downloading video ", title, " ", index, " of ", len(titles))
DownloadVideosFromIds(id_list)
def ScrapeVidId(search_query):
print("Getting video id for: ", search_query)
BASIC='https://www.youtube.com/results?search_query='
URL = (BASIC + search_query)
URL.replace(' ', '+')
page = requests.get(URL)
session = HTMLSession()
response = session.get(URL)
response.html.render(sleep=1)
soup = BeautifulSoup(response.html.html, 'html.parser')
results = soup.find('a', id='video-title')
return results['href'].split('/watch?v=')[1]
def DownloadVideosFromIds(ids):
SAVE_PATH = str(os.path.join(Path.home(), 'Downloads/songs'))
try:
os.mkdir(SAVE_PATH)
print("Directory ", SAVE_PATH, " created")
except FileExistsError: #check this
print("Directory ", SAVE_PATH, " already exists")
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': SAVE_PATH + '/%(title)s.%(ext)s', #CHECK THIS
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(ids)
def __main__():
data = pandas.read_csv('songs.csv')
data = data['colummn'].tolist()
print("Found ", len(data), " songs!")
DownloadVideosFromTitles(data[0:1])
__main__()