File size: 9,997 Bytes
5c9c324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa64aba
5c9c324
 
 
 
 
 
 
 
 
 
 
 
aa64aba
 
 
5c9c324
 
 
 
 
 
aa64aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c9c324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
from __future__ import annotations

"""
TGSC (The Good Scents Company) material scraper.

This module fetches raw material HTML pages from TGSC and extracts the
properties we need for realistic physics:
  - Boiling point (K)
  - Vapor pressure (mmHg -> Pa)
  - Odor description string

The parser is intentionally regex-driven against the page text because TGSC
pages are legacy hand-written HTML without stable semantic markup. Callers can
pass either a URL, a raw HTML string, or a Camoufox page object.
"""

import logging
import re
from typing import Any

try:
    from camoufox.sync_api import Camoufox
except Exception:  # pragma: no cover - optional dependency for stealth mode
    Camoufox = None

import requests
from bs4 import BeautifulSoup

logger = logging.getLogger("pino.scraper")

# Conversion factor: 1 mmHg = 133.3224 Pa (exact definition used in PINO).
MMHG_TO_PA = 133.3224

# Default room temperature for boiling-point conversions if only Celsius is given.
ROOM_TEMP_K = 298.15


def _kelvin_from_string(text: str) -> float | None:
    """Try to extract a temperature in Kelvin or Celsius from free text."""
    # Celsius: 123.45 °C / 123.45 C
    c_match = re.search(r"([\d\.]+)\s*°?C", text)
    if c_match:
        c = float(c_match.group(1))
        return c + 273.15
    # Kelvin: 123.45 K
    k_match = re.search(r"([\d\.]+)\s*K\b", text)
    if k_match:
        return float(k_match.group(1))
    return None


def _extract_boiling_point(page_text: str) -> float | None:
    """
    Extract boiling point from page text.

    TGSC usually labels this as "Boiling Point:" followed by a temperature range.
    We take the midpoint of the range.
    """
    # Look for a labelled boiling point line, e.g. "Boiling Point: 176.00 to 177.00 °C. @ 760.00 mm Hg"
    bp_match = re.search(
        r"Boiling Point:\s*([\d\.]+)\s*(?:to\s+([\d\.]+))?\s*°?C",
        page_text,
        re.IGNORECASE,
    )
    if bp_match:
        low = float(bp_match.group(1))
        high = float(bp_match.group(2)) if bp_match.group(2) else low
        return (low + high) / 2.0 + 273.15
    # Fallback: any phrase like "bp 123.45 °C".
    fallback = re.search(r"\bb\.?p\.?\s*([\d\.]+)\s*°?C", page_text, re.IGNORECASE)
    if fallback:
        return float(fallback.group(1)) + 273.15
    return None


def _extract_vapor_pressure(page_text: str) -> float | None:
    """
    Extract vapor pressure in mmHg and convert to Pascals.

    TGSC labels this as "Vapor Pressure: 0.003000 mmHg @ 25.00 °C. (est)".
    We always convert the mmHg value to Pa using MMHG_TO_PA.
    """
    vp_match = re.search(
        r"Vapor Pressure:\s*([\d\.]+)\s*mmHg", page_text, re.IGNORECASE
    )
    if vp_match:
        vp_mmhg = float(vp_match.group(1))
        return vp_mmhg * MMHG_TO_PA
    return None


def _extract_odor_description(page_text: str) -> str:
    """
    Extract the odor description keyword string.

    TGSC labels this as "Odor Description: ...". We capture the remainder of the
    line and strip the trailing attribution/reference text.
    """
    odor_match = re.search(
        r"Odor Description:\s*at\s+\d+(?:\.\d+)?\s*\%\.\s*([^\n\r]+)",
        page_text,
        re.IGNORECASE,
    )
    if odor_match:
        return odor_match.group(1).strip().split("  ")[0].strip()
    odor_match = re.search(
        r"Odor Description:\s*([^\n\r]+)", page_text, re.IGNORECASE
    )
    if odor_match:
        return odor_match.group(1).strip().split("  ")[0].strip()
    odor_match = re.search(r"Odor:\s*([^\n\r]+)", page_text, re.IGNORECASE)
    if odor_match:
        return odor_match.group(1).strip().split("  ")[0].strip()
    return ""


def parse_tgsc_html(html: str) -> dict[str, Any]:
    """Parse a TGSC raw material HTML string and return extracted properties."""
    soup = BeautifulSoup(html, "html.parser")
    page_text = soup.get_text(" ", strip=True)

    bp_k = _extract_boiling_point(page_text)
    vp_pa = _extract_vapor_pressure(page_text)
    odor_desc = _extract_odor_description(page_text)

    return {
        "boiling_point_k": bp_k,
        "vapor_pressure_pa": vp_pa,
        "odor_description": odor_desc,
    }


def parse_tgsc_page_requests(url: str, timeout: float = 30.0) -> dict[str, Any]:
    """
    Fetch a TGSC raw material URL with plain requests and parse its properties.
    """
    try:
        response = requests.get(
            url,
            headers={"User-Agent": "Mozilla/5.0"},
            timeout=timeout,
        )
        response.raise_for_status()
    except Exception as exc:
        logger.warning("Failed to fetch TGSC page %s: %s", url, exc)
        return {
            "boiling_point_k": None,
            "vapor_pressure_pa": None,
            "odor_description": "",
            "error": str(exc),
        }

    return parse_tgsc_html(response.text)


def parse_tgsc_page_camofox(url: str, browser: Any | None = None) -> dict[str, Any]:
    """
    Fetch a TGSC raw material URL using Camoufox for stealth and parse properties.

    If a browser instance is provided, it will be reused; otherwise a temporary
    Camoufox browser is created and closed.
    """
    if Camoufox is None:
        raise RuntimeError("Camoufox is not installed")

    close_browser = False
    if browser is None:
        browser = Camoufox(headless=True).__enter__()
        close_browser = True

    try:
        context = browser.new_context(no_viewport=True)
        page = context.new_page()
        page.goto(url, wait_until="networkidle")
        html = page.content()
        return parse_tgsc_html(html)
    except Exception as exc:
        logger.warning("Camoufox failed to fetch TGSC page %s: %s", url, exc)
        return {
            "boiling_point_k": None,
            "vapor_pressure_pa": None,
            "odor_description": "",
            "error": str(exc),
        }
    finally:
        if close_browser and browser is not None:
            browser.close()


def parse_tgsc_page(
    url: str,
    *,
    use_camofox: bool = True,
    timeout: float = 30.0,
    fallback_to_requests: bool = True,
) -> dict[str, Any]:
    """
    Fetch a TGSC raw material URL and parse its properties.

    Parameters
    ----------
    url: str
        TGSC data page URL (e.g., https://www.thegoodscentscompany.com/data/rw1007831.html).
    use_camofox: bool
        If True, use Camoufox for stealth; otherwise plain requests.
    timeout: float
        Request timeout for plain requests mode.
    fallback_to_requests: bool
        If True, retry with plain requests when Camoufox fails or returns no
        parseable TGSC data.

    Returns
    -------
    dict with boiling_point_k, vapor_pressure_pa, odor_description, and optionally error.
    """
    if use_camofox:
        try:
            result = parse_tgsc_page_camofox(url)
        except Exception as exc:
            if not fallback_to_requests:
                raise
            logger.warning("Camoufox unavailable for %s; falling back to requests: %s", url, exc)
            result = {
                "boiling_point_k": None,
                "vapor_pressure_pa": None,
                "odor_description": "",
                "error": str(exc),
            }

        has_data = any(
            result.get(key)
            for key in ("boiling_point_k", "vapor_pressure_pa", "odor_description")
        )
        if has_data or not fallback_to_requests:
            return result

        fallback = parse_tgsc_page_requests(url, timeout=timeout)
        if result.get("error") and "error" not in fallback:
            fallback["camofox_error"] = result["error"]
        return fallback
    return parse_tgsc_page_requests(url, timeout=timeout)


def mmhg_to_pa(mmhg: float) -> float:
    """Public helper for the strict mmHg -> Pa conversion."""
    return mmhg * MMHG_TO_PA


def search_tgsc(
    query: str,
    browser: Any | None = None,
) -> list[dict[str, str]]:
    """
    Search TGSC for materials by name and return candidate data page URLs.

    Returns a list of {"name": str, "url": str} records.
    """
    if Camoufox is None:
        raise RuntimeError("Camoufox is not installed")

    close_browser = False
    if browser is None:
        browser = Camoufox(headless=True).__enter__()
        close_browser = True

    results: list[dict[str, str]] = []
    try:
        context = browser.new_context(no_viewport=True)
        page = context.new_page()
        page.goto("https://www.thegoodscentscompany.com/search.php", wait_until="networkidle")
        page.fill('input[name="qName"]', query)
        page.click('input[type="Image"]')
        page.wait_for_load_state("networkidle")
        html = page.content()
        soup = BeautifulSoup(html, "html.parser")
        seen: set[str] = set()
        for tr in soup.find_all("tr"):
            a = tr.find("a", onclick=True)
            if a and "openMainWindow" in a.get("onclick", ""):
                m = re.search(r"openMainWindow\('([^']+)'\)", a["onclick"])
                if not m:
                    continue
                path = m.group(1)
                if path.startswith("/"):
                    url = f"https://www.thegoodscentscompany.com{path}"
                else:
                    url = f"https://www.thegoodscentscompany.com/{path}"
                if url in seen:
                    continue
                seen.add(url)
                name = a.get_text(strip=True)
                prefix = ""
                prefix_td = tr.find("td", class_="lstw9")
                if prefix_td:
                    prefix = prefix_td.get_text(strip=True)
                results.append({"name": f"{prefix} {name}".strip(), "url": url})
    finally:
        if close_browser and browser is not None:
            browser.close()

    return results


if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print("Usage: python -m pino.scraper <tgsc-url>")
        sys.exit(1)

    result = parse_tgsc_page(sys.argv[1])
    print(result)