django-agentweb 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentweb/__init__.py +17 -0
- agentweb/apps.py +15 -0
- agentweb/commerce/__init__.py +15 -0
- agentweb/commerce/cap_lite.py +59 -0
- agentweb/commerce/urls.py +15 -0
- agentweb/commerce/views.py +35 -0
- agentweb/conf.py +131 -0
- agentweb/discovery/__init__.py +7 -0
- agentweb/discovery/descriptors.py +249 -0
- agentweb/discovery/middleware.py +54 -0
- agentweb/discovery/urls.py +25 -0
- agentweb/discovery/views.py +55 -0
- agentweb/jsonld/__init__.py +35 -0
- agentweb/jsonld/builders.py +498 -0
- agentweb/jsonld/graph.py +80 -0
- agentweb/jsonld/profiles.py +260 -0
- agentweb/llms/__init__.py +7 -0
- agentweb/llms/sections.py +61 -0
- agentweb/llms/urls.py +72 -0
- agentweb/llms/validate.py +47 -0
- agentweb/llms/views.py +64 -0
- agentweb/management/__init__.py +0 -0
- agentweb/management/commands/__init__.py +0 -0
- agentweb/management/commands/generate_llms_txt.py +65 -0
- agentweb/sdf/__init__.py +6 -0
- agentweb/sdf/urls.py +15 -0
- agentweb/sdf/views.py +23 -0
- agentweb/static/agentweb/webmcp.js +109 -0
- agentweb/templates/agentweb/llms-full.txt +12 -0
- agentweb/templates/agentweb/llms.txt +11 -0
- agentweb/templatetags/__init__.py +0 -0
- agentweb/templatetags/agentweb_jsonld.py +38 -0
- agentweb/templatetags/agentweb_webmcp.py +63 -0
- agentweb/templatetags/agentweb_webmcp_decl.py +36 -0
- agentweb/urls.py +31 -0
- agentweb/webmcp/__init__.py +13 -0
- agentweb/webmcp/tools.py +99 -0
- agentweb/webmcp/urls.py +20 -0
- agentweb/webmcp/views.py +87 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py310-django42.txt +22 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py310-django52.txt +22 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py311-django42.txt +18 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py311-django52.txt +18 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py312-django42.txt +18 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py312-django52.txt +18 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py313-django52.txt +18 -0
- django_agentweb-0.0.1.data/data/share/django-agentweb/requirements/py314-django52.txt +18 -0
- django_agentweb-0.0.1.dist-info/METADATA +172 -0
- django_agentweb-0.0.1.dist-info/RECORD +52 -0
- django_agentweb-0.0.1.dist-info/WHEEL +5 -0
- django_agentweb-0.0.1.dist-info/licenses/LICENSE +21 -0
- django_agentweb-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Structured-data domain — Schema.org / JSON-LD.
|
|
2
|
+
|
|
3
|
+
Builders, ``build_graph`` assembler, and a consumer-agnostic profile registry
|
|
4
|
+
(``sitewide``, ``breadcrumb``, ``article``, ``faq``, ``lodging``,
|
|
5
|
+
``lodging_room``, plus additive ``recipe`` / ``person`` / ``local_business`` /
|
|
6
|
+
``review``). Render with ``{% jsonld_script %}``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from .builders import ( # noqa: F401
|
|
10
|
+
build_aggregate_offer,
|
|
11
|
+
build_article,
|
|
12
|
+
build_breadcrumb,
|
|
13
|
+
build_faq,
|
|
14
|
+
build_hotel,
|
|
15
|
+
build_hotel_room,
|
|
16
|
+
build_local_business,
|
|
17
|
+
build_offer,
|
|
18
|
+
build_organization,
|
|
19
|
+
build_person,
|
|
20
|
+
build_price_specification,
|
|
21
|
+
build_product,
|
|
22
|
+
build_recipe,
|
|
23
|
+
build_review,
|
|
24
|
+
build_webpage,
|
|
25
|
+
build_website,
|
|
26
|
+
normalize_type,
|
|
27
|
+
prices_match,
|
|
28
|
+
)
|
|
29
|
+
from .graph import build_graph, merge_graphs # noqa: F401
|
|
30
|
+
from .profiles import ( # noqa: F401
|
|
31
|
+
MVP_PROFILES,
|
|
32
|
+
PROFILES,
|
|
33
|
+
build_from_profiles,
|
|
34
|
+
resolve_profiles,
|
|
35
|
+
)
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
"""Schema.org JSON-LD builders (native dicts, no third-party JSON-LD libs).
|
|
2
|
+
|
|
3
|
+
Keys with ``None`` values are dropped. Pass ``context=False`` when a node will
|
|
4
|
+
sit inside a ``build_graph`` ``@graph`` that already declares ``@context``.
|
|
5
|
+
The typo ``Website`` is normalized to ``WebSite`` via :func:`normalize_type`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Dict, List, Optional, Sequence, Union
|
|
11
|
+
|
|
12
|
+
TYPE_ALIASES = {
|
|
13
|
+
"Website": "WebSite",
|
|
14
|
+
"website": "WebSite",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def normalize_type(type_name: str) -> str:
|
|
19
|
+
"""Normalize known Schema.org type misspellings (e.g. Website → WebSite)."""
|
|
20
|
+
return TYPE_ALIASES.get(type_name, type_name)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _clean(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
24
|
+
"""Drop keys whose value is ``None`` (recursively for nested dicts)."""
|
|
25
|
+
cleaned: Dict[str, Any] = {}
|
|
26
|
+
for key, value in data.items():
|
|
27
|
+
if value is None:
|
|
28
|
+
continue
|
|
29
|
+
if isinstance(value, dict):
|
|
30
|
+
value = _clean(value)
|
|
31
|
+
elif isinstance(value, list):
|
|
32
|
+
value = [
|
|
33
|
+
_clean(item) if isinstance(item, dict) else item
|
|
34
|
+
for item in value
|
|
35
|
+
if item is not None
|
|
36
|
+
]
|
|
37
|
+
cleaned[key] = value
|
|
38
|
+
return cleaned
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _with_id(node: Dict[str, Any], node_id: Optional[str]) -> Dict[str, Any]:
|
|
42
|
+
if node_id:
|
|
43
|
+
node = {"@id": node_id, **node}
|
|
44
|
+
return node
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _maybe_context(node: Dict[str, Any], context: bool) -> Dict[str, Any]:
|
|
48
|
+
if context:
|
|
49
|
+
return {"@context": "https://schema.org", **node}
|
|
50
|
+
return node
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def build_organization(
|
|
54
|
+
*,
|
|
55
|
+
name: str,
|
|
56
|
+
url: Optional[str] = None,
|
|
57
|
+
logo: Optional[str] = None,
|
|
58
|
+
same_as: Optional[Sequence[str]] = None,
|
|
59
|
+
node_id: Optional[str] = None,
|
|
60
|
+
context: bool = False,
|
|
61
|
+
) -> Dict[str, Any]:
|
|
62
|
+
"""Build an ``Organization`` node."""
|
|
63
|
+
node = _with_id(
|
|
64
|
+
{
|
|
65
|
+
"@type": "Organization",
|
|
66
|
+
"name": name,
|
|
67
|
+
"url": url,
|
|
68
|
+
"logo": logo,
|
|
69
|
+
"sameAs": list(same_as) if same_as else None,
|
|
70
|
+
},
|
|
71
|
+
node_id or (f"{url.rstrip('#')}#organization" if url else None),
|
|
72
|
+
)
|
|
73
|
+
return _maybe_context(_clean(node), context)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def build_website(
|
|
77
|
+
*,
|
|
78
|
+
name: str,
|
|
79
|
+
url: str,
|
|
80
|
+
publisher: Optional[Union[str, Dict[str, Any]]] = None,
|
|
81
|
+
in_language: Optional[str] = None,
|
|
82
|
+
node_id: Optional[str] = None,
|
|
83
|
+
context: bool = False,
|
|
84
|
+
) -> Dict[str, Any]:
|
|
85
|
+
"""Build a ``WebSite`` node (never ``Website``)."""
|
|
86
|
+
pub = publisher
|
|
87
|
+
if isinstance(publisher, str):
|
|
88
|
+
pub = {"@id": publisher}
|
|
89
|
+
node = _with_id(
|
|
90
|
+
{
|
|
91
|
+
"@type": "WebSite",
|
|
92
|
+
"name": name,
|
|
93
|
+
"url": url,
|
|
94
|
+
"publisher": pub,
|
|
95
|
+
"inLanguage": in_language,
|
|
96
|
+
},
|
|
97
|
+
node_id or f"{url.rstrip('#')}#website",
|
|
98
|
+
)
|
|
99
|
+
return _maybe_context(_clean(node), context)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def build_webpage(
|
|
103
|
+
*,
|
|
104
|
+
name: str,
|
|
105
|
+
url: str,
|
|
106
|
+
description: Optional[str] = None,
|
|
107
|
+
is_part_of: Optional[Union[str, Dict[str, Any]]] = None,
|
|
108
|
+
breadcrumb: Optional[Union[str, Dict[str, Any]]] = None,
|
|
109
|
+
node_id: Optional[str] = None,
|
|
110
|
+
context: bool = False,
|
|
111
|
+
) -> Dict[str, Any]:
|
|
112
|
+
"""Build a ``WebPage`` node."""
|
|
113
|
+
part = is_part_of
|
|
114
|
+
if isinstance(is_part_of, str):
|
|
115
|
+
part = {"@id": is_part_of}
|
|
116
|
+
crumb = breadcrumb
|
|
117
|
+
if isinstance(breadcrumb, str):
|
|
118
|
+
crumb = {"@id": breadcrumb}
|
|
119
|
+
node = _with_id(
|
|
120
|
+
{
|
|
121
|
+
"@type": "WebPage",
|
|
122
|
+
"name": name,
|
|
123
|
+
"url": url,
|
|
124
|
+
"description": description,
|
|
125
|
+
"isPartOf": part,
|
|
126
|
+
"breadcrumb": crumb,
|
|
127
|
+
},
|
|
128
|
+
node_id or url,
|
|
129
|
+
)
|
|
130
|
+
return _maybe_context(_clean(node), context)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def build_breadcrumb(
|
|
134
|
+
items: Sequence[Dict[str, str]],
|
|
135
|
+
*,
|
|
136
|
+
node_id: Optional[str] = None,
|
|
137
|
+
context: bool = False,
|
|
138
|
+
) -> Dict[str, Any]:
|
|
139
|
+
"""Build a ``BreadcrumbList`` from ``[{name, url}, ...]``."""
|
|
140
|
+
elements = []
|
|
141
|
+
for position, item in enumerate(items, start=1):
|
|
142
|
+
elements.append(
|
|
143
|
+
_clean(
|
|
144
|
+
{
|
|
145
|
+
"@type": "ListItem",
|
|
146
|
+
"position": position,
|
|
147
|
+
"name": item.get("name"),
|
|
148
|
+
"item": item.get("url"),
|
|
149
|
+
}
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
node = _with_id(
|
|
153
|
+
{"@type": "BreadcrumbList", "itemListElement": elements},
|
|
154
|
+
node_id,
|
|
155
|
+
)
|
|
156
|
+
return _maybe_context(_clean(node), context)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def build_article(
|
|
160
|
+
*,
|
|
161
|
+
headline: str,
|
|
162
|
+
url: Optional[str] = None,
|
|
163
|
+
description: Optional[str] = None,
|
|
164
|
+
date_published: Optional[str] = None,
|
|
165
|
+
date_modified: Optional[str] = None,
|
|
166
|
+
author: Optional[Union[str, Dict[str, Any]]] = None,
|
|
167
|
+
image: Optional[str] = None,
|
|
168
|
+
node_id: Optional[str] = None,
|
|
169
|
+
context: bool = False,
|
|
170
|
+
) -> Dict[str, Any]:
|
|
171
|
+
"""Build an ``Article`` node."""
|
|
172
|
+
auth = author
|
|
173
|
+
if isinstance(author, str):
|
|
174
|
+
auth = {"@type": "Person", "name": author}
|
|
175
|
+
node = _with_id(
|
|
176
|
+
{
|
|
177
|
+
"@type": "Article",
|
|
178
|
+
"headline": headline,
|
|
179
|
+
"url": url,
|
|
180
|
+
"description": description,
|
|
181
|
+
"datePublished": date_published,
|
|
182
|
+
"dateModified": date_modified,
|
|
183
|
+
"author": auth,
|
|
184
|
+
"image": image,
|
|
185
|
+
},
|
|
186
|
+
node_id or url,
|
|
187
|
+
)
|
|
188
|
+
return _maybe_context(_clean(node), context)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def build_faq(
|
|
192
|
+
questions: Sequence[Dict[str, str]],
|
|
193
|
+
*,
|
|
194
|
+
node_id: Optional[str] = None,
|
|
195
|
+
context: bool = False,
|
|
196
|
+
) -> Dict[str, Any]:
|
|
197
|
+
"""Build an ``FAQPage`` from ``[{question, answer}, ...]``."""
|
|
198
|
+
entities = []
|
|
199
|
+
for qa in questions:
|
|
200
|
+
entities.append(
|
|
201
|
+
_clean(
|
|
202
|
+
{
|
|
203
|
+
"@type": "Question",
|
|
204
|
+
"name": qa.get("question"),
|
|
205
|
+
"acceptedAnswer": {
|
|
206
|
+
"@type": "Answer",
|
|
207
|
+
"text": qa.get("answer"),
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
)
|
|
211
|
+
)
|
|
212
|
+
node = _with_id(
|
|
213
|
+
{"@type": "FAQPage", "mainEntity": entities},
|
|
214
|
+
node_id,
|
|
215
|
+
)
|
|
216
|
+
return _maybe_context(_clean(node), context)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def build_person(
|
|
220
|
+
*,
|
|
221
|
+
name: str,
|
|
222
|
+
url: Optional[str] = None,
|
|
223
|
+
job_title: Optional[str] = None,
|
|
224
|
+
image: Optional[str] = None,
|
|
225
|
+
same_as: Optional[Sequence[str]] = None,
|
|
226
|
+
node_id: Optional[str] = None,
|
|
227
|
+
context: bool = False,
|
|
228
|
+
) -> Dict[str, Any]:
|
|
229
|
+
"""Build a ``Person`` node (author / personal-site profiles)."""
|
|
230
|
+
node = _with_id(
|
|
231
|
+
{
|
|
232
|
+
"@type": "Person",
|
|
233
|
+
"name": name,
|
|
234
|
+
"url": url,
|
|
235
|
+
"jobTitle": job_title,
|
|
236
|
+
"image": image,
|
|
237
|
+
"sameAs": list(same_as) if same_as else None,
|
|
238
|
+
},
|
|
239
|
+
node_id or url,
|
|
240
|
+
)
|
|
241
|
+
return _maybe_context(_clean(node), context)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def build_local_business(
|
|
245
|
+
*,
|
|
246
|
+
name: str,
|
|
247
|
+
url: Optional[str] = None,
|
|
248
|
+
telephone: Optional[str] = None,
|
|
249
|
+
address: Optional[Dict[str, Any]] = None,
|
|
250
|
+
business_type: str = "LocalBusiness",
|
|
251
|
+
node_id: Optional[str] = None,
|
|
252
|
+
context: bool = False,
|
|
253
|
+
) -> Dict[str, Any]:
|
|
254
|
+
"""Build a ``LocalBusiness`` (or subtype e.g. HomeAndConstructionBusiness)."""
|
|
255
|
+
type_name = normalize_type(business_type)
|
|
256
|
+
node = _with_id(
|
|
257
|
+
{
|
|
258
|
+
"@type": type_name,
|
|
259
|
+
"name": name,
|
|
260
|
+
"url": url,
|
|
261
|
+
"telephone": telephone,
|
|
262
|
+
"address": address,
|
|
263
|
+
},
|
|
264
|
+
node_id or (f"{url.rstrip('#')}#business" if url else None),
|
|
265
|
+
)
|
|
266
|
+
return _maybe_context(_clean(node), context)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def build_recipe(
|
|
270
|
+
*,
|
|
271
|
+
name: str,
|
|
272
|
+
description: Optional[str] = None,
|
|
273
|
+
recipe_ingredient: Optional[Sequence[str]] = None,
|
|
274
|
+
recipe_instructions: Optional[Sequence[str]] = None,
|
|
275
|
+
image: Optional[str] = None,
|
|
276
|
+
node_id: Optional[str] = None,
|
|
277
|
+
context: bool = False,
|
|
278
|
+
) -> Dict[str, Any]:
|
|
279
|
+
"""Build a ``Recipe`` node (additive profile for content sites)."""
|
|
280
|
+
instructions = None
|
|
281
|
+
if recipe_instructions:
|
|
282
|
+
instructions = [
|
|
283
|
+
{"@type": "HowToStep", "text": step} for step in recipe_instructions
|
|
284
|
+
]
|
|
285
|
+
node = _with_id(
|
|
286
|
+
{
|
|
287
|
+
"@type": "Recipe",
|
|
288
|
+
"name": name,
|
|
289
|
+
"description": description,
|
|
290
|
+
"recipeIngredient": (
|
|
291
|
+
list(recipe_ingredient) if recipe_ingredient else None
|
|
292
|
+
),
|
|
293
|
+
"recipeInstructions": instructions,
|
|
294
|
+
"image": image,
|
|
295
|
+
},
|
|
296
|
+
node_id,
|
|
297
|
+
)
|
|
298
|
+
return _maybe_context(_clean(node), context)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def build_review(
|
|
302
|
+
*,
|
|
303
|
+
item_name: str,
|
|
304
|
+
review_body: Optional[str] = None,
|
|
305
|
+
rating_value: Optional[Any] = None,
|
|
306
|
+
best_rating: Optional[Any] = 5,
|
|
307
|
+
author: Optional[str] = None,
|
|
308
|
+
node_id: Optional[str] = None,
|
|
309
|
+
context: bool = False,
|
|
310
|
+
) -> Dict[str, Any]:
|
|
311
|
+
"""Build a ``Review`` node."""
|
|
312
|
+
rating = None
|
|
313
|
+
if rating_value is not None:
|
|
314
|
+
rating = _clean(
|
|
315
|
+
{
|
|
316
|
+
"@type": "Rating",
|
|
317
|
+
"ratingValue": rating_value,
|
|
318
|
+
"bestRating": best_rating,
|
|
319
|
+
}
|
|
320
|
+
)
|
|
321
|
+
node = _with_id(
|
|
322
|
+
{
|
|
323
|
+
"@type": "Review",
|
|
324
|
+
"itemReviewed": {"@type": "Thing", "name": item_name},
|
|
325
|
+
"reviewBody": review_body,
|
|
326
|
+
"reviewRating": rating,
|
|
327
|
+
"author": {"@type": "Person", "name": author} if author else None,
|
|
328
|
+
},
|
|
329
|
+
node_id,
|
|
330
|
+
)
|
|
331
|
+
return _maybe_context(_clean(node), context)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def build_product(
|
|
335
|
+
*,
|
|
336
|
+
name: str,
|
|
337
|
+
description: Optional[str] = None,
|
|
338
|
+
sku: Optional[str] = None,
|
|
339
|
+
offers: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
|
|
340
|
+
image: Optional[str] = None,
|
|
341
|
+
node_id: Optional[str] = None,
|
|
342
|
+
context: bool = False,
|
|
343
|
+
) -> Dict[str, Any]:
|
|
344
|
+
"""Build a ``Product`` node (CAP Lite / lodging room MTE)."""
|
|
345
|
+
node = _with_id(
|
|
346
|
+
{
|
|
347
|
+
"@type": "Product",
|
|
348
|
+
"name": name,
|
|
349
|
+
"description": description,
|
|
350
|
+
"sku": sku,
|
|
351
|
+
"offers": offers,
|
|
352
|
+
"image": image,
|
|
353
|
+
},
|
|
354
|
+
node_id,
|
|
355
|
+
)
|
|
356
|
+
return _maybe_context(_clean(node), context)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def build_aggregate_offer(
|
|
360
|
+
*,
|
|
361
|
+
low_price: Any,
|
|
362
|
+
high_price: Any,
|
|
363
|
+
currency: str,
|
|
364
|
+
offer_count: Optional[int] = None,
|
|
365
|
+
context: bool = False,
|
|
366
|
+
) -> Dict[str, Any]:
|
|
367
|
+
"""Build an ``AggregateOffer`` node."""
|
|
368
|
+
node = {
|
|
369
|
+
"@type": "AggregateOffer",
|
|
370
|
+
"lowPrice": low_price,
|
|
371
|
+
"highPrice": high_price,
|
|
372
|
+
"priceCurrency": currency,
|
|
373
|
+
"offerCount": offer_count,
|
|
374
|
+
}
|
|
375
|
+
return _maybe_context(_clean(node), context)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def build_price_specification(
|
|
379
|
+
price: Any,
|
|
380
|
+
currency: str,
|
|
381
|
+
*,
|
|
382
|
+
unit_code: Optional[str] = None,
|
|
383
|
+
valid_from: Optional[str] = None,
|
|
384
|
+
valid_through: Optional[str] = None,
|
|
385
|
+
) -> Dict[str, Any]:
|
|
386
|
+
"""Build a ``UnitPriceSpecification`` node."""
|
|
387
|
+
return _clean(
|
|
388
|
+
{
|
|
389
|
+
"@type": "UnitPriceSpecification",
|
|
390
|
+
"price": price,
|
|
391
|
+
"priceCurrency": currency,
|
|
392
|
+
"unitCode": unit_code,
|
|
393
|
+
"validFrom": valid_from,
|
|
394
|
+
"validThrough": valid_through,
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def build_offer(
|
|
400
|
+
*,
|
|
401
|
+
price: Any,
|
|
402
|
+
currency: str,
|
|
403
|
+
availability: Optional[str] = None,
|
|
404
|
+
url: Optional[str] = None,
|
|
405
|
+
valid_from: Optional[str] = None,
|
|
406
|
+
valid_through: Optional[str] = None,
|
|
407
|
+
price_specification: Optional[Dict[str, Any]] = None,
|
|
408
|
+
) -> Dict[str, Any]:
|
|
409
|
+
"""Build an ``Offer`` node."""
|
|
410
|
+
return _clean(
|
|
411
|
+
{
|
|
412
|
+
"@type": "Offer",
|
|
413
|
+
"price": price,
|
|
414
|
+
"priceCurrency": currency,
|
|
415
|
+
"availability": availability,
|
|
416
|
+
"url": url,
|
|
417
|
+
"validFrom": valid_from,
|
|
418
|
+
"validThrough": valid_through,
|
|
419
|
+
"priceSpecification": price_specification,
|
|
420
|
+
}
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def build_hotel_room(
|
|
425
|
+
*,
|
|
426
|
+
name: str,
|
|
427
|
+
description: Optional[str] = None,
|
|
428
|
+
occupancy: Optional[int] = None,
|
|
429
|
+
bed_type: Optional[str] = None,
|
|
430
|
+
offers: Optional[List[Dict[str, Any]]] = None,
|
|
431
|
+
amenities: Optional[Sequence[str]] = None,
|
|
432
|
+
as_product: bool = False,
|
|
433
|
+
node_id: Optional[str] = None,
|
|
434
|
+
context: bool = False,
|
|
435
|
+
) -> Dict[str, Any]:
|
|
436
|
+
"""Build a ``HotelRoom`` node; optionally multi-type ``[HotelRoom, Product]``."""
|
|
437
|
+
types: Union[str, List[str]] = (
|
|
438
|
+
["HotelRoom", "Product"] if as_product else "HotelRoom"
|
|
439
|
+
)
|
|
440
|
+
amenity_features = None
|
|
441
|
+
if amenities:
|
|
442
|
+
amenity_features = [
|
|
443
|
+
{"@type": "LocationFeatureSpecification", "name": a}
|
|
444
|
+
for a in amenities
|
|
445
|
+
]
|
|
446
|
+
node = _with_id(
|
|
447
|
+
{
|
|
448
|
+
"@type": types,
|
|
449
|
+
"name": name,
|
|
450
|
+
"description": description,
|
|
451
|
+
"occupancy": occupancy,
|
|
452
|
+
"bed": bed_type,
|
|
453
|
+
"offers": offers,
|
|
454
|
+
"amenityFeature": amenity_features,
|
|
455
|
+
},
|
|
456
|
+
node_id,
|
|
457
|
+
)
|
|
458
|
+
return _maybe_context(_clean(node), context)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def build_hotel(
|
|
462
|
+
*,
|
|
463
|
+
name: str,
|
|
464
|
+
url: Optional[str] = None,
|
|
465
|
+
description: Optional[str] = None,
|
|
466
|
+
address: Optional[Dict[str, Any]] = None,
|
|
467
|
+
telephone: Optional[str] = None,
|
|
468
|
+
star_rating: Optional[Any] = None,
|
|
469
|
+
rooms: Optional[List[Dict[str, Any]]] = None,
|
|
470
|
+
node_id: Optional[str] = None,
|
|
471
|
+
context: bool = True,
|
|
472
|
+
) -> Dict[str, Any]:
|
|
473
|
+
"""Build a ``Hotel`` / ``LodgingBusiness``-compatible node."""
|
|
474
|
+
rating = None
|
|
475
|
+
if star_rating is not None:
|
|
476
|
+
rating = {"@type": "Rating", "ratingValue": star_rating}
|
|
477
|
+
node = _with_id(
|
|
478
|
+
{
|
|
479
|
+
"@type": "Hotel",
|
|
480
|
+
"name": name,
|
|
481
|
+
"url": url,
|
|
482
|
+
"description": description,
|
|
483
|
+
"address": address,
|
|
484
|
+
"telephone": telephone,
|
|
485
|
+
"starRating": rating,
|
|
486
|
+
"containsPlace": rooms,
|
|
487
|
+
},
|
|
488
|
+
node_id or (f"{url.rstrip('#')}#hotel" if url else None),
|
|
489
|
+
)
|
|
490
|
+
return _maybe_context(_clean(node), context)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
def prices_match(jsonld_price: Any, visible_price: Any) -> bool:
|
|
494
|
+
"""Return True when JSON-LD offer price matches the visible UI price."""
|
|
495
|
+
try:
|
|
496
|
+
return float(jsonld_price) == float(visible_price)
|
|
497
|
+
except (TypeError, ValueError):
|
|
498
|
+
return str(jsonld_price).strip() == str(visible_price).strip()
|
agentweb/jsonld/graph.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Assemble a single Schema.org ``@graph`` with stable ``@id`` deduplication."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, List, Optional
|
|
6
|
+
|
|
7
|
+
from .builders import build_organization, build_website, normalize_type
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _strip_context(node: Dict[str, Any]) -> Dict[str, Any]:
|
|
11
|
+
return {k: v for k, v in node.items() if k != "@context"}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _normalize_types(node: Dict[str, Any]) -> Dict[str, Any]:
|
|
15
|
+
raw = node.get("@type")
|
|
16
|
+
if isinstance(raw, str):
|
|
17
|
+
node = {**node, "@type": normalize_type(raw)}
|
|
18
|
+
elif isinstance(raw, list):
|
|
19
|
+
node = {**node, "@type": [normalize_type(t) for t in raw]}
|
|
20
|
+
return node
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_graph(
|
|
24
|
+
*nodes: Dict[str, Any],
|
|
25
|
+
site_url: Optional[str] = None,
|
|
26
|
+
include_sitewide: bool = False,
|
|
27
|
+
organization: Optional[Dict[str, Any]] = None,
|
|
28
|
+
website: Optional[Dict[str, Any]] = None,
|
|
29
|
+
organization_name: Optional[str] = None,
|
|
30
|
+
) -> Dict[str, Any]:
|
|
31
|
+
"""Build ``{"@context": schema.org, "@graph": [...]}`` with ``@id`` dedupe.
|
|
32
|
+
|
|
33
|
+
Nodes that share the same ``@id`` collapse to the first occurrence
|
|
34
|
+
(reference-don't-clone). Nested ``@context`` keys are stripped so the
|
|
35
|
+
document has a single top-level context.
|
|
36
|
+
"""
|
|
37
|
+
graph: List[Dict[str, Any]] = []
|
|
38
|
+
seen: Dict[str, int] = {}
|
|
39
|
+
|
|
40
|
+
def _add(node: Optional[Dict[str, Any]]) -> None:
|
|
41
|
+
if not node:
|
|
42
|
+
return
|
|
43
|
+
cleaned = _normalize_types(_strip_context(dict(node)))
|
|
44
|
+
node_id = cleaned.get("@id")
|
|
45
|
+
if node_id and node_id in seen:
|
|
46
|
+
return
|
|
47
|
+
if node_id:
|
|
48
|
+
seen[node_id] = len(graph)
|
|
49
|
+
graph.append(cleaned)
|
|
50
|
+
|
|
51
|
+
if include_sitewide and site_url:
|
|
52
|
+
org = organization or build_organization(
|
|
53
|
+
name=organization_name or site_url,
|
|
54
|
+
url=site_url,
|
|
55
|
+
context=False,
|
|
56
|
+
)
|
|
57
|
+
web = website or build_website(
|
|
58
|
+
name=organization_name or site_url,
|
|
59
|
+
url=site_url,
|
|
60
|
+
publisher=org.get("@id"),
|
|
61
|
+
context=False,
|
|
62
|
+
)
|
|
63
|
+
_add(org)
|
|
64
|
+
_add(web)
|
|
65
|
+
|
|
66
|
+
for node in nodes:
|
|
67
|
+
_add(node)
|
|
68
|
+
|
|
69
|
+
return {"@context": "https://schema.org", "@graph": graph}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def merge_graphs(*graphs: Dict[str, Any]) -> Dict[str, Any]:
|
|
73
|
+
"""Merge multiple graph documents, deduping by ``@id``."""
|
|
74
|
+
nodes: List[Dict[str, Any]] = []
|
|
75
|
+
for doc in graphs:
|
|
76
|
+
nodes.extend(doc.get("@graph") or [])
|
|
77
|
+
# Also accept a lone typed node.
|
|
78
|
+
if "@type" in doc and "@graph" not in doc:
|
|
79
|
+
nodes.append(doc)
|
|
80
|
+
return build_graph(*nodes)
|