Files
sisvietnamvn_01/sisvietnamvn_main/extract_icons_img.py
T

120 lines
4.6 KiB
Python

import sys
from bs4 import BeautifulSoup
import re
import unicodedata
def slugify(value):
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('utf-8')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
# The slugs used in our DB
db_slugs = [
"chuyen-khoa-tai-mui-hong",
"khoa-chan-doan-hinh-anh",
"khoa-cap-cuu",
"khoa-dinh-duong-tiet-che",
"khoa-duoc",
"khoa-kham-benh",
"don-vi-kiem-soat-nhiem-khuan",
"khoa-ngoai-tong-hop",
"tim-mach", # This is Khoa Nội Tổng Hợp in our DB
"phau-thuat-gay-me-hoi-suc",
"khoa-than-kinh-dot-quy",
"khoa-vat-ly-tri-lieu-phuc-hoi-chuc-nang",
"khoa-xet-nghiem",
"phong-quan-ly-van-hanh",
"don-vi-can-thiep-mach-dsa",
"don-vi-cap-cuu-ngoai-vien",
"don-vi-kham-suc-khoe-ngoai-vien"
]
manual_mapping = {
"noi tong hop": "tim-mach",
"phau thuat gay me hoi suc phong mo": "phau-thuat-gay-me-hoi-suc",
"dsa": "don-vi-can-thiep-mach-dsa",
"tai mui hong": "chuyen-khoa-tai-mui-hong",
"kiem soat nhiem khuan": "don-vi-kiem-soat-nhiem-khuan",
"kham suc khoe ngoai vien": "don-vi-kham-suc-khoe-ngoai-vien",
"cap cuu ngoai vien": "don-vi-cap-cuu-ngoai-vien"
}
html_path = '/home/x79/sisvietnamvn_01/BV_DHYD_HCM/Các chuyên khoa tại Bệnh viện Đại học Y Dược TP. Hồ Chí Minh.html'
with open(html_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
icons = {}
links = soup.find_all('a', href=True)
for a in links:
href = a.get('href')
if '/chuyen-khoa/' in href:
img = a.find('img')
if img:
src = img.get('src')
if not src:
# Next image srcset or similar fallback
src = img.get('data-src') or img.get('srcset')
if src and ' ' in src:
src = src.split(' ')[0]
title = href.split('/')[-1] # use the slug from href
# Find the actual name
name_span = a.find('span')
if name_span:
title = name_span.text.strip()
else:
title_text = a.text.strip()
if title_text:
title = title_text
if src:
# Decode src if it's encoded or make it absolute
if src.startswith('/_next/image?url='):
import urllib.parse
src = urllib.parse.unquote(src.replace('/_next/image?url=', '').split('&')[0])
# Match it
s_text = slugify(title)
matched_slug = None
for key, val in manual_mapping.items():
if slugify(key) in s_text:
matched_slug = val
break
if not matched_slug:
for s in db_slugs:
if s_text in s or s in s_text or s_text.replace("khoa-", "") in s:
matched_slug = s
break
if matched_slug:
icons[matched_slug] = src
print(f"Mapped {len(icons)} image icons to specialties")
# Generate the thymeleaf switch block for images
switch_code = '<th:block th:switch="${spec.slug}">\n'
for slug, src in icons.items():
switch_code += f' <!-- {slug} -->\n'
switch_code += f' <th:block th:case="\'{slug}\'">\n'
switch_code += f' <img src="{src}" alt="Icon" class="w-12 h-12 object-contain" />\n'
switch_code += f' </th:block>\n'
# Default case
switch_code += ' <!-- Default -->\n'
switch_code += ' <th:block th:case="*">\n'
switch_code += ' <div class="w-12 h-12 rounded-full bg-primary-100 flex items-center justify-center text-primary-600">\n'
switch_code += ' <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="w-6 h-6">\n'
switch_code += ' <path d="M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 012 2v14a2 2 0 01-2 2H5a2 2 0 01-2-2V6a2 2 0 012-2zM9 14h6M12 11v6"/>\n'
switch_code += ' </svg>\n'
switch_code += ' </div>\n'
switch_code += ' </th:block>\n'
switch_code += ' </th:block>'
with open('/tmp/icons_block_img.txt', 'w', encoding='utf-8') as f:
f.write(switch_code)
print("Wrote switch block to /tmp/icons_block_img.txt")