Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ import subprocess
|
|
| 17 |
import sys
|
| 18 |
|
| 19 |
def setup_chrome_driver():
|
| 20 |
-
"""
|
| 21 |
chrome_options = Options()
|
| 22 |
chrome_options.add_argument("--headless")
|
| 23 |
chrome_options.add_argument("--no-sandbox")
|
|
@@ -27,7 +27,7 @@ def setup_chrome_driver():
|
|
| 27 |
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
| 28 |
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
driver = webdriver.Chrome(options=chrome_options)
|
| 32 |
return driver
|
| 33 |
except Exception as e:
|
|
@@ -35,10 +35,10 @@ def setup_chrome_driver():
|
|
| 35 |
return None
|
| 36 |
|
| 37 |
def extract_images_from_pinterest(url, max_images, progress_callback=None):
|
| 38 |
-
"""
|
| 39 |
driver = setup_chrome_driver()
|
| 40 |
if not driver:
|
| 41 |
-
return [], "
|
| 42 |
|
| 43 |
try:
|
| 44 |
driver.get(url)
|
|
@@ -49,12 +49,12 @@ def extract_images_from_pinterest(url, max_images, progress_callback=None):
|
|
| 49 |
no_new_images_count = 0
|
| 50 |
|
| 51 |
while len(imgList) < max_images:
|
| 52 |
-
#
|
| 53 |
scroll += 800
|
| 54 |
driver.execute_script(f'window.scrollTo(0, {scroll})')
|
| 55 |
time.sleep(1)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
imgs = driver.find_elements(By.CSS_SELECTOR, 'div[data-test-id="pin"] img')
|
| 59 |
new_images = 0
|
| 60 |
|
|
@@ -68,9 +68,9 @@ def extract_images_from_pinterest(url, max_images, progress_callback=None):
|
|
| 68 |
continue
|
| 69 |
|
| 70 |
if progress_callback:
|
| 71 |
-
progress_callback(f"已找到 {len(imgList)}
|
| 72 |
|
| 73 |
-
#
|
| 74 |
if new_images == 0:
|
| 75 |
no_new_images_count += 1
|
| 76 |
else:
|
|
@@ -82,15 +82,15 @@ def extract_images_from_pinterest(url, max_images, progress_callback=None):
|
|
| 82 |
return imgList, None
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
return [], f"
|
| 86 |
finally:
|
| 87 |
driver.quit()
|
| 88 |
|
| 89 |
def download_image(args):
|
| 90 |
-
"""
|
| 91 |
index, url, temp_dir = args
|
| 92 |
try:
|
| 93 |
-
#
|
| 94 |
if '236x' in url:
|
| 95 |
url = url.replace('236x', 'originals')
|
| 96 |
elif '474x' in url:
|
|
@@ -99,7 +99,7 @@ def download_image(args):
|
|
| 99 |
# 生成文件名
|
| 100 |
filename = f"pinterest_img_{index+1:04d}"
|
| 101 |
|
| 102 |
-
#
|
| 103 |
url_parts = url.split('/')
|
| 104 |
if len(url_parts) > 0:
|
| 105 |
original_name = url_parts[-1].split('?')[0]
|
|
@@ -107,13 +107,13 @@ def download_image(args):
|
|
| 107 |
clean_name = re.sub(r'[^\w\-_\.]', '_', original_name)
|
| 108 |
filename = f"pinterest_img_{index+1:04d}_{clean_name}"
|
| 109 |
|
| 110 |
-
#
|
| 111 |
if not filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.webp')):
|
| 112 |
filename += '.jpg'
|
| 113 |
|
| 114 |
filepath = os.path.join(temp_dir, filename)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
headers = {
|
| 118 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 119 |
}
|
|
@@ -127,30 +127,30 @@ def download_image(args):
|
|
| 127 |
return True, filename
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
-
return False, f"
|
| 131 |
|
| 132 |
def scrape_pinterest_images(pinterest_url, num_images, progress=gr.Progress()):
|
| 133 |
-
"""
|
| 134 |
if not pinterest_url:
|
| 135 |
-
return None, "
|
| 136 |
|
| 137 |
if num_images <= 0:
|
| 138 |
-
return None, "
|
| 139 |
|
| 140 |
-
if num_images > 500: #
|
| 141 |
-
return None, "
|
| 142 |
|
| 143 |
-
#
|
| 144 |
try:
|
| 145 |
parsed_url = urlparse(pinterest_url)
|
| 146 |
if 'pinterest.com' not in parsed_url.netloc:
|
| 147 |
-
return None, "
|
| 148 |
except:
|
| 149 |
-
return None, "URL
|
| 150 |
|
| 151 |
-
progress(0, desc="
|
| 152 |
|
| 153 |
-
#
|
| 154 |
def update_progress(msg):
|
| 155 |
progress(0.3, desc=msg)
|
| 156 |
|
|
@@ -160,40 +160,40 @@ def scrape_pinterest_images(pinterest_url, num_images, progress=gr.Progress()):
|
|
| 160 |
return None, error
|
| 161 |
|
| 162 |
if not img_urls:
|
| 163 |
-
return None, "
|
| 164 |
|
| 165 |
-
progress(0.5, desc=f"
|
| 166 |
|
| 167 |
-
#
|
| 168 |
temp_dir = tempfile.mkdtemp()
|
| 169 |
|
| 170 |
try:
|
| 171 |
-
#
|
| 172 |
download_args = [(i, url, temp_dir) for i, url in enumerate(img_urls)]
|
| 173 |
|
| 174 |
-
#
|
| 175 |
successful_downloads = []
|
| 176 |
failed_downloads = []
|
| 177 |
|
| 178 |
-
with ThreadPoolExecutor(max_workers=3) as executor: #
|
| 179 |
results = list(executor.map(download_image, download_args))
|
| 180 |
|
| 181 |
for i, (success, info) in enumerate(results):
|
| 182 |
if success:
|
| 183 |
successful_downloads.append(info)
|
| 184 |
else:
|
| 185 |
-
failed_downloads.append(f"
|
| 186 |
|
| 187 |
-
#
|
| 188 |
progress((0.5 + 0.4 * (i + 1) / len(results)),
|
| 189 |
-
desc=f"
|
| 190 |
|
| 191 |
if not successful_downloads:
|
| 192 |
-
return None, "
|
| 193 |
|
| 194 |
-
progress(0.9, desc="
|
| 195 |
|
| 196 |
-
#
|
| 197 |
zip_path = os.path.join(tempfile.gettempdir(), "pinterest_images.zip")
|
| 198 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 199 |
for filename in os.listdir(temp_dir):
|
|
@@ -203,49 +203,49 @@ def scrape_pinterest_images(pinterest_url, num_images, progress=gr.Progress()):
|
|
| 203 |
|
| 204 |
progress(1.0, desc="完成!")
|
| 205 |
|
| 206 |
-
#
|
| 207 |
result_info = f"""
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
|
| 214 |
-
|
| 215 |
"""
|
| 216 |
|
| 217 |
if failed_downloads:
|
| 218 |
-
result_info += f"\n\n
|
| 219 |
|
| 220 |
return zip_path, result_info
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
-
return None, f"
|
| 224 |
finally:
|
| 225 |
-
#
|
| 226 |
try:
|
| 227 |
shutil.rmtree(temp_dir)
|
| 228 |
except:
|
| 229 |
pass
|
| 230 |
|
| 231 |
-
#
|
| 232 |
def create_interface():
|
| 233 |
-
with gr.Blocks(title="Pinterest
|
| 234 |
gr.Markdown("""
|
| 235 |
-
# 🖼️ Pinterest
|
| 236 |
|
| 237 |
-
|
| 238 |
|
| 239 |
-
|
| 240 |
-
1.
|
| 241 |
-
2.
|
| 242 |
-
3.
|
| 243 |
-
4.
|
| 244 |
|
| 245 |
-
|
| 246 |
-
-
|
| 247 |
-
-
|
| 248 |
-
-
|
| 249 |
""")
|
| 250 |
|
| 251 |
with gr.Row():
|
|
@@ -254,7 +254,7 @@ def create_interface():
|
|
| 254 |
label="Pinterest URL",
|
| 255 |
placeholder="https://www.pinterest.com/search/pins/?q=your-search-term",
|
| 256 |
lines=2,
|
| 257 |
-
info="
|
| 258 |
)
|
| 259 |
|
| 260 |
num_images = gr.Slider(
|
|
@@ -262,22 +262,22 @@ def create_interface():
|
|
| 262 |
maximum=500,
|
| 263 |
value=20,
|
| 264 |
step=1,
|
| 265 |
-
label="
|
| 266 |
-
info="
|
| 267 |
)
|
| 268 |
|
| 269 |
-
download_btn = gr.Button("🚀
|
| 270 |
|
| 271 |
with gr.Column():
|
| 272 |
result_info = gr.Textbox(
|
| 273 |
-
label="
|
| 274 |
lines=10,
|
| 275 |
interactive=False,
|
| 276 |
-
info="
|
| 277 |
)
|
| 278 |
|
| 279 |
download_file = gr.File(
|
| 280 |
-
label="
|
| 281 |
interactive=False,
|
| 282 |
visible=False
|
| 283 |
)
|
|
@@ -292,7 +292,7 @@ def create_interface():
|
|
| 292 |
```
|
| 293 |
""")
|
| 294 |
|
| 295 |
-
#
|
| 296 |
def handle_download(url, num):
|
| 297 |
zip_path, info = scrape_pinterest_images(url, int(num))
|
| 298 |
if zip_path:
|
|
@@ -309,7 +309,7 @@ def create_interface():
|
|
| 309 |
|
| 310 |
return interface
|
| 311 |
|
| 312 |
-
#
|
| 313 |
if __name__ == "__main__":
|
| 314 |
interface = create_interface()
|
| 315 |
interface.launch(
|
|
|
|
| 17 |
import sys
|
| 18 |
|
| 19 |
def setup_chrome_driver():
|
| 20 |
+
"""設置Chrome WebDriver,適用於Hugging Face Spaces"""
|
| 21 |
chrome_options = Options()
|
| 22 |
chrome_options.add_argument("--headless")
|
| 23 |
chrome_options.add_argument("--no-sandbox")
|
|
|
|
| 27 |
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
# 嘗試使用系統中的Chrome
|
| 31 |
driver = webdriver.Chrome(options=chrome_options)
|
| 32 |
return driver
|
| 33 |
except Exception as e:
|
|
|
|
| 35 |
return None
|
| 36 |
|
| 37 |
def extract_images_from_pinterest(url, max_images, progress_callback=None):
|
| 38 |
+
"""從Pinterest頁面提取圖片URL"""
|
| 39 |
driver = setup_chrome_driver()
|
| 40 |
if not driver:
|
| 41 |
+
return [], "無法啟動Chrome WebDriver"
|
| 42 |
|
| 43 |
try:
|
| 44 |
driver.get(url)
|
|
|
|
| 49 |
no_new_images_count = 0
|
| 50 |
|
| 51 |
while len(imgList) < max_images:
|
| 52 |
+
# 滾動頁面
|
| 53 |
scroll += 800
|
| 54 |
driver.execute_script(f'window.scrollTo(0, {scroll})')
|
| 55 |
time.sleep(1)
|
| 56 |
|
| 57 |
+
# 獲取圖片元素
|
| 58 |
imgs = driver.find_elements(By.CSS_SELECTOR, 'div[data-test-id="pin"] img')
|
| 59 |
new_images = 0
|
| 60 |
|
|
|
|
| 68 |
continue
|
| 69 |
|
| 70 |
if progress_callback:
|
| 71 |
+
progress_callback(f"已找到 {len(imgList)} 張圖片")
|
| 72 |
|
| 73 |
+
# 檢查是否沒有新圖片
|
| 74 |
if new_images == 0:
|
| 75 |
no_new_images_count += 1
|
| 76 |
else:
|
|
|
|
| 82 |
return imgList, None
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return [], f"提取圖片時出錯: {str(e)}"
|
| 86 |
finally:
|
| 87 |
driver.quit()
|
| 88 |
|
| 89 |
def download_image(args):
|
| 90 |
+
"""下載單張圖片"""
|
| 91 |
index, url, temp_dir = args
|
| 92 |
try:
|
| 93 |
+
# 轉換為高清圖片URL
|
| 94 |
if '236x' in url:
|
| 95 |
url = url.replace('236x', 'originals')
|
| 96 |
elif '474x' in url:
|
|
|
|
| 99 |
# 生成文件名
|
| 100 |
filename = f"pinterest_img_{index+1:04d}"
|
| 101 |
|
| 102 |
+
# 從URL提取原始文件名
|
| 103 |
url_parts = url.split('/')
|
| 104 |
if len(url_parts) > 0:
|
| 105 |
original_name = url_parts[-1].split('?')[0]
|
|
|
|
| 107 |
clean_name = re.sub(r'[^\w\-_\.]', '_', original_name)
|
| 108 |
filename = f"pinterest_img_{index+1:04d}_{clean_name}"
|
| 109 |
|
| 110 |
+
# 確保文件擴展名
|
| 111 |
if not filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.webp')):
|
| 112 |
filename += '.jpg'
|
| 113 |
|
| 114 |
filepath = os.path.join(temp_dir, filename)
|
| 115 |
|
| 116 |
+
# 下載圖片
|
| 117 |
headers = {
|
| 118 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 119 |
}
|
|
|
|
| 127 |
return True, filename
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
+
return False, f"下載失敗: {str(e)}"
|
| 131 |
|
| 132 |
def scrape_pinterest_images(pinterest_url, num_images, progress=gr.Progress()):
|
| 133 |
+
"""主要的爬蟲函數"""
|
| 134 |
if not pinterest_url:
|
| 135 |
+
return None, "請輸入Pinterest URL"
|
| 136 |
|
| 137 |
if num_images <= 0:
|
| 138 |
+
return None, "圖片數量必須大於0"
|
| 139 |
|
| 140 |
+
if num_images > 500: # 限制最大數量
|
| 141 |
+
return None, "圖片數量不能超過500張"
|
| 142 |
|
| 143 |
+
# 驗證URL
|
| 144 |
try:
|
| 145 |
parsed_url = urlparse(pinterest_url)
|
| 146 |
if 'pinterest.com' not in parsed_url.netloc:
|
| 147 |
+
return None, "請輸入有效的Pinterest URL"
|
| 148 |
except:
|
| 149 |
+
return None, "URL格式無效"
|
| 150 |
|
| 151 |
+
progress(0, desc="開始提取圖片URL...")
|
| 152 |
|
| 153 |
+
# 提取圖片URL
|
| 154 |
def update_progress(msg):
|
| 155 |
progress(0.3, desc=msg)
|
| 156 |
|
|
|
|
| 160 |
return None, error
|
| 161 |
|
| 162 |
if not img_urls:
|
| 163 |
+
return None, "未找到任何圖片"
|
| 164 |
|
| 165 |
+
progress(0.5, desc=f"開始下載 {len(img_urls)} 張圖片...")
|
| 166 |
|
| 167 |
+
# 創建臨時目錄
|
| 168 |
temp_dir = tempfile.mkdtemp()
|
| 169 |
|
| 170 |
try:
|
| 171 |
+
# 準備下載參數
|
| 172 |
download_args = [(i, url, temp_dir) for i, url in enumerate(img_urls)]
|
| 173 |
|
| 174 |
+
# 多線程下載
|
| 175 |
successful_downloads = []
|
| 176 |
failed_downloads = []
|
| 177 |
|
| 178 |
+
with ThreadPoolExecutor(max_workers=3) as executor: # 降低並發數
|
| 179 |
results = list(executor.map(download_image, download_args))
|
| 180 |
|
| 181 |
for i, (success, info) in enumerate(results):
|
| 182 |
if success:
|
| 183 |
successful_downloads.append(info)
|
| 184 |
else:
|
| 185 |
+
failed_downloads.append(f"圖片 {i+1}: {info}")
|
| 186 |
|
| 187 |
+
# 更新進度
|
| 188 |
progress((0.5 + 0.4 * (i + 1) / len(results)),
|
| 189 |
+
desc=f"已下載 {len(successful_downloads)} / {len(img_urls)} 張圖片")
|
| 190 |
|
| 191 |
if not successful_downloads:
|
| 192 |
+
return None, "所有圖片下載失敗"
|
| 193 |
|
| 194 |
+
progress(0.9, desc="創建ZIP文件...")
|
| 195 |
|
| 196 |
+
# 創建ZIP文件
|
| 197 |
zip_path = os.path.join(tempfile.gettempdir(), "pinterest_images.zip")
|
| 198 |
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 199 |
for filename in os.listdir(temp_dir):
|
|
|
|
| 203 |
|
| 204 |
progress(1.0, desc="完成!")
|
| 205 |
|
| 206 |
+
# 準備結果信息
|
| 207 |
result_info = f"""
|
| 208 |
+
下載完成!
|
| 209 |
|
| 210 |
+
成功下載: {len(successful_downloads)} 張圖片
|
| 211 |
+
失敗: {len(failed_downloads)} 張圖片
|
| 212 |
+
總計: {len(img_urls)} 張圖片
|
| 213 |
|
| 214 |
+
請點擊下方鏈接下載ZIP文件。
|
| 215 |
"""
|
| 216 |
|
| 217 |
if failed_downloads:
|
| 218 |
+
result_info += f"\n\n失敗詳情:\n" + "\n".join(failed_downloads[:10]) # 只顯示前10個錯誤
|
| 219 |
|
| 220 |
return zip_path, result_info
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
+
return None, f"處理過程中出錯: {str(e)}"
|
| 224 |
finally:
|
| 225 |
+
# 清理臨時目錄
|
| 226 |
try:
|
| 227 |
shutil.rmtree(temp_dir)
|
| 228 |
except:
|
| 229 |
pass
|
| 230 |
|
| 231 |
+
# 創建Gradio界面
|
| 232 |
def create_interface():
|
| 233 |
+
with gr.Blocks(title="Pinterest圖片下載器", theme=gr.themes.Soft()) as interface:
|
| 234 |
gr.Markdown("""
|
| 235 |
+
# 🖼️ Pinterest 圖片下載器
|
| 236 |
|
| 237 |
+
輸入Pinterest搜索頁面的URL,批量下載圖片。
|
| 238 |
|
| 239 |
+
**使用說明:**
|
| 240 |
+
1. 輸入Pinterest搜索頁面或板塊的完整URL
|
| 241 |
+
2. 設置要下載的圖片數量(建議不超過100張)
|
| 242 |
+
3. 點擊"開始下載"按鈕
|
| 243 |
+
4. 等待處理完成後下載ZIP文件
|
| 244 |
|
| 245 |
+
**注意事項:**
|
| 246 |
+
- 請確保輸入的是有效的Pinterest URL
|
| 247 |
+
- 下載速度取決於網絡狀況和圖片大小
|
| 248 |
+
- 建議單次下載不超過100張圖片
|
| 249 |
""")
|
| 250 |
|
| 251 |
with gr.Row():
|
|
|
|
| 254 |
label="Pinterest URL",
|
| 255 |
placeholder="https://www.pinterest.com/search/pins/?q=your-search-term",
|
| 256 |
lines=2,
|
| 257 |
+
info="輸入Pinterest搜索頁面或板塊的完整URL"
|
| 258 |
)
|
| 259 |
|
| 260 |
num_images = gr.Slider(
|
|
|
|
| 262 |
maximum=500,
|
| 263 |
value=20,
|
| 264 |
step=1,
|
| 265 |
+
label="圖片數量",
|
| 266 |
+
info="要下載的圖片數量(建議不超過100張)"
|
| 267 |
)
|
| 268 |
|
| 269 |
+
download_btn = gr.Button("🚀 開始下載", variant="primary", size="lg")
|
| 270 |
|
| 271 |
with gr.Column():
|
| 272 |
result_info = gr.Textbox(
|
| 273 |
+
label="下載結果",
|
| 274 |
lines=10,
|
| 275 |
interactive=False,
|
| 276 |
+
info="顯示下載進度和結果信息"
|
| 277 |
)
|
| 278 |
|
| 279 |
download_file = gr.File(
|
| 280 |
+
label="下載文件",
|
| 281 |
interactive=False,
|
| 282 |
visible=False
|
| 283 |
)
|
|
|
|
| 292 |
```
|
| 293 |
""")
|
| 294 |
|
| 295 |
+
# 綁定事件
|
| 296 |
def handle_download(url, num):
|
| 297 |
zip_path, info = scrape_pinterest_images(url, int(num))
|
| 298 |
if zip_path:
|
|
|
|
| 309 |
|
| 310 |
return interface
|
| 311 |
|
| 312 |
+
# 啟動應用
|
| 313 |
if __name__ == "__main__":
|
| 314 |
interface = create_interface()
|
| 315 |
interface.launch(
|