116 lines
4.9 KiB
Python
116 lines
4.9 KiB
Python
import urllib.parse
|
|
import re
|
|
import unicodedata
|
|
|
|
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:
|
|
text = f.read()
|
|
|
|
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)
|
|
|
|
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",
|
|
"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",
|
|
"chan doan hinh anh": "khoa-chan-doan-hinh-anh",
|
|
"vat ly tri lieu phuc hoi chuc nang": "khoa-vat-ly-tri-lieu-phuc-hoi-chuc-nang",
|
|
"phuc hoi chuc nang": "khoa-vat-ly-tri-lieu-phuc-hoi-chuc-nang",
|
|
"kham benh": "khoa-kham-benh",
|
|
"cap cuu": "khoa-cap-cuu",
|
|
"than kinh": "khoa-than-kinh-dot-quy",
|
|
"ngoai tong hop": "khoa-ngoai-tong-hop",
|
|
"xet nghiem": "khoa-xet-nghiem",
|
|
"dinh duong tiet che": "khoa-dinh-duong-tiet-che"
|
|
}
|
|
|
|
chunks = text.split('href="https://bvdaihoc.com.vn/chuyen-khoa/')
|
|
|
|
icons = {}
|
|
for chunk in chunks[1:]:
|
|
slug_in_href = chunk.split('"')[0]
|
|
if '?' in slug_in_href: continue
|
|
|
|
# Try to find absolute img src in srcset
|
|
src = None
|
|
srcset_m = re.search(r'srcset=\"([^\"]+)\"', chunk)
|
|
if srcset_m:
|
|
# get the first url from srcset
|
|
srcsets = srcset_m.group(1).split(', ')
|
|
first = srcsets[0].split(' ')[0]
|
|
if first.startswith('/_next/image?url='):
|
|
src = urllib.parse.unquote(first.replace('/_next/image?url=', '').split('&')[0])
|
|
|
|
if not src:
|
|
# look for absolute console.bvdaihoc url anywhere in chunk
|
|
abs_m = re.search(r'https%3A%2F%2Fconsole.bvdaihoc.com.vn%2Fuploads%2Fchuyen-khoa%2F[^&\s\"\'\\]+', chunk)
|
|
if abs_m:
|
|
src = urllib.parse.unquote(abs_m.group(0))
|
|
|
|
if src:
|
|
title = slug_in_href
|
|
text_match = re.findall(r'>([^<]+)<', chunk[:1000])
|
|
texts = [t.strip() for t in text_match if len(t.strip()) > 3 and not t.strip().startswith('&')]
|
|
if texts:
|
|
title = texts[0]
|
|
|
|
s_text = slugify(title)
|
|
matched = None
|
|
for key, val in manual_mapping.items():
|
|
if slugify(key) in s_text:
|
|
matched = val
|
|
break
|
|
|
|
if not matched:
|
|
for s in db_slugs:
|
|
if s_text in s or s in s_text or s_text.replace("khoa-", "") in s:
|
|
matched = s
|
|
break
|
|
|
|
if matched:
|
|
icons[matched] = src
|
|
else:
|
|
s_slug = slugify(slug_in_href)
|
|
for s in db_slugs:
|
|
if s_slug in s or s in s_slug:
|
|
icons[s] = src
|
|
break
|
|
|
|
switch_code = ' <th:block th:switch="${spec.slug}">\n'
|
|
for slug, src in sorted(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 mx-auto" />\n'
|
|
switch_code += f' </th:block>\n'
|
|
|
|
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 mx-auto">\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 block to /tmp/icons_block_img.txt")
|