Deploy with Docker Compose / deploy (push) Successful in 1m31s
A Violentmonkey userscript, served by the site and installed from /cv/jobs, adds the posting open in the browser (LinkedIn, an ATS, any company page) to the tracker. Claude reads the title, company and location off the page text. - Three token-guarded REST routes under /api/jobs/capture (status, extract, upsert). They take a bearer token, JOB_CAPTURE_TOKEN, because the auth cookies are SameSite=Lax and never sent from another origin. An unset token switches the routes off. - The upsert matches on a canonical posting URL, then (company, job title), so a capture and the email pipeline do not make two rows for one job. - The Claude call is a new internal Python route, /internal/jobs/extract, alongside the rowing reader; the client and fence stripping they share move to python/app/claude.py. - New "saved" status ranked below "applied", and a description column holding the posting text, shown per row on the tracker. Co-Authored-By: Claude Opus 5.5 <[email protected]>
99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
"""Read a rowing machine display from a photo with Claude.
|
|
|
|
This is the image half of the Go backend's admin-only POST /rowing. The Go
|
|
handler keeps everything that needs the database or the session cookie - the
|
|
admin gate, the EXIF date, the duplicate check, the sanity bounds, the insert -
|
|
and hands the image here only once it has decided the Claude call is worth
|
|
paying for. This module just turns pixels into three numbers.
|
|
|
|
Like `travelodge`, it never imports FastAPI; `main` is the HTTP skin.
|
|
"""
|
|
|
|
import logging
|
|
|
|
import anthropic
|
|
from pydantic import BaseModel, Field, ValidationError
|
|
|
|
# Re-exported under the names this module has always had: `main` and the tests
|
|
# reach the client and the exceptions through `rowing`.
|
|
from .claude import MODEL, NotConfigured, ReadError, strip_markdown_fence
|
|
from .claude import client as _client
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
# The answer is a three-key JSON object, so this is a ceiling, not a target.
|
|
MAX_TOKENS = 256
|
|
|
|
# The keys named here are the JSON contract: RowingReading's field names below
|
|
# and ExtractedRowingData in backend/handlers/handle_rowing.go must match them.
|
|
PROMPT = """Look at this rowing machine display. Extract the total elapsed time and total distance.
|
|
|
|
Return ONLY a JSON object with these exact keys and numeric values:
|
|
- "timeMinutes": total minutes (e.g. 2:30 = 2)
|
|
- "timeSeconds": total seconds (e.g. 2:30 = 30)
|
|
- "distance": distance in meters as a number (e.g. 5000)
|
|
|
|
No text, no markdown, no explanation. Just the JSON object."""
|
|
|
|
|
|
class RowingReading(BaseModel):
|
|
"""What Claude read off the display.
|
|
|
|
camelCase on purpose: these are the keys the prompt asks for and the Go
|
|
handler unmarshals. A key the model left out defaults to 0, which the Go
|
|
side's sanity bounds then reject - the same outcome as when Go parsed the
|
|
model's output itself.
|
|
"""
|
|
|
|
timeMinutes: int = Field(0, ge=0)
|
|
timeSeconds: int = Field(0, ge=0)
|
|
distance: int = Field(0, ge=0)
|
|
|
|
|
|
def read_display(media_type: str, data: str) -> RowingReading:
|
|
"""Ask Claude for the elapsed time and distance shown in an image.
|
|
|
|
Args:
|
|
media_type: one of the image types the Anthropic API accepts; the
|
|
caller is responsible for the allow-list.
|
|
data: the image, base64-encoded (no data: prefix).
|
|
|
|
Raises:
|
|
NotConfigured: no API key.
|
|
ReadError: the API call failed, or the reply was empty or not the JSON
|
|
object that was asked for.
|
|
"""
|
|
try:
|
|
message = _client().messages.create(
|
|
model=MODEL,
|
|
max_tokens=MAX_TOKENS,
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "image",
|
|
"source": {"type": "base64", "media_type": media_type, "data": data},
|
|
},
|
|
{"type": "text", "text": PROMPT},
|
|
],
|
|
}
|
|
],
|
|
)
|
|
except anthropic.APIStatusError as e:
|
|
log.warning("Claude API error %s: %s", e.status_code, e.message)
|
|
raise ReadError("failed to process image") from e
|
|
except anthropic.APIConnectionError as e:
|
|
log.warning("Claude API unreachable: %s", e)
|
|
raise ReadError("failed to process image") from e
|
|
|
|
text = next((block.text for block in message.content if block.type == "text"), None)
|
|
if text is None:
|
|
raise ReadError("empty response from image processor")
|
|
|
|
try:
|
|
return RowingReading.model_validate_json(strip_markdown_fence(text))
|
|
except ValidationError as e:
|
|
log.warning("unparseable model output %r: %s", text, e)
|
|
raise ReadError("failed to parse image data") from e
|