diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..86993d4 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,91 @@ +# Agent Guidelines for Flickr Mail + +This document provides context for AI agents working on this codebase. + +## Project Overview + +Flickr Mail is a Flask web application that helps users find photos on Flickr +for Wikipedia articles and contact photographers to request Creative Commons +licensing. + +## Architecture + +- **main.py**: Single-file Flask application containing all routes and logic +- **templates/**: Jinja2 templates using Bootstrap 5 for styling + - `base.html`: Base template with Bootstrap CSS/JS + - `combined.html`: Main UI template for search, results, and message composition + - `message.jinja`: Template for the permission request message body + - `show_error.html`: Error display template + +## Key Components + +### Flickr Search (`search_flickr`, `parse_flickr_search_results`) + +Searches Flickr by scraping the search results page. The page embeds JSON data +in a `modelExport` JavaScript variable which contains photo metadata. + +- Uses browser-like headers (`BROWSER_HEADERS`) to avoid blocks +- Parses embedded JSON by counting braces (not regex) to handle nested structures +- Returns `FlickrPhoto` dataclass instances with id, title, username, license, URLs + +### FlickrPhoto Dataclass + +Represents a photo with: +- `id`, `title`, `path_alias`, `owner_nsid`, `username`, `realname` +- `license` (int): Flickr license code (0=ARR, 4=CC BY, 5=CC BY-SA, etc.) +- `thumb_url`, `medium_url`: Static image URLs +- `flickr_url` property: URL to photo page +- `license_name` property: Human-readable license name + +### License Codes + +Wikipedia-compatible licenses (can be used): 4 (CC BY), 5 (CC BY-SA), 7 (No +known copyright), 8 (US Government), 9 (CC0), 10 (Public Domain). + +Not compatible: 0 (All Rights Reserved), 1-3 (NC variants), 6 (ND). + +### URL Validation (`is_valid_flickr_image_url`) + +Validates that image URLs passed via query params are from legitimate Flickr +static image servers: +- `live.staticflickr.com` +- `farm*.staticflickr.com` +- `c1.staticflickr.com`, `c2.staticflickr.com` + +### NSID Lookup (`flickr_usrename_to_nsid`) + +Converts a Flickr username/path alias to the NSID (internal user ID) needed +for the Flickr mail URL. Scrapes the user's profile page for embedded params. + +## Request Flow + +1. User enters Wikipedia article title/URL → `start()` extracts article name +2. `search_flickr()` fetches and parses Flickr search results +3. Results displayed as clickable photo grid with license badges +4. User clicks photo → page reloads with `flickr` and `img` params +5. `flickr_usrename_to_nsid()` looks up the photographer's NSID +6. Message template rendered with photo details +7. User copies message and clicks link to Flickr's mail compose page + +## Testing Changes + +Run the Flask app locally: +```bash +python3 main.py +``` +Then visit http://localhost:5000/ + +Test search functionality: +```python +from main import search_flickr +photos = search_flickr("Big Ben") +print(len(photos), photos[0].title, photos[0].license_name) +``` + +## Potential Improvements + +- Add pagination for search results (currently shows ~25 photos) +- Cache search results to reduce Flickr requests +- Add filtering by license type +- Handle Flickr rate limiting/blocks more gracefully +- Add tests for the parsing logic diff --git a/README.md b/README.md index 458b90d..b1e0633 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,14 @@ primary purpose is to simplify the process of discovering and contacting photographers on Flickr whose photos can be used to enhance Wikipedia articles. ### Key Features -- Search for Flickr photos related to specific Wikipedia articles. +- **Integrated Flickr search**: Enter a Wikipedia article title and see Flickr + photos directly in the interface - no need to visit Flickr's search page. +- **Photo grid with metadata**: Search results display as a grid of thumbnails + showing the photographer's name and license for each photo. +- **License highlighting**: Photos with Wikipedia-compatible licenses (CC BY, + CC BY-SA, CC0, Public Domain) are highlighted with a green badge. +- **One-click message composition**: Click any photo to compose a permission + request message with the photo displayed alongside. - Generate messages to request permission to use photos on Wikipedia. - Handle exceptions gracefully and provide detailed error information. @@ -29,10 +36,11 @@ To use the tool, follow these steps: 1. Start the tool by running the script. 2. Access the tool through a web browser. -3. Enter the Wikipedia URL for the article you want to find photos for. -4. Optionally, provide a Flickr URL if you already have one in mind. -5. The tool will extract relevant information and generate a message for you to - request permission to use the photo on Wikipedia. +3. Enter the Wikipedia article title or URL. +4. Browse the Flickr search results displayed in the interface. +5. Click on a photo to select it and compose a permission request message. +6. Copy the subject and message, then click "Send message on Flickr" to contact + the photographer. ## Error Handling