Last active
October 20, 2020 10:22
-
-
Save dylanroy/a7f3b67cf21ed63dc1f0a64c250c7b02 to your computer and use it in GitHub Desktop.
Auto-Updating Your Github Readme With Python
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
## Auto-Updating Your Github Readme With Python on Medium by Dylan Roy @ https://medium.com/@dylanroy |
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: 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 |
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: cron | |
on: | |
push: | |
branches: | |
- master | |
schedule: | |
- cron: "5 5 * * *" |
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
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest |
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
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 |
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: 🍳 Update README | |
run: | | |
cd ${GITHUB_WORKSPACE}/src/ | |
python main.py |
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: 🚀 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 |
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
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()) |
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
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}" |
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
def update_footer(): | |
timestamp = datetime.datetime.now(pytz.timezone("Europe/Madrid")).strftime("%c") | |
footer = Path('../FOOTER.md').read_text() | |
return footer.format(timestamp=timestamp) |
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
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