Files
sisvietnamvn_01/sisvietnamvn_main/extract_icons.py
T

145 lines
5.5 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 mapping bvdaihoc.com.vn titles to our slugs (if slugify is not perfect)
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')
# Find SVGs by looking for "Chuyên khoa" or "Khoa" links
icons = {}
# We'll just look for SVGs in specific items or a tags with title text
a_tags = soup.find_all('a')
for a in a_tags:
svg = a.find('svg')
if svg:
text = a.text.strip()
if text:
# Try to match it to our slugs
s_text = slugify(text)
# Simple substring matching
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:
# Clean up the SVG
svg['class'] = "w-8 h-8 text-primary-600 transition-colors duration-300"
# Remove some inline styles if any
for attr in ['style', 'width', 'height']:
if attr in svg.attrs:
del svg.attrs[attr]
icons[matched_slug] = str(svg)
# If we didn't find many icons, let's look for div with specific classes
if len(icons) < 5:
print("Found few icons, looking for specific classes...")
items = soup.select('.elementor-widget-icon-box .elementor-icon-box-icon svg, .elementor-icon svg')
for svg in items:
# traverse up to find title
parent = svg.parent
title = ""
while parent and parent.name not in ['body']:
header = parent.find(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
if header and header.text.strip():
title = header.text.strip()
break
parent = parent.parent
if title:
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:
matched_slug = s
break
if matched_slug:
# Clean up the SVG
svg['class'] = "w-8 h-8 text-primary-600 transition-colors duration-300"
for attr in ['style', 'width', 'height']:
if attr in svg.attrs:
del svg.attrs[attr]
icons[matched_slug] = str(svg)
print(f"Mapped {len(icons)} icons to specialties")
# Generate the thymeleaf switch block
switch_code = '<th:block th:switch="${spec.slug}">\n'
for slug, svg_html in icons.items():
switch_code += f' <!-- {slug} -->\n'
switch_code += f' <th:block th:case="\'{slug}\'">\n'
switch_code += f' {svg_html}\n'
switch_code += f' </th:block>\n'
# Default case
switch_code += ' <!-- Default -->\n'
switch_code += ' <th:block th:case="*">\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-8 h-8 text-primary-600 transition-colors duration-300">\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 += ' </th:block>\n'
switch_code += ' </th:block>'
# Write the new block to a file so we can see it
with open('/tmp/icons_block.txt', 'w', encoding='utf-8') as f:
f.write(switch_code)
print("Wrote switch block to /tmp/icons_block.txt")