-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate_readme.py
More file actions
94 lines (76 loc) · 3.43 KB
/
update_readme.py
File metadata and controls
94 lines (76 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import requests # Import the requests library to make HTTP requests
import feedparser # Import the feedparser library to parse RSS feeds
from string import Template # Import the Template class from the string module for template substitution
import sys # Import sys to exit the script if needed
import re # Import the regex module for extracting the countdown value
# URL of the freeCodeCamp RSS feed
rss_url = "https://www.freecodecamp.org/news/rss/"
# List of tags to filter by
target_tags = [
"beginners guide", "beginners", "learning", "python", "learn coding", "github",
"javascript", "html", "css", "self-improvement", "git"
]
# First read the current README to extract the countdown value
try:
with open("README.md", "r") as current_readme:
current_content = current_readme.read()
# Find the current countdown value using regex
countdown_match = re.search(r'Days remaining: <span id="countdown" [^>]*>(\d+)</span>', current_content)
current_countdown = countdown_match.group(1) if countdown_match else "##COUNTDOWN##"
except (FileNotFoundError, AttributeError):
current_countdown = "##COUNTDOWN##"
# Fetch the RSS feed
response = requests.get(rss_url)
feed = feedparser.parse(response.content)
# Filter posts by tags
filtered_posts = []
for post in feed.entries:
# Check if the post has any of the target tags
if 'tags' in post:
# Extract the tag content from CDATA format
post_tags = [tag.get('term', '').lower().strip() for tag in post.tags]
# Check if any of our target tags match the post tags
if any(target_tag.lower() in post_tag for target_tag in target_tags for post_tag in post_tags):
filtered_posts.append(post)
# Stop once we have 2 matching posts
if len(filtered_posts) >= 2:
break
# If we don't have exactly 2 filtered posts, exit without updating
if len(filtered_posts) != 2:
print(f"Not enough filtered posts found. Only found {len(filtered_posts)} posts matching the tags.")
print("README not updated.")
sys.exit(0) # Exit the script without error
# Extract the two filtered posts with author information
latest_posts = filtered_posts[:2]
news_posts = []
for post in latest_posts:
author = post.get('author', '')
if not author and hasattr(post, 'creator'):
author = post.creator
news_posts.append(f"<a href='{post.link}'>{post.title}</a> by {author}")
# Debug prints to verify extracted data
for i, post in enumerate(news_posts, 1):
print(f"news_post_{i}: {post}")
# Also print tags to help debug
if i <= len(filtered_posts):
if 'tags' in filtered_posts[i-1]:
tags = [tag.get('term', '') for tag in filtered_posts[i-1].tags]
print(f" Tags: {', '.join(tags)}")
# Read the template file
with open("README.md.tpl", "r") as tpl_file:
tpl_content = tpl_file.read()
# Replace placeholders with actual posts
template = Template(tpl_content)
readme_content = template.substitute(
news_post_1=news_posts[0],
news_post_2=news_posts[1]
)
# Replace the ##COUNTDOWN## placeholder with the current countdown value
readme_content = readme_content.replace("##COUNTDOWN##", current_countdown)
# Debug print to verify the final content
print("Updated README content:")
print(readme_content)
# Write the updated content to README.md
with open("README.md", "w") as readme_file:
readme_file.write(readme_content)
print("README successfully updated.")