Skip to content

Instantly share code, notes, and snippets.

@dylanroy
Last active October 20, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanroy/a7f3b67cf21ed63dc1f0a64c250c7b02 to your computer and use it in GitHub Desktop.
Save dylanroy/a7f3b67cf21ed63dc1f0a64c250c7b02 to your computer and use it in GitHub Desktop.
Auto-Updating Your Github Readme With Python
## Auto-Updating Your Github Readme With Python on Medium by Dylan Roy @ https://medium.com/@dylanroy
name: cron
on:
push:
branches:
- master
schedule:
- cron: "5 5 * * *"
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 🍽️ Get working copy
uses: actions/checkout@master
with:
fetch-depth: 1
- name: 🐍 Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: 💿 Install feedparser & pytz
run: pip install feedparser pytz
- name: 🍳 Update README
run: |
cd ${GITHUB_WORKSPACE}/src/
python main.py
- name: 🚀 Deploy
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -am "feat(auto generate): Updated content"
git push --all -f https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git
name: cron
on:
push:
branches:
- master
schedule:
- cron: "5 5 * * *"
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 🍽️ Get working copy
uses: actions/checkout@master
with:
fetch-depth: 1
- name: 🐍 Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: 💿 Install feedparser & pytz
run: pip install feedparser pytz
- name: 🍳 Update README
run: |
cd ${GITHUB_WORKSPACE}/src/
python main.py
- name: 🚀 Deploy
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -am "feat(auto generate): Updated content"
git push --all -f https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git
from pathlib import Path
import datetime
import pytz
import feedparser
def update_footer():
timestamp = datetime.datetime.now(pytz.timezone("Europe/Madrid")).strftime("%c")
footer = Path('../FOOTER.md').read_text()
return footer.format(timestamp=timestamp)
def update_readme_medium_posts(medium_feed, readme_base, join_on):
d = feedparser.parse(medium_feed)
posts = []
for item in d.entries:
if item.get('tags'):
posts.append(f" - [{item['title']}]({item['link']})")
posts_joined = '\n'.join(posts)
return readme_base[:readme_base.find(rss_title)] + f"{join_on}\n{posts_joined}"
rss_title = "### Stories by Dylan Roy on Medium" # Anchor for where to append posts
readme = Path('../README.md').read_text()
updated_readme = update_readme_medium_posts("https://medium.com/feed/@dylanroy", readme, rss_title)
with open('../README.md', "w+") as f:
f.write(updated_readme + update_footer())
def update_readme_medium_posts(medium_feed, readme_base, join_on):
d = feedparser.parse(medium_feed)
title = f"### {d['feed']['title']}"
posts = []
for item in d.entries:
if item.get('tags'):
posts.append(f" - [{item['title']}]({item['link']})")
posts_joined = '\n'.join(posts)
return readme_base[:readme_base.find(rss_title)] + f"{title}\n{posts_joined}"
def update_footer():
timestamp = datetime.datetime.now(pytz.timezone("Europe/Madrid")).strftime("%c")
footer = Path('../FOOTER.md').read_text()
return footer.format(timestamp=timestamp)
rss_title = "### Stories by Dylan Roy on Medium" # Anchor for where to append posts
readme = Path('../README.md').read_text()
updated_readme = update_readme_medium_posts("https://medium.com/feed/@dylanroy", readme, rss_title)
with open('../README.md', "w+") as f:
f.write(updated_readme + update_footer())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment