gnosticdev commited on
Commit
7baf91b
·
verified ·
1 Parent(s): 253d283

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -42
app.py CHANGED
@@ -34,7 +34,7 @@ def load_proxies(proxy_file="proxys.txt"):
34
  with open(proxy_file, 'r') as f:
35
  proxies = [line.strip() for line in f if line.strip()]
36
  print(f"Loaded {len(proxies)} proxies from file")
37
- return [{"http": f"http://{proxy}", "https": f"http://{proxy}"} for proxy in proxies]
38
  except Exception as e:
39
  print(f"Error loading proxies: {e}")
40
  return []
@@ -58,48 +58,51 @@ def search_google_images(query, num_images=1):
58
 
59
  if not proxies:
60
  print("No proxies available, trying without proxy")
61
- proxies = [None]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- for proxy in proxies:
64
- try:
65
- service = build("customsearch", "v1", developerKey=api_key)
66
-
67
- if proxy:
68
- session = requests.Session()
69
- session.proxies = proxy
70
- service._http.http = session
71
- print(f"Trying with proxy: {proxy['http']}")
72
-
73
- result = service.cse().list(
74
- q=query,
75
- cx=cse_id,
76
- searchType="image",
77
- num=num_images * 3,
78
- safe='off',
79
- imgSize='HUGE',
80
- rights='cc_publicdomain|cc_attribute|cc_sharealike'
81
- ).execute()
82
-
83
-
84
- if 'items' in result:
85
- image_urls = []
86
- for item in result.get('items', []):
87
- if 'image' in item:
88
- width = int(item['image'].get('width', 0))
89
- height = int(item['image'].get('height', 0))
90
- if width >= MIN_WIDTH and height >= MIN_HEIGHT:
91
- image_urls.append(item['link'])
92
- if len(image_urls) >= num_images:
93
- break
94
-
95
- print(f"Encontradas {len(image_urls)} imágenes de tamaño adecuado")
96
- return image_urls
97
-
98
- time.sleep(1)
99
-
100
- except Exception as e:
101
- print(f"Error con proxy {proxy}: {str(e)}")
102
- continue
103
 
104
  print("No se encontraron imágenes después de probar todos los proxies")
105
  return []
@@ -108,6 +111,7 @@ def search_google_images(query, num_images=1):
108
  print(f"Error general en la búsqueda de imágenes: {str(e)}")
109
  return []
110
 
 
111
  def process_image(image):
112
  try:
113
  width, height = image.size
 
34
  with open(proxy_file, 'r') as f:
35
  proxies = [line.strip() for line in f if line.strip()]
36
  print(f"Loaded {len(proxies)} proxies from file")
37
+ return [requests_proxy.ProxyInfo(proxy=f"http://{proxy}") for proxy in proxies]
38
  except Exception as e:
39
  print(f"Error loading proxies: {e}")
40
  return []
 
58
 
59
  if not proxies:
60
  print("No proxies available, trying without proxy")
61
+ with requests.Session() as session:
62
+ result = session.get(
63
+ f"https://www.googleapis.com/customsearch/v1",
64
+ params={
65
+ "q": query,
66
+ "cx": cse_id,
67
+ "searchType": "image",
68
+ "num": num_images * 3,
69
+ "safe": 'off',
70
+ "imgSize": 'HUGE',
71
+ "imgType": 'photo',
72
+ "rights": 'cc_publicdomain|cc_attribute|cc_sharealike',
73
+ "key": api_key
74
+ }
75
+ ).json()
76
+ else:
77
+ with requests_proxy.ProxyManager(proxies) as proxy_manager:
78
+ result = proxy_manager.get(
79
+ f"https://www.googleapis.com/customsearch/v1",
80
+ params={
81
+ "q": query,
82
+ "cx": cse_id,
83
+ "searchType": "image",
84
+ "num": num_images * 3,
85
+ "safe": 'off',
86
+ "imgSize": 'HUGE',
87
+ "imgType": 'photo',
88
+ "rights": 'cc_publicdomain|cc_attribute|cc_sharealike',
89
+ "key": api_key
90
+ }
91
+ ).json()
92
 
93
+ if 'items' in result:
94
+ image_urls = []
95
+ for item in result.get('items', []):
96
+ if 'image' in item:
97
+ width = int(item['image'].get('width', 0))
98
+ height = int(item['image'].get('height', 0))
99
+ if width >= MIN_WIDTH and height >= MIN_HEIGHT:
100
+ image_urls.append(item['link'])
101
+ if len(image_urls) >= num_images:
102
+ break
103
+
104
+ print(f"Encontradas {len(image_urls)} imágenes de tamaño adecuado")
105
+ return image_urls
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  print("No se encontraron imágenes después de probar todos los proxies")
108
  return []
 
111
  print(f"Error general en la búsqueda de imágenes: {str(e)}")
112
  return []
113
 
114
+
115
  def process_image(image):
116
  try:
117
  width, height = image.size