-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_search.py
772 lines (666 loc) · 31.1 KB
/
advanced_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
import asyncio
import os
import random
import logging
from urllib.parse import urlparse
import re
from pathlib import Path
from io import BytesIO
import gradio as gr
import sys
# Playwright & Parsers
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader
# ------------------------------------------------------------------------------
# If packaging with PyInstaller or similar:
if getattr(sys, 'frozen', False):
bundle_dir = sys._MEIPASS
else:
bundle_dir = os.path.dirname(os.path.abspath(__file__))
# ------------------------------------------------------------------------------
# Logging Setup
logging.basicConfig(
filename='advanced_download_log.txt',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger()
# ------------------------------------------------------------------------------
# User-Agent Rotations
USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/115.0.0.0 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6_3) AppleWebKit/605.1.15 (KHTML, like Gecko) '
'Version/16.4 Safari/605.1.15',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/115.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0',
]
def get_random_user_agent():
return random.choice(USER_AGENTS)
def sizeof_fmt(num, suffix='B'):
"""Convert a file size to a human-readable string."""
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Y{suffix}"
# Known MIME types we might want to treat as downloadable files:
KNOWN_MIME_TYPES = {
"application/pdf": ".pdf",
"application/zip": ".zip",
"application/x-rar-compressed": ".rar",
"application/x-7z-compressed": ".7z",
"application/msword": ".doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
"audio/mpeg": ".mp3",
"video/mp4": ".mp4",
"image/png": ".png",
"image/jpeg": ".jpg",
"image/gif": ".gif",
# add more if you wish
}
# ------------------------------------------------------------------------------
# Playwright Helper Functions
async def human_like_scroll(page):
scroll_height = await page.evaluate('document.body.scrollHeight')
viewport_height = await page.evaluate('window.innerHeight')
current_scroll = 0
while current_scroll < scroll_height:
await page.evaluate(f'window.scrollTo(0, {current_scroll})')
await asyncio.sleep(random.uniform(0.5, 1.5))
current_scroll += viewport_height * random.uniform(0.5, 1.5)
scroll_height = await page.evaluate('document.body.scrollHeight')
async def human_like_interactions(page):
await page.mouse.move(random.randint(0, 1000), random.randint(0, 1000))
await asyncio.sleep(random.uniform(0.5, 1.5))
await page.mouse.click(random.randint(0, 1000), random.randint(0, 1000))
await asyncio.sleep(random.uniform(0.5, 1.5))
await page.evaluate("window.scrollBy(0, window.innerHeight / 2)")
await asyncio.sleep(random.uniform(0.5, 1.5))
async def get_file_size(url, page):
"""Returns a human-readable file size string or 'Unknown Size' if not available."""
try:
response = await page.request.head(url)
length = response.headers.get('Content-Length', None)
if length:
return sizeof_fmt(int(length))
else:
return "Unknown Size"
except Exception:
return "Unknown Size"
async def get_pdf_metadata(url, page):
"""Fetch PDF from the given URL and extract minimal metadata."""
try:
resp = await page.request.get(url, timeout=15000)
if resp.ok:
content = await resp.body()
pdf = BytesIO(content)
reader = PdfReader(pdf)
return {
'Title': reader.metadata.title if reader.metadata.title else 'N/A',
'Author': reader.metadata.author if reader.metadata.author else 'N/A',
'Pages': len(reader.pages),
}
else:
return {}
except Exception:
return {}
# ------------------------------------------------------------------------------
# Bing Search & File Extraction
async def perform_bing_search(query, num_results, page):
bing_url = f"https://www.bing.com/search?q={query.replace(' ', '+')}&count={num_results}"
try:
await page.goto(bing_url, timeout=30000)
await page.wait_for_selector('li.b_algo', timeout=30000)
await human_like_scroll(page)
html = await page.content()
soup = BeautifulSoup(html, 'html.parser')
results = soup.find_all('li', class_='b_algo')
urls = []
for r in results:
link = r.find('a')
if link and 'href' in link.attrs:
urls.append(link['href'])
if len(urls) >= num_results:
break
return urls
except PlaywrightTimeoutError:
logger.error("Bing search timed out.")
return []
except Exception as e:
logger.error(f"Bing search error: {e}")
return []
async def extract_downloadable_files(url, page, custom_ext_list):
"""
Analyze the page for direct file links, or Google Drive links, or
files indicated by HEAD request checks.
`custom_ext_list` is a list of additional file extensions to consider.
"""
found_files = []
try:
await page.goto(url, timeout=30000)
await page.wait_for_load_state('networkidle', timeout=30000)
await human_like_interactions(page)
content = await page.content()
soup = BeautifulSoup(content, 'html.parser')
# Base file extensions we always look for:
default_exts = [
'.pdf', '.docx', '.zip', '.rar', '.exe', '.mp3',
'.mp4', '.avi', '.mkv', '.png', '.jpg', '.jpeg', '.gif'
]
# Merge them with the user's custom list
all_exts = set(default_exts + [ext.strip().lower() for ext in custom_ext_list if ext.strip()])
anchors = soup.find_all('a', href=True)
for a in anchors:
href = a['href'].strip()
if not href:
continue
# Build the absolute URL if necessary
if href.startswith('http'):
file_url = href
elif href.startswith('/'):
parsed = urlparse(url)
file_url = f"{parsed.scheme}://{parsed.netloc}{href}"
else:
# Some relative or unusual link
parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc:
# Skip if we cannot form a valid absolute URL
continue
base_path = parsed.path.rsplit('/', 1)[0]
file_url = f"{parsed.scheme}://{parsed.netloc}{base_path}/{href}"
# Check #1: Google Drive link detection
if "drive.google.com" in href.lower():
file_id = None
match1 = re.search(r'/file/d/([^/]+)/', href)
if match1:
file_id = match1.group(1)
match2 = re.search(r'open\?id=([^&]+)', href)
if match2:
file_id = match2.group(1)
match3 = re.search(r'id=([^&]+)', href)
if match3:
file_id = match3.group(1)
if file_id:
direct = f"https://drive.google.com/uc?export=download&id={file_id}"
size_str = await get_file_size(direct, page)
found_files.append({
'url': direct,
'filename': f"drive_file_{file_id}",
'size': size_str,
'metadata': {}
})
continue
# Check #2: Known or custom extension
lower_href = href.lower()
if any(lower_href.endswith(ext) for ext in all_exts):
# It's a recognized direct file link
size_str = await get_file_size(file_url, page)
meta = {}
if file_url.lower().endswith('.pdf'):
meta = await get_pdf_metadata(file_url, page)
found_files.append({
'url': file_url,
'filename': os.path.basename(file_url.split('?')[0]),
'size': size_str,
'metadata': meta
})
else:
# Check #3: Use HEAD request to see if it's a known file by MIME type
try:
head_resp = await page.request.head(file_url, timeout=8000)
if head_resp.ok:
ctype = head_resp.headers.get("Content-Type", "").lower()
if ctype in KNOWN_MIME_TYPES:
# We treat this as a file
# If there's a content-disposition filename, we can use that;
# otherwise, we'll guess from the URL
cdisp = head_resp.headers.get("Content-Disposition", "")
filename = os.path.basename(file_url.split('?')[0])
if cdisp:
mt = re.search(r'filename\*?="?([^";]+)', cdisp)
if mt:
cdisp_fname = mt.group(1).strip('"').strip()
if cdisp_fname:
filename = cdisp_fname
else:
# If there's no extension in the URL, attach the known extension
base_part, ext_part = os.path.splitext(filename)
if not ext_part:
known_ext = KNOWN_MIME_TYPES[ctype]
filename = base_part + known_ext
size_str = await get_file_size(file_url, page)
meta = {}
# PDF metadata if relevant
if KNOWN_MIME_TYPES[ctype] == '.pdf':
meta = await get_pdf_metadata(file_url, page)
found_files.append({
'url': file_url,
'filename': filename,
'size': size_str,
'metadata': meta
})
except PlaywrightTimeoutError:
# skip link if HEAD request times out
pass
except Exception:
pass
return found_files
except PlaywrightTimeoutError:
logger.error(f"Timeout extracting from {url}")
return []
except Exception as e:
logger.error(f"Error extracting from {url}: {e}")
return []
async def download_file(file_info, save_dir, page, referer):
file_url = file_info['url']
fname = file_info['filename']
path = os.path.join(save_dir, fname)
base, ext = os.path.splitext(fname)
i = 1
while os.path.exists(path):
path = os.path.join(save_dir, f"{base}({i}){ext}")
i += 1
os.makedirs(save_dir, exist_ok=True)
try:
headers = {
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': referer
}
await human_like_interactions(page)
resp = await page.request.get(file_url, headers=headers, timeout=30000)
if resp.status == 403:
logger.error(f"403 Forbidden: {file_url}")
return None
if not resp.ok:
logger.error(f"Failed to download {file_url}: Status {resp.status}")
return None
# If it's Google Drive, refine filename
if "drive.google.com" in file_url.lower():
cdisp = resp.headers.get("Content-Disposition")
if cdisp:
mt = re.search(r'filename\*?="?([^";]+)', cdisp)
if mt:
real_fname = mt.group(1).strip('"').strip()
if real_fname:
path = os.path.join(save_dir, real_fname)
b2, e2 = os.path.splitext(real_fname)
j = 1
while os.path.exists(path):
path = os.path.join(save_dir, f"{b2}({j}){e2}")
j += 1
data = await resp.body()
with open(path, 'wb') as f:
f.write(data)
logger.info(f"Downloaded: {path}")
return path
except PlaywrightTimeoutError:
logger.error(f"Timeout downloading {file_url}")
return None
except Exception as e:
logger.error(f"Error downloading {file_url}: {e}")
return None
# ------------------------------------------------------------------------------
# DownloadManager
class DownloadManager:
def __init__(self, use_proxy=False, proxy=None, query=None, num_results=5):
self.use_proxy = use_proxy
self.proxy = proxy
self.query = query
self.num_results = num_results
self.playwright = None
self.browser = None
self.context = None
self.page = None
async def __aenter__(self):
self.playwright = await async_playwright().start()
opts = {"headless": True}
if self.use_proxy and self.proxy:
opts["proxy"] = {"server": self.proxy}
self.browser = await self.playwright.chromium.launch(**opts)
self.context = await self.browser.new_context(user_agent=get_random_user_agent())
self.page = await self.context.new_page()
# Extra headers
await self.page.set_extra_http_headers({
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.bing.com/'
})
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.browser.close()
await self.playwright.stop()
async def search_bing(self):
if not self.query:
return []
return await perform_bing_search(self.query, self.num_results, self.page)
async def analyze_url(self, url, custom_ext_list):
"""Now includes a list of custom extensions and advanced MIME checks."""
return await extract_downloadable_files(url, self.page, custom_ext_list)
async def download_files(self, file_list, directory, referer):
out_paths = []
for fi in file_list:
saved = await download_file(fi, directory, self.page, referer)
if saved:
out_paths.append(saved)
return out_paths
# ------------------------------------------------------------------------------
# BUILD THE APP (Two “pages” in one UI via radio + show/hide groups)
# ------------------------------------------------------------------------------
def build_gradio_app():
with gr.Blocks() as demo:
gr.Markdown("# Advanced Downloader with 'Two Pages' in One UI")
gr.Markdown(
"Use the radio below to switch between **Manual URL** mode and **Bing Search** mode. "
"Use the **Advanced Options** to specify extra file extensions to detect. "
"No API keys required!"
)
# Radio to choose the "page"
mode_radio = gr.Radio(
choices=["Manual URL", "Bing Search"],
value="Manual URL",
label="Select a mode:",
)
# Shared "Advanced Options"
with gr.Accordion("Advanced Options (Optional)", open=False):
custom_extensions = gr.Textbox(
label="Custom File Extensions (comma-separated)",
placeholder=".csv, .txt, .torrent, etc."
)
gr.Markdown(
"Any extensions here get **added** to the default set:\n"
"`.pdf, .docx, .zip, .rar, .exe, .mp3, .mp4, .avi, .mkv, .png, .jpg, .jpeg, .gif`\n"
)
# A "Clear Logs" button
def clear_logs_action():
return ""
# ----------------------------------------------------------------
# Page A: Manual URL
# ----------------------------------------------------------------
with gr.Group(visible=True) as manual_group:
gr.Markdown("## Manual URL Workflow")
manual_manager_state = gr.State(None)
manual_files_label_state = gr.State([])
manual_url_state = gr.State("")
use_proxy_manual = gr.Checkbox(label="Use Proxy? (Manual)", value=False)
proxy_manual = gr.Textbox(label="Proxy (http://ip:port)", placeholder="Optional")
manual_url = gr.Textbox(label="Manual URL", placeholder="https://example.com")
analyze_manual_btn = gr.Button("Analyze URL (Manual)")
manual_files_checkbox = gr.CheckboxGroup(label="Files from Manual URL", choices=[])
with gr.Row():
select_all_manual_btn = gr.Button("Select All (Manual)")
deselect_all_manual_btn = gr.Button("Deselect All (Manual)")
directory_manual = gr.Textbox(label="Download Directory (Manual)", placeholder="./downloads_manual")
delete_manual_ck = gr.Checkbox(label="Delete after download? (Manual)", value=False)
download_manual_btn = gr.Button("Download (Manual)")
manual_output = gr.Textbox(label="Manual Output / Logs", lines=5)
# For clearing logs
clear_manual_logs_btn = gr.Button("Clear Logs (Manual)")
# Helper
async def create_manual_manager(usep, prox):
dm = DownloadManager(use_proxy=usep, proxy=prox)
await dm.__aenter__()
return dm
async def analyze_manual_fn(url_val, usep, prox, mgr, custom_ext_str):
if not url_val:
return (gr.update(choices=[], value=[]), [], None, "Please enter a URL first.")
exts = [x.strip() for x in custom_ext_str.split(",") if x.strip()]
if mgr is None:
mgr = await create_manual_manager(usep, prox)
discovered = await mgr.analyze_url(url_val, exts)
if not discovered:
return (
gr.update(choices=[], value=[]),
[],
mgr,
f"No files found at {url_val}."
)
label_list = []
for i, f in enumerate(discovered):
detail_parts = []
if f['size']:
detail_parts.append(f"Size: {f['size']}")
meta = f.get("metadata", {})
if meta.get('Title') and meta['Title'] != 'N/A':
detail_parts.append(f"Title: {meta['Title']}")
if meta.get('Author') and meta['Author'] != 'N/A':
detail_parts.append(f"Author: {meta['Author']}")
if meta.get('Pages') is not None:
detail_parts.append(f"Pages: {meta['Pages']}")
label_str = f"{i}. {f['filename']} | " + " | ".join(detail_parts)
label_list.append(label_str)
return (
gr.update(choices=label_list, value=[]),
label_list,
mgr,
f"Found {len(discovered)} files at {url_val}."
)
def select_all_manual_fn(file_labels):
return gr.update(value=file_labels)
def deselect_all_manual_fn():
return gr.update(value=[])
async def download_manual_fn(selected, label_list, folder, do_del, manager, last_url, custom_ext_str):
if manager is None:
return "No manager. Please analyze a Manual URL first."
if not last_url:
return "No last URL. Please analyze a URL first."
if not selected:
return "No files selected."
exts = [x.strip() for x in custom_ext_str.split(",") if x.strip()]
discovered = await manager.analyze_url(last_url, exts)
try:
indices = [int(x.split(".")[0]) for x in selected]
except:
return "Error parsing selected items."
chosen = []
for i in indices:
if i < len(discovered):
chosen.append(discovered[i])
if not chosen:
return "No valid files selected."
if not folder:
folder = "./downloads_manual"
downloaded = await manager.download_files(chosen, folder, referer=last_url)
if not downloaded:
return "No files downloaded."
if do_del:
for fp in downloaded:
try:
os.remove(fp)
logger.info(f"Deleted: {fp}")
except OSError as e:
logger.error(f"Error deleting {fp}: {e}")
return f"Downloaded & deleted {len(downloaded)} files: {downloaded}"
else:
return f"Downloaded {len(downloaded)} files to '{folder}': {downloaded}"
# Wire up
analyze_manual_btn.click(
fn=analyze_manual_fn,
inputs=[manual_url, use_proxy_manual, proxy_manual, manual_manager_state, custom_extensions],
outputs=[manual_files_checkbox, manual_files_label_state, manual_manager_state, manual_output]
).then(
fn=lambda x: x,
inputs=[manual_url],
outputs=[manual_url_state]
)
select_all_manual_btn.click(
fn=select_all_manual_fn,
inputs=[manual_files_label_state],
outputs=[manual_files_checkbox]
)
deselect_all_manual_btn.click(
fn=deselect_all_manual_fn,
outputs=[manual_files_checkbox]
)
download_manual_btn.click(
fn=download_manual_fn,
inputs=[manual_files_checkbox, manual_files_label_state, directory_manual,
delete_manual_ck, manual_manager_state, manual_url_state, custom_extensions],
outputs=[manual_output]
)
clear_manual_logs_btn.click(
fn=clear_logs_action,
inputs=[],
outputs=[manual_output]
)
# ----------------------------------------------------------------
# Page B: Bing Search
# ----------------------------------------------------------------
with gr.Group(visible=False) as search_group:
gr.Markdown("## Bing Search Workflow")
search_manager_state = gr.State(None)
search_file_label_state = gr.State([])
search_url_state = gr.State("")
use_proxy_search = gr.Checkbox(label="Use Proxy? (Search)", value=False)
proxy_search = gr.Textbox(label="Proxy (http://ip:port)", placeholder="Optional")
query_inp = gr.Textbox(label="Bing Search Query")
num_results_sl = gr.Slider(label="Number of Results", minimum=1, maximum=50, value=5, step=1)
search_btn = gr.Button("Search Bing")
results_dd = gr.Dropdown(label="Bing Results", choices=[], value=None)
analyze_search_btn = gr.Button("Analyze Selected URL")
search_files_checkbox = gr.CheckboxGroup(label="Files from Search", choices=[])
with gr.Row():
select_all_search_btn = gr.Button("Select All (Search)")
deselect_all_search_btn = gr.Button("Deselect All (Search)")
directory_search = gr.Textbox(label="Download Directory (Search)", placeholder="./downloads_search")
delete_search_ck = gr.Checkbox(label="Delete after download? (Search)", value=False)
download_search_btn = gr.Button("Download (Search)")
search_output = gr.Textbox(label="Search Output / Logs", lines=5)
# For clearing logs
clear_search_logs_btn = gr.Button("Clear Logs (Search)")
async def create_search_manager(usep, px, query, numr):
dm = DownloadManager(use_proxy=usep, proxy=px, query=query, num_results=numr)
await dm.__aenter__()
return dm
async def do_search_fn(usep, px, query, numr):
if not query:
return (gr.update(choices=[], value=[]), None, "No query entered.")
mgr = await create_search_manager(usep, px, query, numr)
results = await mgr.search_bing()
if not results:
return (gr.update(choices=[], value=[]), mgr, "No results or Bing error.")
return (gr.update(choices=results, value=results[0]), mgr, f"Found {len(results)} results.")
async def analyze_search_fn(sel_url, mgr, custom_ext_str):
if not sel_url:
return (gr.update(choices=[], value=[]), [], "No URL selected.")
if mgr is None:
return (gr.update(choices=[], value=[]), [], "No manager. Please search again.")
exts = [x.strip() for x in custom_ext_str.split(",") if x.strip()]
discovered = await mgr.analyze_url(sel_url, exts)
if not discovered:
return (gr.update(choices=[], value=[]), [], "No files found on that page.")
labels = []
for i, f in enumerate(discovered):
detail_parts = []
if f['size']:
detail_parts.append(f"Size: {f['size']}")
meta = f.get("metadata", {})
if meta.get('Title') and meta['Title'] != 'N/A':
detail_parts.append(f"Title: {meta['Title']}")
if meta.get('Author') and meta['Author'] != 'N/A':
detail_parts.append(f"Author: {meta['Author']}")
if meta.get('Pages') is not None:
detail_parts.append(f"Pages: {meta['Pages']}")
labels.append(f"{i}. {f['filename']} | " + " | ".join(detail_parts))
return (gr.update(choices=labels, value=[]), labels, f"Found {len(discovered)} files.")
def select_all_search_fn(file_labels):
return gr.update(value=file_labels)
def deselect_all_search_fn():
return gr.update(value=[])
async def download_search_fn(selected, label_list, folder, do_del, mgr, sel_url, custom_ext_str):
if mgr is None:
return "No manager. Please search first."
if not sel_url:
return "No selected URL from results."
if not selected:
return "No files selected."
exts = [x.strip() for x in custom_ext_str.split(",") if x.strip()]
discovered = await mgr.analyze_url(sel_url, exts)
try:
idxs = [int(x.split(".")[0]) for x in selected]
except:
return "Error parsing selected items."
chosen = []
for i in idxs:
if i < len(discovered):
chosen.append(discovered[i])
if not chosen:
return "No valid files selected."
if not folder:
folder = "./downloads_search"
downloaded = await mgr.download_files(chosen, folder, referer=sel_url)
if not downloaded:
return "No files downloaded."
if do_del:
for fp in downloaded:
try:
os.remove(fp)
logger.info(f"Deleted: {fp}")
except OSError as e:
logger.error(f"Error deleting {fp}: {e}")
return f"Downloaded & deleted {len(downloaded)} files: {downloaded}"
else:
return f"Downloaded {len(downloaded)} files to '{folder}': {downloaded}"
# Wire up
search_btn.click(
fn=do_search_fn,
inputs=[use_proxy_search, proxy_search, query_inp, num_results_sl],
outputs=[results_dd, search_manager_state, search_output]
)
analyze_search_btn.click(
fn=analyze_search_fn,
inputs=[results_dd, search_manager_state, custom_extensions],
outputs=[search_files_checkbox, search_file_label_state, search_output]
).then(
fn=lambda x: x,
inputs=[results_dd],
outputs=[search_url_state]
)
select_all_search_btn.click(
fn=select_all_search_fn,
inputs=[search_file_label_state],
outputs=[search_files_checkbox]
)
deselect_all_search_btn.click(
fn=deselect_all_search_fn,
outputs=[search_files_checkbox]
)
download_search_btn.click(
fn=download_search_fn,
inputs=[search_files_checkbox, search_file_label_state, directory_search,
delete_search_ck, search_manager_state, search_url_state, custom_extensions],
outputs=[search_output]
)
clear_search_logs_btn.click(
fn=clear_logs_action,
inputs=[],
outputs=[search_output]
)
# ------------------------------------------------------------------
# Toggle which Group is visible based on radio selection
# ------------------------------------------------------------------
def switch_mode(mode_choice):
return (
gr.update(visible=(mode_choice == "Manual URL")),
gr.update(visible=(mode_choice == "Bing Search"))
)
mode_radio.change(
fn=switch_mode,
inputs=[mode_radio],
outputs=[manual_group, search_group],
)
return demo
# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------
if __name__ == "__main__":
app = build_gradio_app()
# Try port 7860; if it's unavailable, fall back to port 0 (an open random port).
try:
asyncio.run(app.launch(server_name="127.0.0.1", server_port=7860))
except OSError:
print("Port 7860 is busy. Trying a random open port...")
asyncio.run(app.launch(server_name="127.0.0.1", server_port=0))