44
55import json
66import sys
7+ from datetime import datetime , timezone
78from typing import Any
89
910
1011def _json_compact (data : Any ) -> str :
1112 return json .dumps (data , ensure_ascii = False , separators = ("," , ":" ))
1213
1314
14- def print_results (data : dict , * , output : str = "table" , limit : int | None = None ) -> None :
15+ def _slim (doc : dict ) -> dict :
16+ """Strip a listing to agent-essential fields."""
17+ price = doc .get ("price" , {})
18+ out : dict [str , Any ] = {
19+ "id" : doc .get ("id" ),
20+ "heading" : doc .get ("heading" ),
21+ "price" : price .get ("amount" ),
22+ "currency" : price .get ("currency_code" , "SEK" ),
23+ "location" : doc .get ("location" ),
24+ "url" : doc .get ("canonical_url" ),
25+ }
26+ ts = doc .get ("timestamp" )
27+ if ts :
28+ out ["date" ] = datetime .fromtimestamp (ts / 1000 , tz = timezone .utc ).strftime ("%Y-%m-%d" )
29+ for extra in doc .get ("extras" , []):
30+ if extra .get ("id" ) == "brand" and extra .get ("values" ):
31+ out ["brand" ] = extra ["values" ][0 ]
32+ return out
33+
34+
35+ def print_results (data : dict , * , output : str = "table" , limit : int | None = None , raw : bool = False ) -> None :
1536 """Print search results."""
1637 docs = data .get ("docs" , [])
1738 total = data .get ("metadata" , {}).get ("result_size" , {}).get ("match_count" , len (docs ))
@@ -20,12 +41,13 @@ def print_results(data: dict, *, output: str = "table", limit: int | None = None
2041 docs = docs [:limit ]
2142
2243 if output == "json" :
23- print (_json_compact ({"total" : total , "results" : docs }))
44+ results = docs if raw else [_slim (d ) for d in docs ]
45+ print (_json_compact ({"total" : total , "results" : results }))
2446 return
2547
2648 if output == "jsonl" :
2749 for doc in docs :
28- print (_json_compact (doc ))
50+ print (_json_compact (doc if raw else _slim ( doc ) ))
2951 return
3052
3153 if not docs :
0 commit comments