-
Notifications
You must be signed in to change notification settings - Fork 4
Add a plugin for completecollegephotolibrary.org #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cclauss
wants to merge
7
commits into
ISKME:main
Choose a base branch
from
cclauss:add-a-plugin-for-collage-photos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d574ba
ome_node.channel_cards() was removed
cclauss bf6f1a1
[pre-commit.ci] pre-commit autoupdate (#195)
pre-commit-ci[bot] cff5c2f
Bump actions/upload-pages-artifact in the github-actions group (#196)
dependabot[bot] dd29d37
download images from complete_college_photo_library
cclauss f9f40c0
Update server/plugins/collage_photos/get_photos.py
cclauss 442c97b
Merge branch 'main' into add-a-plugin-for-collage-photos
cclauss 855b51a
Comment out pytest runs in ci.yml
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # /// script | ||
| # requires-python = ">=3.13" | ||
| # dependencies = [ | ||
| # "beautifulsoup4", | ||
| # "httpx", | ||
| # ] | ||
| # /// | ||
|
|
||
| import asyncio | ||
| from collections.abc import AsyncGenerator | ||
| from pathlib import Path | ||
|
|
||
| import httpx | ||
| from bs4 import BeautifulSoup | ||
|
|
||
| PLUGIN_NAME = "college_photos" | ||
| IMAGE_DIR = Path(__file__).parent / "college_photos_images" | ||
| SEARCH_URL = "https://completecollegephotolibrary.org/?s=laptop" | ||
| NUM_IMAGES = 10 | ||
|
|
||
|
|
||
| async def fetch_image_urls( | ||
| query_url: str = SEARCH_URL, max_images: int = NUM_IMAGES | ||
| ) -> AsyncGenerator[str, None]: | ||
| """Yield image URLs from the query results page.""" | ||
| async with httpx.AsyncClient() as client: | ||
| resp = await client.get(query_url) | ||
| resp.raise_for_status() | ||
| soup = BeautifulSoup(resp.text, "html.parser") | ||
| images = soup.find_all("img") | ||
| count = 0 | ||
| for img in images: | ||
| src = img.get("src") | ||
| if ( | ||
| src | ||
| and "collegephotolibrary.org" in src | ||
| and src.lower().endswith((".jpg", ".jpeg", ".png")) | ||
| ): | ||
| yield src | ||
| count += 1 | ||
| if count >= max_images: | ||
| break | ||
|
|
||
|
|
||
| async def download_images( | ||
| image_urls: list[str], dest_dir: Path = IMAGE_DIR | ||
| ) -> list[str]: | ||
| """Download images to the destination directory asynchronously.""" | ||
| dest_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| async def download_one(i: int, url: str) -> str | None: | ||
| try: | ||
| (resp := await client.get(url, timeout=10)).raise_for_status() | ||
| ext = Path(url).suffix or ".jpg" | ||
| filename = f"college_photo_{i + 1:07_}{ext}" | ||
| path = dest_dir / filename | ||
| path.write_bytes(resp.content) | ||
| return str(path) | ||
| except Exception as ex: # noqa: BLE001 | ||
| print(f"Failed to download {url}: {ex}") | ||
| return None | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| tasks = [download_one(i, url) for i, url in enumerate(image_urls)] | ||
| results = await asyncio.gather(*tasks) | ||
| return [result for result in results if result] | ||
| return None | ||
|
|
||
|
|
||
| async def create_college_photos_from_laptop_query() -> list[str]: | ||
| """Main entry point: fetch and save 10 images from the laptop query.""" | ||
| return await download_images([url async for url in fetch_image_urls()]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| results = asyncio.run(create_college_photos_from_laptop_query()) | ||
| print("Downloaded images:", results) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.