-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (54 loc) · 2.41 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from pytubefix import Search, YouTube
from pytubefix.cli import on_progress
from pytubefix.exceptions import *
results = Search(input('Enter the search term: '))
vidNumber = 0
options = [] # Create an empty list to hold the selected options
# SSL certificate verify bypass
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
for video in results.videos:
vidNumber += 1
print(f'Option: {vidNumber}')
for attempt in range(5):
try:
print(f'Title: {video.title}')
print(f'Author: {video.author}')
print(f'Duration: {video.length} seconds')
print('---')
except BotDetection:
print('Bot detected, please try again later.')
except LoginRequired:
print("Login required to view video.")
except VideoUnavailable:
print(f'Video {video.video_id} is unavailable, skipping.')
else:
break
else:
print("Yeah something broken af")
while True:
selected_option = input("Enter the number of the video you want to download or 'q' to quit: ") # Ask the user to enter a video option
if selected_option.lower() == 'q':
print("Quitting...")
quit()
elif selected_option.isdigit() and 0 < int(selected_option) <= vidNumber: # Validate user input
selected_option = int(selected_option)
options.append(results.videos[selected_option - 1]) # Append the corresponding video object from results to options list using zero-based indexing
# TODO: Allow user to download multiple videos at once
print("Is this right? (y/n) ") # Ask whether the user's selection is what the user wants
print(f"Title: {options[0].title}")
print(f"Author: {options[0].author}")
confirm = input()
if confirm.lower() == 'y':
break # If the user is satisfied, continue the program
else:
options.pop() # If not, remove the last item from options list
else: # Prompt the user for a valid option if neither 'q' nor a valid option was entered
selected_option = int(input("Invalid Option. Enter the number of the video you want to download again: ")) # Re-request user input if invalid
url = options[0].watch_url
yt = YouTube(url, on_progress_callback = on_progress)
print(yt.title)
# TODO: Allow video to be downloaded in MP4
ys = yt.streams.get_audio_only()
ys.download(mp3=True)
quit()