mobenta commited on
Commit
18bdf4a
·
verified ·
1 Parent(s): de850b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import subprocess
2
  import sys
 
 
3
 
4
  # Ensure compatible versions of httpx and httpcore are installed
5
  subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
@@ -10,10 +12,21 @@ import re
10
  import yt_dlp
11
  import logging
12
  import os
 
13
 
14
  # Configure logging for debugging purposes
15
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
16
 
 
 
 
 
 
 
 
 
 
 
17
  # Function to search YouTube videos using yt-dlp for better reliability
18
  def youtube_search(query, max_results=10):
19
  cookies_file = "cookies.txt" # You need to provide this file with cookies exported from YouTube
@@ -23,6 +36,10 @@ def youtube_search(query, max_results=10):
23
  'simulate': True,
24
  'noplaylist': True, # Avoid extracting playlists
25
  'format': 'best',
 
 
 
 
26
  }
27
 
28
  if os.path.exists(cookies_file):
@@ -33,6 +50,9 @@ def youtube_search(query, max_results=10):
33
  logging.debug(f"Starting YouTube search for query: {query}")
34
 
35
  try:
 
 
 
36
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
37
  result = ydl.extract_info(search_url, download=False)
38
  gallery_items = []
 
1
  import subprocess
2
  import sys
3
+ import random
4
+ import time
5
 
6
  # Ensure compatible versions of httpx and httpcore are installed
7
  subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
 
12
  import yt_dlp
13
  import logging
14
  import os
15
+ from fake_useragent import UserAgent
16
 
17
  # Configure logging for debugging purposes
18
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
19
 
20
+ # Set up User-Agent rotation
21
+ ua = UserAgent()
22
+
23
+ # Define a list of proxies (example placeholders, replace with your working proxies)
24
+ PROXIES = [
25
+ "http://proxy1.example.com:8080",
26
+ "http://proxy2.example.com:8080",
27
+ "http://proxy3.example.com:8080",
28
+ ]
29
+
30
  # Function to search YouTube videos using yt-dlp for better reliability
31
  def youtube_search(query, max_results=10):
32
  cookies_file = "cookies.txt" # You need to provide this file with cookies exported from YouTube
 
36
  'simulate': True,
37
  'noplaylist': True, # Avoid extracting playlists
38
  'format': 'best',
39
+ 'proxy': random.choice(PROXIES), # Randomly choose a proxy for each request
40
+ 'http_headers': {
41
+ 'User-Agent': ua.random # Rotate user agents to make requests appear less like a bot
42
+ },
43
  }
44
 
45
  if os.path.exists(cookies_file):
 
50
  logging.debug(f"Starting YouTube search for query: {query}")
51
 
52
  try:
53
+ # Introduce a random delay to avoid rate-limiting issues
54
+ time.sleep(random.uniform(2, 5))
55
+
56
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
57
  result = ydl.extract_info(search_url, download=False)
58
  gallery_items = []