add support for adding weekly discussions and i made this posts

This commit is contained in:
Bhupesh-V 2024-01-19 13:13:26 +05:30
parent e1e6dbd929
commit 5d7700d61b

View File

@ -19,6 +19,47 @@ 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):
current_year = datetime.date.today().year
current_month = datetime.date.today().month
posts = []
for post in subreddit.search(f'flair_name:"{flair}"', time_filter='month'):
post_date = datetime.datetime.fromtimestamp(post.created_utc)
if post_date.year == current_year and post_date.month == current_month:
posts.append(post)
posts = sorted(posts, key=lambda post: post.created_utc, reverse=True)
return posts
def get_weekly_discussion_posts(subreddit):
flair = next(
filter(
lambda flair: "Weekly Discussion" 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):
flair = next(
filter(
lambda flair: "I Made This" in flair["flair_text"],
subreddit.flair.link_templates.user_selectable(),
)
)
# Get all posts with the specified flair
posts = get_posts_by_flair(subreddit, flair["flair_text"])
# Sort the posts by upvotes and then comments in descending order
posts = sorted(posts, key=lambda post: (post.score, post.num_comments), reverse=True)
# Return only the top 10 posts
return posts[:10]
def get_gist_content(gist_id): def get_gist_content(gist_id):
headers = { headers = {
@ -48,7 +89,7 @@ def get_monthly_roundup():
return saved_collection_posts return saved_collection_posts
def create_community_roundup_post(subreddit, posts): def create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_posts):
flair = next( flair = next(
filter( filter(
lambda flair: "Community Roundup" in flair["flair_text"], lambda flair: "Community Roundup" in flair["flair_text"],
@ -60,19 +101,29 @@ def create_community_roundup_post(subreddit, posts):
month=datetime.date.today().strftime("%B"), year=datetime.date.today().year month=datetime.date.today().strftime("%B"), year=datetime.date.today().year
) )
text = "|||" text = "\n## Community Threads\n|S.No|Discussions started by members|"
text += "\n|--------|--------|\n" 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. 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 posts_counter = 0
for post in posts: for post in posts:
posts_counter += 1 posts_counter += 1
text += f"| {posts_counter} | [**{post['title']}**]({post['url']}) |\n" text += f"| {posts_counter} | [**{post['title']}**]({post['url']}) |\n"
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"
text += "## I Made This\n|Top 10 posts|\n|--------|\n"
for post in i_made_this_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
text = text + footer_text text = text + footer_text
submission = subreddit.submit( submission = subreddit.submit(
@ -104,7 +155,9 @@ def main():
if len(posts) == 0: if len(posts) == 0:
print("No posts found in the collection for this month. Skipping") print("No posts found in the collection for this month. Skipping")
return return
create_community_roundup_post(subreddit, posts) i_made_this_posts = get_i_made_this_posts(subreddit)
weekly_discussion_posts = get_weekly_discussion_posts(subreddit)
create_community_roundup_post(subreddit, posts, i_made_this_posts, weekly_discussion_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")