add ama posts in roundup

This commit is contained in:
Bhupesh-V 2024-01-20 18:51:40 +05:30
parent 1c85fa5adc
commit ee24bdcbf6

View File

@ -1,6 +1,7 @@
import datetime import datetime
import praw import praw
import os import os
import sys
import json import json
import requests import requests
@ -43,6 +44,15 @@ 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):
flair = next(
filter(
lambda flair: "AMA" in flair["flair_text"],
subreddit.flair.link_templates.user_selectable(),
)
)
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(
@ -73,7 +83,7 @@ def get_gist_content(gist_id):
return gist["files"][filename]["content"] return gist["files"][filename]["content"]
def get_monthly_roundup(): def get_community_threads():
saved_collection_posts = json.loads(get_gist_content(gist_id)) saved_collection_posts = json.loads(get_gist_content(gist_id))
# filter posts for this month & year # filter posts for this month & year
saved_collection_posts = list( saved_collection_posts = list(
@ -90,7 +100,7 @@ def get_monthly_roundup():
return saved_collection_posts return saved_collection_posts
def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts): def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts, ama_posts):
flair = next( flair = next(
filter( filter(
lambda flair: "Community Roundup" in flair["flair_text"], lambda flair: "Community Roundup" in flair["flair_text"],
@ -102,27 +112,43 @@ def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_di
month=datetime.date.today().strftime("%B"), year=datetime.date.today().year month=datetime.date.today().strftime("%B"), year=datetime.date.today().year
) )
text = "\n## Community Threads\n|S.No|Discussions started by members|"
text += "\n|--------|--------|\n"
footer_text = """\n\n footer_text = """\n\n
--- ---
**Community Roundup is posted on the last day of each month. To explore a compilation of all interesting posts and community threads over time, [visit our wiki](https://www.reddit.com/r/developersIndia/wiki/community-threads/).**\n **Community Roundup is posted on the last day of each month. To explore a compilation of all interesting posts and community threads over time, [visit our wiki](https://www.reddit.com/r/developersIndia/wiki/community-threads/).**\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 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
""" """
posts_counter = 0
for post in posts:
posts_counter += 1
text += f"| {posts_counter} | [**{post['title']}**]({post['url']}) |\n"
text += "\n## Weekly Discussions\n|Started by Volunteer/Mod Team|\n|--------|\n" if len(ama_posts) > 0:
for post in weekly_discussion_posts: text = "\n## AMAs\n||\n|--------|\n"
text += f"| [**{post.title}**]({post.url}) |\n" for post in ama_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
else:
print("No AMAs found. Skipping")
if len(posts) > 0:
text += "\n## Community Threads\n|S.No|Discussions started by members|\n|--------|--------|\n"
posts_counter = 0
for post in posts:
posts_counter += 1
text += f"| {posts_counter} | [**{post['title']}**]({post['url']}) |\n"
else:
print("No posts found in the collection for this month. Skipping")
if len(weekly_discussion_posts) > 0:
text += "\n## Weekly Discussions\n|Started by Volunteer/Mod Team|\n|--------|\n"
for post in weekly_discussion_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
else:
print("No weekly discussions found. Skipping")
text += "## I Made This\n|Top 10 posts|\n|--------|\n" if len(i_made_this_posts) > 0:
for post in i_made_this_posts: text += "\n## I Made This\n|Top 10 posts|\n|--------|\n"
text += f"| [**{post.title}**]({post.url}) |\n" for post in i_made_this_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
else:
print("No I Made This posts found. Skipping")
text = text + footer_text text = text + footer_text
@ -152,13 +178,11 @@ def main():
subreddit = reddit.subreddit(sub) subreddit = reddit.subreddit(sub)
if is_last_day_of_month(): if is_last_day_of_month():
posts = get_monthly_roundup() posts = get_community_threads()
if len(posts) == 0:
print("No posts found in the collection for this month. Skipping")
return
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)
create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts) ama_posts = get_ama_posts(subreddit)
create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts, ama_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")