add support for listing announcements in roundup post

This commit is contained in:
Bhupesh-V 2024-02-15 13:28:17 +05:30
parent bd986018d2
commit e41335edc3

View File

@ -20,11 +20,12 @@ def is_last_day_of_month():
tomorrow = today + datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1)
return tomorrow.day == 1 return tomorrow.day == 1
def get_posts_by_flair(subreddit, flair): def get_posts_by_flair(subreddit, flair):
current_year = datetime.date.today().year current_year = datetime.date.today().year
current_month = datetime.date.today().month current_month = datetime.date.today().month
posts = [] posts = []
for post in subreddit.search(f'flair_name:"{flair}"', time_filter='month'): for post in subreddit.search(f'flair_name:"{flair}"', time_filter="month"):
post_date = datetime.datetime.fromtimestamp(post.created_utc) post_date = datetime.datetime.fromtimestamp(post.created_utc)
if post_date.year == current_year and post_date.month == current_month: if post_date.year == current_year and post_date.month == current_month:
post.title = post.title.replace("|", "\\|") # Escape the "|" character post.title = post.title.replace("|", "\\|") # Escape the "|" character
@ -44,6 +45,7 @@ def get_weekly_discussion_posts(subreddit):
return get_posts_by_flair(subreddit, flair["flair_text"]) return get_posts_by_flair(subreddit, flair["flair_text"])
def get_ama_posts(subreddit): def get_ama_posts(subreddit):
flair = next( flair = next(
filter( filter(
@ -54,6 +56,7 @@ def get_ama_posts(subreddit):
return get_posts_by_flair(subreddit, flair["flair_text"]) return get_posts_by_flair(subreddit, flair["flair_text"])
def get_i_made_this_posts(subreddit): def get_i_made_this_posts(subreddit):
flair = next( flair = next(
filter( filter(
@ -66,12 +69,25 @@ def get_i_made_this_posts(subreddit):
posts = get_posts_by_flair(subreddit, flair["flair_text"]) posts = get_posts_by_flair(subreddit, flair["flair_text"])
# Sort the posts by upvotes and then comments in descending order # Sort the posts by upvotes and then comments in descending order
posts = sorted(posts, key=lambda post: (post.score, post.num_comments), reverse=True) posts = sorted(
posts, key=lambda post: (post.score, post.num_comments), reverse=True
)
# Return only the top 10 posts # Return only the top 10 posts
return posts[:10] return posts[:10]
def get_announcement_posts(subreddit):
flair = next(
filter(
lambda flair: "Announcement" in flair["flair_text"],
subreddit.flair.link_templates.user_selectable(),
)
)
return get_posts_by_flair(subreddit, flair["flair_text"])
def get_gist_content(gist_id): def get_gist_content(gist_id):
headers = { headers = {
"Authorization": f"token {token}", "Authorization": f"token {token}",
@ -90,17 +106,26 @@ def get_community_threads():
filter( filter(
lambda post: datetime.datetime.strptime( lambda post: datetime.datetime.strptime(
post["created_at"], "%Y-%m-%dT%H:%M:%S" post["created_at"], "%Y-%m-%dT%H:%M:%S"
).year == datetime.date.today().year and ).year
datetime.datetime.strptime( == datetime.date.today().year
and datetime.datetime.strptime(
post["created_at"], "%Y-%m-%dT%H:%M:%S" post["created_at"], "%Y-%m-%dT%H:%M:%S"
).month == datetime.date.today().month, ).month
== datetime.date.today().month,
saved_collection_posts["posts"], saved_collection_posts["posts"],
) )
) )
return saved_collection_posts return saved_collection_posts
def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts, ama_posts): def create_community_roundup_post(
subreddit,
posts,
i_made_this_posts,
weekly_discussion_posts,
ama_posts,
announcement_posts,
):
flair = next( flair = next(
filter( filter(
lambda flair: "Community Roundup" in flair["flair_text"], lambda flair: "Community Roundup" in flair["flair_text"],
@ -119,6 +144,13 @@ def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_di
The collection is curated by our volunteer team & is independent of the number of upvotes and comments (except for "I made This" posts). If you believe we may have overlooked any engaging posts or discussions, please share them with us via [modmail](https://reddit.com/message/compose?to=r/developersIndia&subject=Community%20Threads%20Collection%20Suggestion&message=Hey%20folks%2C%0A%0A%3Cpost%20link%3E).\n The collection is curated by our volunteer team & is independent of the number of upvotes and comments (except for "I made This" posts). If you believe we may have overlooked any engaging posts or discussions, please share them with us via [modmail](https://reddit.com/message/compose?to=r/developersIndia&subject=Community%20Threads%20Collection%20Suggestion&message=Hey%20folks%2C%0A%0A%3Cpost%20link%3E).\n
""" """
if len(announcement_posts) > 0:
text = "## Announcements\n||\n|--------|\n"
for post in announcement_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
else:
print("No announcements found. Skipping")
if len(ama_posts) > 0: if len(ama_posts) > 0:
text = "\n## AMAs\n||\n|--------|\n" text = "\n## AMAs\n||\n|--------|\n"
for post in ama_posts: for post in ama_posts:
@ -142,7 +174,6 @@ The collection is curated by our volunteer team & is independent of the number o
else: else:
print("No weekly discussions found. Skipping") print("No weekly discussions found. Skipping")
if len(i_made_this_posts) > 0: if len(i_made_this_posts) > 0:
text += "\n## I Made This\n|Top 10 posts|\n|--------|\n" text += "\n## I Made This\n|Top 10 posts|\n|--------|\n"
for post in i_made_this_posts: for post in i_made_this_posts:
@ -150,7 +181,6 @@ The collection is curated by our volunteer team & is independent of the number o
else: else:
print("No I Made This posts found. Skipping") print("No I Made This posts found. Skipping")
text = text + footer_text text = text + footer_text
submission = subreddit.submit( submission = subreddit.submit(
@ -182,7 +212,10 @@ def main():
i_made_this_posts = get_i_made_this_posts(subreddit) i_made_this_posts = get_i_made_this_posts(subreddit)
weekly_discussion_posts = get_weekly_discussion_posts(subreddit) weekly_discussion_posts = get_weekly_discussion_posts(subreddit)
ama_posts = get_ama_posts(subreddit) ama_posts = get_ama_posts(subreddit)
create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts, ama_posts) announcement_posts = get_announcement_posts(subreddit)
create_community_roundup_post(
subreddit, posts, i_made_this_posts, weekly_discussion_posts, ama_posts, announcement_posts
)
print("Community Roundup post created successfully!") print("Community Roundup post created successfully!")
else: else:
print("Skipping. Not the last day of the month") print("Skipping. Not the last day of the month")