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7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from __future__ import annotations

import os
import re
from enum import Enum
from typing import List, Optional
from urllib.parse import urljoin

import requests
from bs4 import BeautifulSoup, NavigableString, Tag
from fastapi import FastAPI, HTTPException
from fastapi.concurrency import run_in_threadpool
from pydantic import BaseModel, Field


class FieldName(str, Enum):
    """Fields exposed by the NIST advanced search form."""

    reactants = "reactants"
    products = "products"
    rxn_order = "rxn_order"
    ref_rxn_reactants = "ref_rxn_reactants"
    ref_rxn_products = "ref_rxn_products"
    ref_rxn_order = "ref_rxn_order"
    t_low = "t_low"
    t_high = "t_high"
    p_low = "p_low"
    p_high = "p_high"
    bath_gas = "bath_gas"
    squib = "kinetics.squib"


class Relation(str, Enum):
    """Comparison operators supported by NIST."""

    equals = "="
    not_equals = "!="  # HTML also exposes "<>" which NIST treats the same
    contains = "~*"
    not_contains = "!~*"
    lt = "<"
    lte = "<="
    gte = ">="
    gt = ">"


class LogicalOperator(str, Enum):
    and_ = "and"
    or_ = "or"


class LeftParenthesis(str, Enum):
    none = ""
    single = "("
    double = "(("


class RightParenthesis(str, Enum):
    none = ""
    single = ")"
    double = "))"


class Category(int, Enum):
    any = 0
    review = 1
    experiment = 2
    theory = 3


MAX_FIELDS = 5
SUMMARY_HEADER = "Reaction"


class SearchFilter(BaseModel):
    field: FieldName = Field(default=FieldName.reactants, description="Field to query")
    relation: Relation = Field(default=Relation.contains, description="Comparison operator")
    value: str = Field(..., min_length=0, description="Text entered into the NIST form field")
    boolean: LogicalOperator | None = Field(
        default=LogicalOperator.and_,
        description="Boolean operator to join this filter with the previous one. Ignored for the first row.",
    )
    left_parenthesis: LeftParenthesis = Field(default=LeftParenthesis.none)
    right_parenthesis: RightParenthesis = Field(default=RightParenthesis.none)


class SearchRequest(BaseModel):
    filters: List[SearchFilter] = Field(
        default_factory=list,
        min_length=1,
        max_length=MAX_FIELDS,
        description="Up to five filter rows mirroring the NIST form layout.",
    )
    decomposition_only: bool = Field(default=False, description="Populate the `decomp` checkbox on the site.")
    category: Category = Field(default=Category.any, description="Optional category restriction.")
    units: str | None = Field(
        default=None,
        description="Optional string passed through to the hidden Units field (rarely needed).",
    )


class ReactionSummary(BaseModel):
    record_count: int
    reaction: str
    detail_url: str


class SearchResponse(BaseModel):
    result_count: int
    results: List[ReactionSummary]


class ReactionDetailRequest(BaseModel):
    detail_url: str = Field(
        ...,
        description="Absolute or relative URL returned by the /api/search endpoint (e.g. /kinetics/ReactionSearch?...).",
    )


class RateExpressionUnits(BaseModel):
    first_order: Optional[str] = None
    second_order: Optional[str] = None
    third_order: Optional[str] = None


class PhysicalUnits(BaseModel):
    energy: Optional[str] = None
    molecular: Optional[str] = None
    pressure: Optional[str] = None
    temperature: Optional[str] = None
    base_volume: Optional[str] = None
    reference_temperature: Optional[str] = None
    evaluation_temperature: Optional[str] = None


class KineticsRecord(BaseModel):
    section: Optional[str] = Field(None, description="Section heading such as Theory or Experiment.")
    dataset_id: Optional[str] = None
    squib: Optional[str] = None
    squib_url: Optional[str] = None
    temperature_range: Optional[str] = None
    pre_exponential_factor: Optional[str] = None
    temperature_exponent: Optional[str] = None
    activation_energy: Optional[str] = None
    rate_at_298: Optional[str] = None
    reaction_order: Optional[str] = None


class ReactionDetail(BaseModel):
    title: Optional[str]
    rate_expression: Optional[str]
    rate_expression_units: RateExpressionUnits | None
    physical_units: PhysicalUnits | None
    datasets: List[KineticsRecord]


class NistKineticsClient:
    """
    Thin wrapper that mirrors the public HTML form exposed on https://kinetics.nist.gov.

    The official site does not provide a documented JSON API. This class assembles the same payload
    the browser would send, performs the POST, and parses the resulting HTML.
    """

    BASE_URL = "https://kinetics.nist.gov/kinetics/"
    SEARCH_ENDPOINT = "Search.jsp"

    def __init__(self, timeout: int = 30) -> None:
        self.timeout = timeout
        self.session = requests.Session()
        self.session.headers.update(
            {
                "User-Agent": (
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                    "AppleWebKit/537.36 (KHTML, like Gecko) "
                    "Chrome/124.0.0.0 Safari/537.36"
                ),
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Language": "en-US,en;q=0.9",
            }
        )

    def search(self, request: SearchRequest) -> List[ReactionSummary]:
        payload = self._build_payload(request)
        html = self._post(self.SEARCH_ENDPOINT, payload)
        return self._parse_search_results(html)

    def fetch_reaction_detail(self, detail_url: str) -> ReactionDetail:
        sanitized_url = self._sanitize_url(detail_url)
        html = self._get_absolute(sanitized_url)
        return self._parse_reaction_detail(html, sanitized_url)

    def _post(self, endpoint: str, payload: dict[str, str]) -> str:
        response = self.session.post(urljoin(self.BASE_URL, endpoint), data=payload, timeout=self.timeout)
        response.raise_for_status()
        return response.text

    def _get_absolute(self, url: str) -> str:
        absolute_url = urljoin(self.BASE_URL, url)
        response = self.session.get(absolute_url, timeout=self.timeout)
        response.raise_for_status()
        return response.text

    def _build_payload(self, request: SearchRequest) -> dict[str, str]:
        payload: dict[str, str] = {
            "doc": "SearchForm",
            "type": "java",
            "database": "kinetics",
            "numberOfFields": str(MAX_FIELDS),
            "category": str(int(request.category.value)),
        }

        if request.units:
            payload["Units"] = request.units

        if request.decomposition_only:
            payload["decomp"] = "true"

        filters = request.filters
        for idx in range(MAX_FIELDS):
            human_index = idx + 1
            boolean_key = f"boolean{human_index}"
            lp_key = f"lp{human_index}"
            field_key = f"field{human_index}"
            relate_key = f"relate{human_index}"
            text_key = f"text{human_index}"
            rp_key = f"rp{human_index}"

            if idx < len(filters):
                view = filters[idx]
                payload[boolean_key] = "" if idx == 0 else (view.boolean.value if view.boolean else "and")
                payload[lp_key] = self._format_parenthesis(view.left_parenthesis)
                payload[field_key] = view.field.value
                payload[relate_key] = view.relation.value
                payload[text_key] = view.value
                payload[rp_key] = self._format_parenthesis(view.right_parenthesis)
            else:
                payload[boolean_key] = "" if idx == 0 else "and"
                payload[lp_key] = " "
                payload[field_key] = FieldName.reactants.value
                payload[relate_key] = Relation.equals.value
                payload[text_key] = ""
                payload[rp_key] = " "

        return payload

    @staticmethod
    def _format_parenthesis(choice: LeftParenthesis | RightParenthesis) -> str:
        return choice.value if choice.value else " "

    def _parse_search_results(self, html: str) -> List[ReactionSummary]:
        soup = BeautifulSoup(html, "html.parser")
        table = None
        intro = soup.find(string=lambda text: isinstance(text, str) and "Click on a link in the table" in text)
        if intro:
            if isinstance(intro, NavigableString):
                intro_parent = intro.parent
            else:
                intro_parent = intro
            table = intro_parent.find_next("table")
        if not table:
            table = self._find_table_with_headers(soup, ["Records", SUMMARY_HEADER])
        if not table:
            # When 0 records are returned, no table is rendered.
            return []

        summaries: List[ReactionSummary] = []
        for row in table.find_all("tr")[1:]:
            cells = row.find_all("td")
            if len(cells) < 3:
                continue

            count_text = cells[0].get_text(strip=True)
            match = re.search(r"(\d+)", count_text)
            record_count = int(match.group(1)) if match else 0

            anchor = cells[0].find("a")
            href = anchor["href"] if anchor and anchor.has_attr("href") else None
            detail_url = self._sanitize_url(href) if href else ""
            reaction_str = cells[-1].get_text(" ", strip=True)
            summaries.append(
                ReactionSummary(record_count=record_count, reaction=reaction_str, detail_url=detail_url)
            )

        return summaries

    def _parse_reaction_detail(self, html: str, source_url: str) -> ReactionDetail:
        soup = BeautifulSoup(html, "html.parser")
        title_div = soup.find("div", attrs={"align": "center"})
        title = title_div.get_text(" ", strip=True) if title_div else None

        rate_expression = None
        for bold in soup.find_all("b"):
            text = bold.get_text(strip=True)
            if text.startswith("Rate expression"):
                rate_expression = text.replace("\xa0", " ")
                break

        rate_units = self._parse_rate_units(soup)
        physical_units = self._parse_physical_units(soup)
        datasets = self._parse_kinetics_table(soup)

        return ReactionDetail(
            title=title,
            rate_expression=rate_expression,
            rate_expression_units=rate_units,
            physical_units=physical_units,
            datasets=datasets,
        )

    @staticmethod
    def _parse_rate_units(soup: BeautifulSoup) -> RateExpressionUnits | None:
        anchor = None
        for bold in soup.find_all("b"):
            if "Rate expression units" in bold.get_text():
                anchor = bold
                break
        if not anchor:
            return None

        values: dict[str, str] = {}
        current_label: Optional[str] = None
        for node in anchor.next_siblings:
            if isinstance(node, Tag) and node.name == "b":
                break
            if isinstance(node, Tag) and node.name == "hr":
                break
            if isinstance(node, NavigableString):
                text = str(node).strip()
            else:
                text = node.get_text(" ", strip=True)
            if not text:
                continue
            if text.endswith(":"):
                current_label = text[:-1]
                continue
            if current_label:
                values[current_label] = text
                current_label = None

        if not values:
            return None

        return RateExpressionUnits(
            first_order=values.get("First order"),
            second_order=values.get("Second order"),
            third_order=values.get("Third order"),
        )

    @staticmethod
    def _parse_physical_units(soup: BeautifulSoup) -> PhysicalUnits | None:
        table = None
        for candidate in soup.find_all("table"):
            if candidate.find(string=lambda text: isinstance(text, str) and "Energy Units" in text):
                table = candidate
                break
        if not table:
            return None

        fields = {}
        for row in table.find_all("tr"):
            cells = row.find_all("td")
            if len(cells) < 7:
                continue
            label_a = cells[0].get_text(strip=True)
            value_a = cells[2].get_text(strip=True)
            label_b = cells[4].get_text(strip=True)
            value_b = cells[6].get_text(strip=True)
            if label_a:
                fields[label_a] = value_a
            if label_b:
                fields[label_b] = value_b

        if not fields:
            return None

        return PhysicalUnits(
            energy=fields.get("Energy Units"),
            molecular=fields.get("Molecular Units"),
            pressure=fields.get("Pressure Units"),
            temperature=fields.get("Temperature Units"),
            base_volume=fields.get("Base Volume Unit"),
            reference_temperature=fields.get("Reference Temperature"),
            evaluation_temperature=fields.get("Evaluation Temperature"),
        )

    def _parse_kinetics_table(self, soup: BeautifulSoup) -> List[KineticsRecord]:
        table = self._find_table_with_headers(soup, ["Squib", "Temp [K]"])
        if not table:
            return []

        datasets: List[KineticsRecord] = []
        current_section: Optional[str] = None

        for row in table.find_all("tr")[1:]:
            header_cells = row.find_all("th")
            if header_cells:
                continue

            data_cells = row.find_all("td")
            if not data_cells:
                continue

            if data_cells[0].get("colspan"):
                current_section = data_cells[0].get_text(" ", strip=True)
                continue

            if len(data_cells) < 15:
                continue

            dataset_id = self._find_hidden_value(data_cells[0], prefix="id")
            squib_cell = data_cells[2]
            squib_link = squib_cell.find("a")
            squib_text = squib_link.get_text(strip=True) if squib_link else squib_cell.get_text(strip=True)
            squib_url = self._sanitize_url(squib_link["href"]) if squib_link and squib_link.has_attr("href") else None

            record = KineticsRecord(
                section=current_section,
                dataset_id=dataset_id,
                squib=squib_text,
                squib_url=squib_url,
                temperature_range=data_cells[4].get_text(strip=True) or None,
                pre_exponential_factor=data_cells[6].get_text(strip=True) or None,
                temperature_exponent=data_cells[8].get_text(strip=True) or None,
                activation_energy=data_cells[10].get_text(strip=True) or None,
                rate_at_298=data_cells[12].get_text(strip=True) or None,
                reaction_order=data_cells[14].get_text(strip=True) or None,
            )
            datasets.append(record)

        return datasets

    @staticmethod
    def _find_hidden_value(cell, prefix: str) -> Optional[str]:
        for hidden in cell.find_all("input"):
            name = hidden.get("name") or ""
            if name.startswith(prefix):
                return hidden.get("value")
        return None

    @staticmethod
    def _sanitize_url(url: Optional[str]) -> str:
        if not url:
            return ""
        cleaned = re.sub(r";jsessionid=[^?]*", "", url)
        return urljoin(NistKineticsClient.BASE_URL, cleaned)

    @staticmethod
    def _find_table_with_headers(soup: BeautifulSoup, headers: List[str]):
        required = [header.strip() for header in headers]
        for table in soup.find_all("table"):
            th_texts = [th.get_text(strip=True) for th in table.find_all("th")]
            if all(req in th_texts for req in required):
                return table
        return None


client = NistKineticsClient()
app = FastAPI(
    title="Unofficial NIST Kinetics API",
    description=(
        "A thin REST wrapper around the public NIST Chemical Kinetics search form. "
        "All data is fetched live from kinetics.nist.gov; please cache results and respect their terms of use."
    ),
    version="1.0.0",
)


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "ok"}


@app.post("/api/search", response_model=SearchResponse)
async def search(request: SearchRequest) -> SearchResponse:
    try:
        results = await run_in_threadpool(client.search, request)
        return SearchResponse(result_count=len(results), results=results)
    except requests.HTTPError as exc:
        raise HTTPException(status_code=exc.response.status_code, detail=str(exc)) from exc
    except Exception as exc:  # pragma: no cover - defensive
        raise HTTPException(status_code=502, detail=f"Failed to query NIST: {exc}") from exc


@app.post("/api/reaction-detail", response_model=ReactionDetail)
async def reaction_detail(request: ReactionDetailRequest) -> ReactionDetail:
    try:
        return await run_in_threadpool(client.fetch_reaction_detail, request.detail_url)
    except requests.HTTPError as exc:
        raise HTTPException(status_code=exc.response.status_code, detail=str(exc)) from exc
    except Exception as exc:  # pragma: no cover - defensive
        raise HTTPException(status_code=502, detail=f"Failed to fetch reaction detail: {exc}") from exc


if __name__ == "__main__":  # pragma: no cover
    import uvicorn

    port = int(os.environ.get("PORT", os.environ.get("HF_SPACE_PORT", 7860)))
    uvicorn.run("nist_kinetics_api:app", host="0.0.0.0", port=port, reload=bool(os.environ.get("DEBUG")))