Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automate blogpost imports with rss (#13472)
* chore: add python script to fetch blog content and create post * chore: implement scheduled action to run script and create pr * Apply suggestions from code review * Update action.yml --------- Co-authored-by: Julien Genestoux <julien.genestoux@gmail.com>
- Loading branch information
1 parent
36a2ce5
commit e04c4fb
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Fetch and Create Blog Posts | ||
description: Fetches the latest blog posts from the Unlock Protocol blog and creates new posts in the website repository. | ||
on: | ||
schedule: | ||
- cron: '0 0 * * *' # Run once per day at midnight | ||
pull_request: | ||
|
||
jobs: | ||
fetch_and_create_posts: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install feedparser requests | ||
- name: Fetch RSS feed and create posts | ||
run: python rss_feed.py | ||
|
||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@v4 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
commit-message: "Add new blog posts" | ||
title: "New blog posts from RSS feed" | ||
body: "Automatically generated PR to add new blog posts fetched from the RSS feed." | ||
branch: new-blog-posts |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import re | ||
import requests | ||
import feedparser | ||
from datetime import datetime | ||
|
||
# Fetch the RSS feed | ||
rss_url = 'https://paragraph.xyz/api/blogs/rss/@unlockprotocol' | ||
feed = feedparser.parse(rss_url) | ||
|
||
# Create the base directory for storing blog posts | ||
blog_dir = '../../../unlock-protocol-com/blog' | ||
os.makedirs(blog_dir, exist_ok=True) | ||
|
||
# Iterate over each post in the feed | ||
for entry in feed.entries: | ||
# Extract post details | ||
title = entry.title | ||
subtitle = entry.subtitle if 'subtitle' in entry else '' | ||
author_name = entry.author | ||
publish_date = entry.published | ||
description = entry.summary | ||
image_url = entry.image.href if 'image' in entry else '' | ||
|
||
# Generate a slug for the blog post | ||
slug = re.sub(r'[^\w\-_\. ]', '_', title).lower().replace(' ', '_') | ||
|
||
# Create a directory for the blog post images | ||
post_images_dir = f'../../../unlock-protocol-com/public/images/blog/{slug}' | ||
os.makedirs(post_images_dir, exist_ok=True) | ||
|
||
# Fetch and save the image locally | ||
local_image_path = '' | ||
if image_url: | ||
image_filename = os.path.basename(image_url) | ||
local_image_path = os.path.join(post_images_dir, image_filename) | ||
response = requests.get(image_url) | ||
with open(local_image_path, 'wb') as f: | ||
f.write(response.content) | ||
|
||
# Create the post content | ||
post_content = f'''--- | ||
title: "{title}" | ||
subtitle: "{subtitle}" | ||
authorName: "{author_name}" | ||
publishDate: "{publish_date}" | ||
description: "{description}" | ||
image: "../../../unlock-protocol-com/public/images/blog/{slug}/{image_filename}" | ||
--- | ||
{entry.content[0].value} | ||
''' | ||
|
||
# Generate the filename for the blog post | ||
post_filename = f'{slug}.md' | ||
post_file_path = os.path.join(blog_dir, post_filename) | ||
|
||
# Save the post to a file | ||
with open(post_file_path, 'w') as f: | ||
f.write(post_content) |