Bubbles30 commited on
Commit
024e348
Β·
verified Β·
1 Parent(s): 8fc6e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -1,7 +1,5 @@
1
  import gradio as gr
2
  from PIL import Image
3
- from PIL import ImageFile
4
- ImageFile.LOAD_TRUNCATED_IMAGES = True
5
  import os
6
  import zipfile
7
  import pandas as pd
@@ -97,9 +95,12 @@ handle_process = lambda mode, sd, ups, pu, fmt, w, h: (
97
  process_single_url_image(pu, fmt, w, h) if pu.strip() else
98
  ([], None, "⚠️ No valid input provided", None)
99
  )
 
 
 
100
  def fetch_with_retries(url, headers, timeout=15, retries=2, backoff=1):
101
  """
102
- Try up to `retries+1` times to GET `url` with `timeout` seconds.
103
  Returns a requests.Response or None.
104
  """
105
  for attempt in range(retries + 1):
@@ -112,56 +113,36 @@ def fetch_with_retries(url, headers, timeout=15, retries=2, backoff=1):
112
  time.sleep(backoff * (attempt + 1))
113
  return None
114
 
115
-
116
- import cloudscraper
117
-
118
  def download_image(url, save_path):
 
119
  if os.path.exists(save_path) and os.path.getsize(save_path) > 1000:
120
- print(f"βœ… Skipping cached: {save_path}")
121
  return save_path
122
 
123
- if any(blocked in url for blocked in BLOCKED_SITES):
 
124
  log_failure(url, "Skipped known slow site")
125
  return None
126
 
127
  referer = f"{urlparse(url).scheme}://{urlparse(url).netloc}/"
128
  headers = random_headers(referer)
129
 
130
- # 1. Try direct
131
- try:
132
- print("πŸš€ Trying direct...")
133
- resp = session.get(url, headers=headers, stream=True, timeout=8)
134
- if resp.status_code == 200 and "image" in resp.headers.get("Content-Type", ""):
135
- with open(save_path, 'wb') as f:
136
- for chunk in resp.iter_content(8192):
137
- f.write(chunk)
138
- print("βœ… Direct worked")
139
- return save_path
140
- except Exception as e:
141
- print(f"⚠️ Direct failed: {e}")
142
-
143
- # 2. Try proxies in parallel
144
- if PROXY_LIST:
145
- headers = random_headers(referer)
146
- with ThreadPoolExecutor(max_workers=5) as executor:
147
- futures = {
148
- executor.submit(try_download, url, headers, {"http": p, "https": p}): p
149
- for p in random.sample(PROXY_LIST, min(5, len(PROXY_LIST)))
150
- }
151
- for future in as_completed(futures):
152
- result = future.result()
153
- if result:
154
- with open(save_path, 'wb') as f:
155
- for chunk in result.iter_content(8192):
156
- f.write(chunk)
157
- print("βœ… Proxy worked:", futures[future])
158
- return save_path
159
-
160
- # 3. cloudscraper fallback
161
  try:
162
- print("🟠 cloudscraper fallback...")
163
  scraper = cloudscraper.create_scraper(sess=session)
164
- resp = scraper.get(url, headers=headers, stream=True, timeout=12)
165
  if resp.status_code == 200 and "image" in resp.headers.get("Content-Type", ""):
166
  with open(save_path, 'wb') as f:
167
  for chunk in resp.iter_content(8192):
@@ -171,7 +152,7 @@ def download_image(url, save_path):
171
  except Exception as e:
172
  print(f"❌ cloudscraper failed: {e}")
173
 
174
- # 4. Final fallback: Selenium
175
  try:
176
  print("πŸ§ͺ Headless browser fallback...")
177
  chrome_opts = Options()
@@ -181,7 +162,10 @@ def download_image(url, save_path):
181
  chrome_opts.add_argument("--disable-dev-shm-usage")
182
  chrome_opts.add_argument(f"user-agent={headers['User-Agent']}")
183
 
184
- driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_opts)
 
 
 
185
  driver.get(url)
186
  time.sleep(4)
187
  final_url = driver.current_url
@@ -192,22 +176,20 @@ def download_image(url, save_path):
192
  for k, v in cookies.items():
193
  session.cookies.set(k, v)
194
  r = session.get(final_url, headers=headers, stream=True, timeout=15)
195
- if r.status_code == 200 and 'image' in r.headers.get("Content-Type", ""):
196
  with open(save_path, 'wb') as f:
197
  for chunk in r.iter_content(8192):
198
  f.write(chunk)
199
  print("βœ… Selenium + cookies worked")
200
  return save_path
201
- else:
202
- log_failure(url, f"Selenium bad response: {r.status_code}")
203
  except Exception as e:
204
  print(f"❌ Selenium failed: {e}")
205
- log_failure(url, f"Selenium exception: {e}")
206
 
207
- # Fail
208
  log_failure(url, "All methods failed")
209
  return None
210
 
 
211
  lamp_html = """
212
  <style>
213
  * {
 
1
  import gradio as gr
2
  from PIL import Image
 
 
3
  import os
4
  import zipfile
5
  import pandas as pd
 
95
  process_single_url_image(pu, fmt, w, h) if pu.strip() else
96
  ([], None, "⚠️ No valid input provided", None)
97
  )
98
+ from PIL import ImageFile
99
+ ImageFile.LOAD_TRUNCATED_IMAGES = True
100
+
101
  def fetch_with_retries(url, headers, timeout=15, retries=2, backoff=1):
102
  """
103
+ Try up to retries+1 times to GET url with timeout seconds.
104
  Returns a requests.Response or None.
105
  """
106
  for attempt in range(retries + 1):
 
113
  time.sleep(backoff * (attempt + 1))
114
  return None
115
 
 
 
 
116
  def download_image(url, save_path):
117
+ # Skip cached
118
  if os.path.exists(save_path) and os.path.getsize(save_path) > 1000:
 
119
  return save_path
120
 
121
+ # Skip blocked
122
+ if any(b in url for b in BLOCKED_SITES):
123
  log_failure(url, "Skipped known slow site")
124
  return None
125
 
126
  referer = f"{urlparse(url).scheme}://{urlparse(url).netloc}/"
127
  headers = random_headers(referer)
128
 
129
+ # 1) Direct with retries
130
+ print(f"πŸš€ Fetching with retries: {url}")
131
+ resp = fetch_with_retries(url, headers, timeout=15, retries=2)
132
+ if resp:
133
+ with open(save_path, 'wb') as f:
134
+ for chunk in resp.iter_content(8192):
135
+ f.write(chunk)
136
+ print("βœ… Direct fetch succeeded")
137
+ return save_path
138
+ else:
139
+ print("❌ Direct fetch failed after retries")
140
+
141
+ # 2) cloudscraper fallback
142
+ print("🟠 cloudscraper fallback...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  try:
 
144
  scraper = cloudscraper.create_scraper(sess=session)
145
+ resp = scraper.get(url, headers=headers, stream=True, timeout=15)
146
  if resp.status_code == 200 and "image" in resp.headers.get("Content-Type", ""):
147
  with open(save_path, 'wb') as f:
148
  for chunk in resp.iter_content(8192):
 
152
  except Exception as e:
153
  print(f"❌ cloudscraper failed: {e}")
154
 
155
+ # 3) Selenium fallback
156
  try:
157
  print("πŸ§ͺ Headless browser fallback...")
158
  chrome_opts = Options()
 
162
  chrome_opts.add_argument("--disable-dev-shm-usage")
163
  chrome_opts.add_argument(f"user-agent={headers['User-Agent']}")
164
 
165
+ driver = webdriver.Chrome(
166
+ service=Service(ChromeDriverManager().install()),
167
+ options=chrome_opts
168
+ )
169
  driver.get(url)
170
  time.sleep(4)
171
  final_url = driver.current_url
 
176
  for k, v in cookies.items():
177
  session.cookies.set(k, v)
178
  r = session.get(final_url, headers=headers, stream=True, timeout=15)
179
+ if r.status_code == 200 and "image" in r.headers.get("Content-Type", ""):
180
  with open(save_path, 'wb') as f:
181
  for chunk in r.iter_content(8192):
182
  f.write(chunk)
183
  print("βœ… Selenium + cookies worked")
184
  return save_path
 
 
185
  except Exception as e:
186
  print(f"❌ Selenium failed: {e}")
 
187
 
188
+ # 4) Give up
189
  log_failure(url, "All methods failed")
190
  return None
191
 
192
+
193
  lamp_html = """
194
  <style>
195
  * {