Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
'''
Referencing:
https://pypi.org/project/youtube-search-python/- pip library used to search and download music/videos from youtube
https://github.com/ytdl-org/youtube-dl - documentation for download feature
'''
'''Imports'''
#Searches via youtube API
from youtubesearchpython import VideosSearch # Link to the project : https://pypi.org/project/youtube-search-python/
#Download the video using youtube_dl
import youtube_dl # Link to the project : https://github.com/ytdl-org/youtube-dl
'''Function'''
# Search with youtube API
def video_link_from_name(name):
search_link = VideosSearch(str(name), limit=1)
video_url = search_link.result()["result"][0]["link"]
return video_url
#Download the video using youtube_dl
def download_video(name, link) :
path = "music/" + str(name) + ".mp3"
options = {
"outtmpl": path,
"format":
"bestaudio/best",
"postprocessors":
[{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "320",
}]
}
with youtube_dl.YoutubeDL(options) as mp3:
mp3.download([link])
return path
'''Main'''
#List of key words that will be in the user input
key_words = ["find", "search", "play", "download"]
# Main loop (The first part asks the user for input. If it is not the first time it has run it will ask “Anything else?”.)
i = 0
while True:
# Asks for user input
if i == 0 :
user_input = str(input("""Hey what's up?
Type find/search/play to search for a song on Youtube
or type download to download it directly!
If you want to close the bot type \"stop\"\n""")).lower()
else:
user_input = str(input("Anything else?\n")).lower()
# Stops the loop (If the user types “stop” the loop is stopped.)
if user_input == "stop":
print("Okay bye!")
break
#Gets the first word in the user input (This splits the user input on spaces into a list and selects the first element which is the first word in the input.)
command = user_input.split(" ", 1)[0]
#If the command the user typed is in the known key words, it splits the user input into a list and removes the command in the beginning.
# After that it joins the list to a string again and gets the video link.
if command in key_words:
user_input_list = user_input.split(" ")
# Removes the command from user input
user_input_list.pop(0)
video_name = " ".join(user_input_list)
video_link = video_link_from_name(video_name)
print(f"Direct link to the music: {video_link}")
#If the user used the download command it downloads the music and prints the location it is saved in.
if command == "download":
path = download_video(video_name, video_link)
print(f"Music downloaded at: {path}")
# Adds one to the iterator
i += 1