-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (38 loc) · 1.43 KB
/
utils.py
File metadata and controls
48 lines (38 loc) · 1.43 KB
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
import json
from typing import Any, Dict, Optional
from bs4 import BeautifulSoup
def extract_json_ld(soup: BeautifulSoup) -> Dict[str, Any]:
"""Extract structured data from JSON-LD script tags."""
for script in soup.find_all('script', {'type': 'application/ld+json'}):
try:
data = json.loads(script.string)
if isinstance(data, dict):
# Valid types for vehicles in car.gr JSON-LD
if data.get('@type') in ['Car', 'Vehicle', 'Product'] or 'offers' in data:
return data
except (json.JSONDecodeError, TypeError):
continue
return {}
def extract_meta_description(soup: BeautifulSoup) -> str:
"""Extract description from meta tags."""
meta = (
soup.find('meta', {'property': 'og:description'}) or
soup.find('meta', {'name': 'description'})
)
return meta['content'] if meta else ""
def safe_int(val: Any) -> Optional[int]:
"""Safely convert value to integer."""
try:
if isinstance(val, str):
val = val.replace('.', '').replace(',', '')
return int(val)
except (ValueError, TypeError):
return None
def safe_float(val: Any) -> Optional[float]:
"""Safely convert value to float."""
try:
if isinstance(val, str):
val = val.replace('.', '').replace(',', '.')
return float(val)
except (ValueError, TypeError):
return None