Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Nov 29, 2024
1 parent ec8276d commit 47da507
Showing 1 changed file with 96 additions and 29 deletions.
125 changes: 96 additions & 29 deletions objects/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
import urllib.request
from datetime import datetime, timedelta
import http.client
http.client.HTTPConnection.debuglevel = 1
import socket

http.client.HTTPConnection.debuglevel = 0 # Set to 1 for verbose HTTP output

# Define the proxy list once at the top
PROXIES = [
'http://44.218.183.55:80',
'http://44.195.247.145:80',
'http://160.86.242.23:8080',
'http://116.203.135.164:8090',
'http://35.215.216.90:80',
]

# Function to ensure pytube is installed
def ensure_pytube_installed():
Expand Down Expand Up @@ -68,31 +78,43 @@ def patched_get_throttling_function_name(js: str) -> str:

# Add a User-Agent header to urllib requests
def add_user_agent():
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
user_agent = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/94.0.4606.61 Safari/537.36")
headers = [
("User-Agent", user_agent),
("Accept-Language", "en-US,en;q=0.9"),
# ("Accept-Encoding", "gzip, deflate, br"),
("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
("Accept", "*/*"),
("Connection", "keep-alive"),
("Upgrade-Insecure-Requests", "1"),
]
opener = urllib.request.build_opener()
opener.addheaders = headers
urllib.request.install_opener(opener)


# Ensure User-Agent is applied
add_user_agent()

def set_proxy(proxy_url):
proxy_handler = urllib.request.ProxyHandler({'http': proxy_url, 'https': proxy_url})
opener = urllib.request.build_opener(proxy_handler)
opener.addheaders = urllib.request.build_opener().addheaders # Keep existing headers
urllib.request.install_opener(opener)
# Also set the proxy for pytube
pytube.request.default_proxy = proxy_url

def reset_proxy():
# Remove the proxy settings
urllib.request.install_opener(urllib.request.build_opener())
pytube.request.default_proxy = None

def log_system_details():
print("Logging system details:")
print(f"Python version: {sys.version}")
print(f"Pytube version: {pytube.__version__}")
print(f"SSL version: {ssl.OPENSSL_VERSION}")
print(f"System platform: {sys.platform}")

def save_progress(stream, bytes_remaining, folder):
def save_progress(stream, chunk, bytes_remaining, folder):
try:
total_size = stream.filesize
downloaded = total_size - bytes_remaining
Expand All @@ -108,7 +130,6 @@ def save_progress(stream, bytes_remaining, folder):
print(f"Progress saved to '{progress_file_path}'.")
except Exception as e:
print(f"Error saving progress: {e}")
raise

def clean_old_folders(base_folder, days=7):
"""Delete folders older than a specified number of days."""
Expand Down Expand Up @@ -159,7 +180,6 @@ def get_metadata_safe(yt):

return metadata


def save_metadata(yt, folder):
"""Save metadata with fallback."""
try:
Expand All @@ -170,9 +190,10 @@ def save_metadata(yt, folder):
with open(metadata_file_path, "w") as meta_file:
json.dump(metadata, meta_file, indent=4)
print(f"Metadata saved successfully to '{metadata_file_path}'.")
return True
except Exception as e:
print(f"Error saving metadata: {e}")

return False

def save_thumbnail(yt, folder):
"""Save the highest resolution thumbnail available, with fallback handling."""
Expand All @@ -185,44 +206,64 @@ def save_thumbnail(yt, folder):
f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg", # Medium quality
yt.thumbnail_url if hasattr(yt, "thumbnail_url") else None # Default thumbnail
]

thumbnail_urls = [url for url in thumbnail_urls if url] # Remove None entries
thumbnail_path = os.path.join(folder, "thumbs.jpg")
os.makedirs(folder, exist_ok=True)

for url in thumbnail_urls:
try:
urllib.request.urlretrieve(url, thumbnail_path)
print(f"Thumbnail downloaded successfully to '{thumbnail_path}' from URL: {url}")
return # Exit the loop on success
return True # Exit the loop on success
except Exception as e:
print(f"Failed to download thumbnail from '{url}': {e}")

print(f"Could not download any thumbnails for video '{yt.title}'.")
return False
except Exception as e:
print(f"Error in save_thumbnail: {e}")

return False

def download_video(yt, folder):
"""Download the video at the highest resolution, with fallback."""
try:
video_stream = yt.streams.get_highest_resolution()
if video_stream is None:
print("No streams available to download.")
return
return False
print(f"Selected video stream: {video_stream}")
video_path = os.path.join(folder, "video.mp4")
yt.register_on_progress_callback(
lambda stream, chunk, bytes_remaining: save_progress(stream, bytes_remaining, folder)
lambda stream, chunk, bytes_remaining: save_progress(stream, chunk, bytes_remaining, folder)
)
video_stream.download(output_path=folder, filename="video.mp4")
print(f"Video downloaded successfully to '{video_path}'.")
return True
except Exception as e:
print(f"Error downloading video: {e}")
import traceback
traceback.print_exc()


return False

def attempt_with_proxies(function, *args):
# First attempt without proxy
reset_proxy()
success = function(*args)
if success:
return True
else:
for proxy in PROXIES:
print(f"Retrying with proxy {proxy}")
try:
set_proxy(proxy)
success = function(*args)
if success:
return True
except Exception as e:
print(f"Failed with proxy {proxy}: {e}")
finally:
reset_proxy()
print("All proxies failed.")
return False

def main():
if len(sys.argv) < 3:
Expand All @@ -242,21 +283,47 @@ def main():
add_user_agent() # Ensure all requests include a user-agent
log_system_details() # Log environment details
print(f"Attempting to access YouTube video: {url}")
yt = YouTube(url)

yt = None
# Attempt to create YouTube object, retrying with proxies if necessary
success = False
# First attempt without proxy
reset_proxy()
try:
yt = YouTube(url)
success = True
except Exception as e:
print(f"Failed to create YouTube object without proxy: {e}")
if not success:
for proxy in PROXIES:
print(f"Retrying with proxy {proxy}")
try:
set_proxy(proxy)
yt = YouTube(url)
print(f"Successfully created YouTube object with proxy {proxy}")
success = True
break
except Exception as e:
print(f"Failed to create YouTube object with proxy {proxy}: {e}")
finally:
reset_proxy()
if not success:
print("Failed to access YouTube video with all proxies.")
sys.exit(1)

if action == "metadata":
save_metadata(yt, folder_name)
attempt_with_proxies(save_metadata, yt, folder_name)
elif action == "thumbnail":
save_thumbnail(yt, folder_name)
attempt_with_proxies(save_thumbnail, yt, folder_name)
elif action == "video":
download_video(yt, folder_name)
attempt_with_proxies(download_video, yt, folder_name)
elif action == "all":
save_metadata(yt, folder_name)
save_thumbnail(yt, folder_name)
download_video(yt, folder_name)
attempt_with_proxies(save_metadata, yt, folder_name)
attempt_with_proxies(save_thumbnail, yt, folder_name)
attempt_with_proxies(download_video, yt, folder_name)
else:
print("Invalid action specified. Use 'metadata', 'thumbnail', 'video', or 'all'.")

clean_old_folders(base_folder)
except Exception as e:
print(f"Error encountered during processing: {e}")
Expand Down

0 comments on commit 47da507

Please sign in to comment.