Process a single IMDRF index entry: download DOCX and convert to Markdown.
(entry: dict)
| 155 | |
| 156 | |
| 157 | def process_entry(entry: dict) -> bool: |
| 158 | """Process a single IMDRF index entry: download DOCX and convert to Markdown.""" |
| 159 | doc_id = entry["id"] |
| 160 | title = entry["title"] |
| 161 | title_en = title.get("en", "") if isinstance(title, dict) else str(title) |
| 162 | source_url = entry.get("source_url", "") |
| 163 | doc_number = entry.get("document_number", "") |
| 164 | |
| 165 | output_path = OUTPUT_DIR / f"{doc_id}.md" |
| 166 | if output_path.exists() and not FORCE_RECONVERT: |
| 167 | size = output_path.stat().st_size |
| 168 | if size > 500: |
| 169 | print(f" [SKIP] {doc_id} -- already exists ({size} bytes)") |
| 170 | return False |
| 171 | |
| 172 | print(f" Processing: {doc_id}") |
| 173 | print(f" Title: {title_en}") |
| 174 | print(f" URL: {source_url}") |
| 175 | |
| 176 | if not source_url: |
| 177 | print(f" [WARN] No source URL") |
| 178 | return False |
| 179 | |
| 180 | docx_url, pdf_url = fetch_docx_url(source_url) |
| 181 | print(f" DOCX: {docx_url or '(none)'}") |
| 182 | print(f" PDF: {pdf_url or '(none)'}") |
| 183 | |
| 184 | if not docx_url: |
| 185 | print(f" [WARN] No DOCX link found, skipping") |
| 186 | return False |
| 187 | |
| 188 | try: |
| 189 | print(f" Downloading DOCX...") |
| 190 | resp = requests.get(docx_url, headers=HEADERS, timeout=60) |
| 191 | resp.raise_for_status() |
| 192 | except Exception as e: |
| 193 | print(f" [ERROR] Download failed: {e}") |
| 194 | return False |
| 195 | |
| 196 | with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as tmp: |
| 197 | tmp.write(resp.content) |
| 198 | tmp_path = tmp.name |
| 199 | |
| 200 | try: |
| 201 | print(f" Converting to Markdown...") |
| 202 | md = docx_to_markdown(tmp_path) |
| 203 | |
| 204 | header = f"# {title_en}\n\n" |
| 205 | if doc_number: |
| 206 | header += f"**Document Number**: {doc_number}\n\n" |
| 207 | header += f"**Source**: [{source_url}]({source_url})\n\n---\n\n" |
| 208 | |
| 209 | full_md = header + md |
| 210 | output_path.write_text(full_md, encoding="utf-8") |
| 211 | print(f" [OK] Written {len(full_md)} chars -> {output_path.name}") |
| 212 | return True |
| 213 | except Exception as e: |
| 214 | print(f" [ERROR] Conversion failed: {e}") |
no test coverage detected