Week 7 - API Testing
Basic YouTube Downloader
I will add frontend to make this useable on the blog for everyone rather than just in this notebook later.
import json, requests, getpass, sys
class color:
BOLD = '\033[91m'
UNDERLINE = '\033[91m'
END = '\033[0m'
# This doesn't really work except make things red, don't know why
print(color.BOLD + "YouTube Video Downloader with APIs: \n" + color.END)
apiFound = False
while apiFound == False:
print(color.UNDERLINE + "Enter a valid YouTube Video URL or ID:" + color.END)
videoURL = input("Enter a valid YouTube Video URL or ID:")
videoID = videoURL[-11:]
print(getpass.getuser() + ": " + videoURL)
url = "https://ytstream-download-youtube-videos.p.rapidapi.com/dl"
querystring = {"id":videoID}
headers = {
"X-RapidAPI-Key": "e51c67fa22mshec43200baacf5b3p148027jsn10e3e84c629e",
"X-RapidAPI-Host": "ytstream-download-youtube-videos.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
json = response.json()
if json['status'] == "fail":
print(color.BOLD + "\nYou have inputted an invalid YouTube ID, please try again.\n" + color.END)
else:
apiFound = True
formatList = json.get('formats')
qualityFound = False
while qualityFound == False:
print(color.UNDERLINE + "\nAvailable qualities:" + color.END)
i = 2
while i >= 0:
print(formatList[i]['qualityLabel'])
i -= 1
print("Note: 144p is " + color.BOLD + "AUDIO-ONLY" + color.END)
print(color.UNDERLINE + "\nEnter chosen quality (from the list):" + color.END)
chosenQuality = input("Enter chosen quality (from the list)")
print(getpass.getuser() + ": " + chosenQuality)
chosenQuality.lower()
downloadURL = 0
if chosenQuality == formatList[0]['qualityLabel']:
downloadURL = 0
qualityFound = True
elif chosenQuality == formatList[1]['qualityLabel']:
downloadURL = 1
qualityFound = True
elif chosenQuality == formatList[2]['qualityLabel']:
downloadURL = 2
qualityFound = True
else:
print(color.BOLD + "\nThe selected resolution is not available, please try again." + color.END)
print(color.UNDERLINE + "\nDownload link:\n" +formatList[downloadURL]['url'] + color.END + "\n\nTo download, click on the three dots in the bottom-right then press download.")