Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -304,10 +304,10 @@ def flickr_photo_candidate(url):
|
|
| 304 |
return flickr_photo_via_html(username, photo_id, page_url)
|
| 305 |
|
| 306 |
|
| 307 |
-
def flickr_stream_via_html(username, max_images, first):
|
| 308 |
"""Read the photostream pages (newest first), then each photo page."""
|
| 309 |
ids, page = [], 1
|
| 310 |
-
while len(ids) < max_images and page <=
|
| 311 |
u = f"https://www.flickr.com/photos/{username}/" + (f"page{page}/" if page > 1 else "")
|
| 312 |
try:
|
| 313 |
r = session.get(u, timeout=REQUEST_TIMEOUT)
|
|
@@ -339,7 +339,7 @@ def flickr_stream_via_html(username, max_images, first):
|
|
| 339 |
return items
|
| 340 |
|
| 341 |
|
| 342 |
-
def flickr_photo_and_stream(url, max_images, include_stream=True):
|
| 343 |
"""Photo page -> the photographer's public photos, newest to oldest, up to max_images."""
|
| 344 |
first = flickr_photo_candidate(url)
|
| 345 |
if not first:
|
|
@@ -347,7 +347,7 @@ def flickr_photo_and_stream(url, max_images, include_stream=True):
|
|
| 347 |
if not include_stream:
|
| 348 |
return [first]
|
| 349 |
username, _ = extract_flickr_photo_id(url)
|
| 350 |
-
items = flickr_stream_via_html(username, max_images, first)
|
| 351 |
items.sort(key=lambda s: s.get("date_uploaded", ""), reverse=True)
|
| 352 |
return items or [first]
|
| 353 |
|
|
@@ -649,7 +649,8 @@ def empty_result(message):
|
|
| 649 |
return [], message, [], pd.DataFrame(), pd.DataFrame(), None, None, None, None
|
| 650 |
|
| 651 |
|
| 652 |
-
def find_images(url, max_images, min_width, min_height, remove_duplicates,
|
|
|
|
| 653 |
url = clean_url(url)
|
| 654 |
if not url:
|
| 655 |
return empty_result("β Please enter a URL.")
|
|
@@ -657,6 +658,7 @@ def find_images(url, max_images, min_width, min_height, remove_duplicates, inclu
|
|
| 657 |
return empty_result("β Invalid HTTP/HTTPS URL.")
|
| 658 |
|
| 659 |
max_images = min(int(max_images), MAX_IMAGES_HARD_LIMIT)
|
|
|
|
| 660 |
min_width = int(min_width or 0)
|
| 661 |
min_height = int(min_height or 0)
|
| 662 |
|
|
@@ -666,7 +668,7 @@ def find_images(url, max_images, min_width, min_height, remove_duplicates, inclu
|
|
| 666 |
# ---- Find candidates ----
|
| 667 |
try:
|
| 668 |
photo_list = (
|
| 669 |
-
flickr_photo_and_stream(url, max_images, include_stream)
|
| 670 |
if is_flickr_url(url) else None
|
| 671 |
)
|
| 672 |
if photo_list:
|
|
@@ -839,6 +841,10 @@ with gr.Blocks(title=APP_TITLE) as demo:
|
|
| 839 |
value=True,
|
| 840 |
label="Photo page: also download the photographer's other photos",
|
| 841 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
|
| 843 |
with gr.Row():
|
| 844 |
find_button = gr.Button("π Find & Analyze Images", variant="primary")
|
|
@@ -873,7 +879,8 @@ with gr.Blocks(title=APP_TITLE) as demo:
|
|
| 873 |
|
| 874 |
find_button.click(
|
| 875 |
fn=find_images,
|
| 876 |
-
inputs=[url_input, max_images, min_width, min_height, remove_duplicates,
|
|
|
|
| 877 |
outputs=[gallery, status, image_state, metadata_table, weekly_table,
|
| 878 |
weekly_plot, xlsx_file, csv_file, plot_file],
|
| 879 |
)
|
|
|
|
| 304 |
return flickr_photo_via_html(username, photo_id, page_url)
|
| 305 |
|
| 306 |
|
| 307 |
+
def flickr_stream_via_html(username, max_images, first, max_pages=100):
|
| 308 |
"""Read the photostream pages (newest first), then each photo page."""
|
| 309 |
ids, page = [], 1
|
| 310 |
+
while len(ids) < max_images and page <= max_pages:
|
| 311 |
u = f"https://www.flickr.com/photos/{username}/" + (f"page{page}/" if page > 1 else "")
|
| 312 |
try:
|
| 313 |
r = session.get(u, timeout=REQUEST_TIMEOUT)
|
|
|
|
| 339 |
return items
|
| 340 |
|
| 341 |
|
| 342 |
+
def flickr_photo_and_stream(url, max_images, include_stream=True, max_pages=100):
|
| 343 |
"""Photo page -> the photographer's public photos, newest to oldest, up to max_images."""
|
| 344 |
first = flickr_photo_candidate(url)
|
| 345 |
if not first:
|
|
|
|
| 347 |
if not include_stream:
|
| 348 |
return [first]
|
| 349 |
username, _ = extract_flickr_photo_id(url)
|
| 350 |
+
items = flickr_stream_via_html(username, max_images, first, max_pages)
|
| 351 |
items.sort(key=lambda s: s.get("date_uploaded", ""), reverse=True)
|
| 352 |
return items or [first]
|
| 353 |
|
|
|
|
| 649 |
return [], message, [], pd.DataFrame(), pd.DataFrame(), None, None, None, None
|
| 650 |
|
| 651 |
|
| 652 |
+
def find_images(url, max_images, min_width, min_height, remove_duplicates,
|
| 653 |
+
include_stream=True, max_pages=3):
|
| 654 |
url = clean_url(url)
|
| 655 |
if not url:
|
| 656 |
return empty_result("β Please enter a URL.")
|
|
|
|
| 658 |
return empty_result("β Invalid HTTP/HTTPS URL.")
|
| 659 |
|
| 660 |
max_images = min(int(max_images), MAX_IMAGES_HARD_LIMIT)
|
| 661 |
+
max_pages = max(1, int(max_pages or 1))
|
| 662 |
min_width = int(min_width or 0)
|
| 663 |
min_height = int(min_height or 0)
|
| 664 |
|
|
|
|
| 668 |
# ---- Find candidates ----
|
| 669 |
try:
|
| 670 |
photo_list = (
|
| 671 |
+
flickr_photo_and_stream(url, max_images, include_stream, max_pages)
|
| 672 |
if is_flickr_url(url) else None
|
| 673 |
)
|
| 674 |
if photo_list:
|
|
|
|
| 841 |
value=True,
|
| 842 |
label="Photo page: also download the photographer's other photos",
|
| 843 |
)
|
| 844 |
+
max_pages = gr.Number(
|
| 845 |
+
value=3, minimum=1, maximum=100, step=1, precision=0,
|
| 846 |
+
label="Photostream pages to scrape (stops earlier if Maximum images is reached)",
|
| 847 |
+
)
|
| 848 |
|
| 849 |
with gr.Row():
|
| 850 |
find_button = gr.Button("π Find & Analyze Images", variant="primary")
|
|
|
|
| 879 |
|
| 880 |
find_button.click(
|
| 881 |
fn=find_images,
|
| 882 |
+
inputs=[url_input, max_images, min_width, min_height, remove_duplicates,
|
| 883 |
+
include_stream, max_pages],
|
| 884 |
outputs=[gallery, status, image_state, metadata_table, weekly_table,
|
| 885 |
weekly_plot, xlsx_file, csv_file, plot_file],
|
| 886 |
)
|