Sync blog posts to README #48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Sync blog posts to README | |
| on: | |
| schedule: | |
| - cron: '17 6 * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Update README from feed | |
| run: | | |
| python3 - <<'PY' | |
| import re | |
| import urllib.request | |
| import xml.etree.ElementTree as ET | |
| from html import unescape | |
| from pathlib import Path | |
| FEED_URL = "https://max.dp.tools/feed.xml" | |
| README = Path("profile/README.md") | |
| MAX_POSTS = 5 | |
| xml = urllib.request.urlopen(FEED_URL, timeout=10).read() | |
| root = ET.fromstring(xml) | |
| items = root.findall(".//item")[:MAX_POSTS] | |
| def og_image(url): | |
| try: | |
| html = urllib.request.urlopen(url, timeout=10).read().decode("utf-8", "ignore") | |
| m = re.search(r'<meta\s+property="og:image"\s+content="([^"]+)"', html) | |
| return m.group(1) if m else None | |
| except Exception: | |
| return None | |
| cards = [] | |
| for idx, item in enumerate(items): | |
| title = unescape((item.findtext("title") or "").strip()) | |
| link = (item.findtext("link") or "").strip() | |
| summary = unescape((item.findtext("description") or "").strip()) | |
| img = og_image(link) | |
| sep = "<br>\n\n" if idx > 0 else "" | |
| if img: | |
| cards.append( | |
| f"{sep}### [{title}]({link})\n\n" | |
| f'<a href="{link}"><img align="left" width="320" src="{img}" alt="{title}" hspace="20" vspace="6"></a>\n\n' | |
| f"{summary}\n\n" | |
| f'<br clear="left">' | |
| ) | |
| else: | |
| cards.append(f"{sep}### [{title}]({link})\n\n{summary}") | |
| block = "\n\n".join(cards) if cards else "_No posts yet._" | |
| content = README.read_text() | |
| new = re.sub( | |
| r"<!-- BLOG:START -->.*?<!-- BLOG:END -->", | |
| f"<!-- BLOG:START -->\n{block}\n<!-- BLOG:END -->", | |
| content, | |
| flags=re.DOTALL, | |
| ) | |
| if new != content: | |
| README.write_text(new) | |
| print("README updated") | |
| else: | |
| print("No change") | |
| PY | |
| - name: Commit if changed | |
| run: | | |
| if git diff --quiet; then | |
| echo "No changes" | |
| exit 0 | |
| fi | |
| git config user.name "max-bot" | |
| git config user.email "noreply@digitalprocesstools.com" | |
| git add profile/README.md | |
| git commit -m "chore: sync latest blog posts" | |
| git push |