diff --git a/scripts/law_monitor.py b/scripts/law_monitor.py index 198c97a..435913f 100644 --- a/scripts/law_monitor.py +++ b/scripts/law_monitor.py @@ -210,33 +210,32 @@ def _should_run(last_check: dict, key: str, interval_days: int) -> bool: def _import_foreign_to_devonthink(filepath: Path, title: str, country: str): - """외국 법령 DEVONthink 임포트""" - country_map = {"US": "US", "JP": "JP", "EU": "EU"} - folder = country_map.get(country, country) - escaped_path = str(filepath).replace('\\', '\\\\').replace('"', '\\"') - escaped_title = title.replace('\\', '').replace('"', '').replace("'", "")[:60] - script = f''' - tell application id "DNtp" - set targetDB to missing value - repeat with db in databases - if name of db is "04_Industrial safety" then - set targetDB to db - exit repeat - end if - end repeat - if targetDB is not missing value then - set targetGroup to create location "/10_Legislation/Foreign/{folder}" in targetDB - set theRecord to import POSIX path "{escaped_path}" to targetGroup - set tags of theRecord to {{"#주제/산업안전/법령", "$유형/법령", "{country}"}} - add custom meta data "law_monitor" for "sourceChannel" to theRecord - add custom meta data "external" for "dataOrigin" to theRecord - add custom meta data (current date) for "lastAIProcess" to theRecord - end if - end tell - ''' + """외국 법령 DEVONthink 임포트 — 파일 경로만 사용 (특수문자 안전)""" + folder = {"US": "US", "JP": "JP", "EU": "EU"}.get(country, country) + fp = str(filepath) + script = ( + 'tell application id "DNtp"\n' + ' set targetDB to missing value\n' + ' repeat with db in databases\n' + ' if name of db is "04_Industrial safety" then\n' + ' set targetDB to db\n' + ' exit repeat\n' + ' end if\n' + ' end repeat\n' + ' if targetDB is not missing value then\n' + f' set targetGroup to create location "/10_Legislation/Foreign/{folder}" in targetDB\n' + f' set theRecord to import POSIX path "{fp}" to targetGroup\n' + ' set tags of theRecord to {"#주제/산업안전/법령", "$유형/법령", "' + country + '"}\n' + ' add custom meta data "law_monitor" for "sourceChannel" to theRecord\n' + ' add custom meta data "external" for "dataOrigin" to theRecord\n' + ' add custom meta data (current date) for "lastAIProcess" to theRecord\n' + ' end if\n' + 'end tell' + ) try: run_applescript_inline(script) - logger.info(f"DEVONthink 임포트 [{country}]: {escaped_title}") + safe_title = title[:40].replace('\n', ' ') + logger.info(f"DEVONthink 임포트 [{country}]: {safe_title}") except Exception as e: logger.error(f"DEVONthink 임포트 실패 [{country}]: {e}") @@ -386,9 +385,9 @@ def fetch_eu_osha(last_check: dict) -> int: if description: content += f"## Summary\n\n{description}\n" - safe_title = "".join(c if c.isalnum() or c in " _-" else "_" for c in title)[:50] + safe_title = "".join(c if c.isalnum() or c in " _-" else "" for c in title)[:50].strip() or f"item{count+1}" today = datetime.now().strftime("%Y%m%d") - filepath = LAWS_DIR / f"EU_{today}_{safe_title}.md" + filepath = LAWS_DIR / f"EU_{today}_{count+1:02d}_{safe_title}.md" with open(filepath, "w", encoding="utf-8") as f: f.write(content)