43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Convert all data/parsed/*.json to .md files alongside them."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
PARSED = Path.cwd() / 'data' / 'parsed'
|
|
|
|
|
|
def article_to_markdown(article: dict) -> str:
|
|
parts = []
|
|
title = article.get('title')
|
|
if title:
|
|
parts.append(f"# {title}")
|
|
meta = []
|
|
if article.get('author'):
|
|
meta.append(str(article.get('author')))
|
|
if article.get('published_at'):
|
|
meta.append(str(article.get('published_at')))
|
|
if meta:
|
|
parts.append('_' + ' • '.join(meta) + '_')
|
|
if article.get('description'):
|
|
parts.append(article.get('description'))
|
|
if article.get('image'):
|
|
parts.append(f"})")
|
|
body = article.get('body_text') or ''
|
|
if body:
|
|
parts.append(body)
|
|
return '\n\n'.join(parts)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
PARSED.mkdir(parents=True, exist_ok=True)
|
|
files = sorted(PARSED.glob('*.json'))
|
|
for f in files:
|
|
try:
|
|
a = json.loads(f.read_text(encoding='utf-8'))
|
|
md = article_to_markdown(a)
|
|
out = f.with_suffix('.md')
|
|
out.write_text(md, encoding='utf-8')
|
|
print('Wrote', out)
|
|
except Exception as e:
|
|
print('Failed', f, e)
|