deviras/community-threads/main.py

169 lines
6.5 KiB
Python
Raw Normal View History

2023-12-16 18:31:34 +05:30
import praw
import os
import argparse
2023-12-16 18:31:34 +05:30
from datetime import datetime
import json
2023-12-27 12:28:27 +05:30
from collections import defaultdict
2023-12-19 00:28:55 +05:30
import requests
2023-12-16 18:31:34 +05:30
client_id = os.environ["REDDIT_CLIENT_ID"]
client_secret = os.environ["REDDIT_CLIENT_SECRET"]
reddit_pass = os.environ["REDDIT_PASSWORD"]
username = os.environ["REDDIT_USERNAME"]
2023-12-19 00:35:07 +05:30
token = os.environ["GIST_TOKEN"]
2023-12-19 00:28:55 +05:30
gist_id = os.environ["GIST_ID"]
sub = "developersIndia"
def get_gist_content(gist_id):
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.get(f"https://api.github.com/gists/{gist_id}", headers=headers)
gist = response.json()
filename = list(gist["files"].keys())[0]
return gist["files"][filename]["content"]
def update_gist(gist_id, filename, content, description=""):
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json",
}
data = {"description": description, "files": {filename: {"content": content}}}
response = requests.patch(
f"https://api.github.com/gists/{gist_id}", headers=headers, json=data
)
return response.json()
2023-12-16 18:31:34 +05:30
# farewell, reddit collections
# def get_collection(reddit):
# collection = reddit.subreddit(sub).collections(
# permalink="https://reddit.com/r/developersIndia/collection/958aef35-f9cb-414d-ab33-08bc639e47de"
# )
# return collection
# let the author know their post is now part of the collection!
def send_message(reddit, username, post_link):
message_subject = 'Woohoo! Your post is now part of our community threads collection!'
message_text = """
Hi there,\n
2024-09-14 18:07:34 +05:30
It looks like one of your [posts]({post_link}) on r/developersIndia was picked-up by the volunteer team to be part of our curated list of 100+ amazing discussions in the community.\n
2024-04-10 14:02:54 +05:30
- You can find your post in our [Community Threads Collection](https://reddit.com/r/developersIndia/wiki/community-threads). Feel free to share the collection with your dev friends.\n
2024-07-12 14:46:17 +05:30
- We post a compilation of these threads every month-end in [Community Roundups](https://www.reddit.com/r/developersIndia/?f=flair_name%3A%22Community%20Roundup%22). Stay tuned for the next one!\n
2024-04-10 14:02:54 +05:30
Cheers,\n
The r/developersIndia Community Team
"""
reddit.redditor(username).message(
subject=message_subject, message=message_text.format(post_link=post_link), from_subreddit=reddit.subreddit(sub)
)
def get_post_data(reddit, post_url):
submission = reddit.submission(url=post_url)
post = {
"title": submission.title,
"url": submission.permalink,
"id": submission.id,
"num_comments": submission.num_comments,
"created_at": datetime.utcfromtimestamp(
submission.created_utc
).isoformat(),
"flair_text": submission.link_flair_text,
2024-08-15 16:17:47 +05:30
"author": submission.author.name if submission.author else "",
}
return post
2023-12-16 18:31:34 +05:30
2023-12-19 00:28:55 +05:30
def update_wiki(reddit, wikipage, posts):
2023-12-27 12:28:27 +05:30
# Group posts by year
posts_by_year = defaultdict(list)
for post in posts:
year = datetime.strptime(post['created_at'], '%Y-%m-%dT%H:%M:%S').year
posts_by_year[year].append(post)
# Sort posts within each year
for year in posts_by_year:
posts_by_year[year] = sorted(posts_by_year[year], key=lambda k: k['created_at'], reverse=True)
2023-12-27 13:02:03 +05:30
# Calculate total posts and years
total_posts = sum(len(posts) for posts in posts_by_year.values())
total_years = len(posts_by_year)
2023-12-27 12:28:27 +05:30
wiki_header = """# A collection of must read discussions started by community members"""
2023-12-19 00:28:55 +05:30
content = wiki_header + "\n\n"
2024-02-25 14:30:58 +05:30
content += f"A handpicked collection of **{total_posts}** interesting posts, discussions & high-quality threads gathered over **{total_years-1}** years & counting.\n\n"
2024-01-06 00:00:12 +05:30
content += "If you spot a post that could be in this list, send us a [modmail](https://reddit.com/message/compose?to=r/developersIndia&subject=Community%20Threads%20Collection%20Suggestion&message=Hey%20folks%2C%0A%0A%3Cpost%20link%3E)\n\n"
2023-12-19 00:28:55 +05:30
2023-12-27 12:28:27 +05:30
for year in sorted(posts_by_year.keys(), reverse=True):
content += f"## {year}\n\n"
# Add the posts for this year
for post in posts_by_year[year]:
2024-01-01 19:26:46 +05:30
formatted_date = datetime.strptime(post['created_at'], '%Y-%m-%dT%H:%M:%S').strftime('%d %b, %Y')
2024-01-03 12:52:54 +05:30
content += f"- `{formatted_date}` [**{post['title']}**]({post['url']})\n\n"
2023-12-19 00:28:55 +05:30
2023-12-27 12:28:27 +05:30
# given a wiki link, update the wiki page with new markdown
wikipage = reddit.subreddit(sub).wiki[wikipage]
2023-12-19 00:28:55 +05:30
wikipage.edit(content=content)
print("Wiki updated successfully!")
2023-12-16 18:31:34 +05:30
def main():
parser = argparse.ArgumentParser(description='Update Community Threads Collection.')
parser.add_argument('post_url', help='The URL of the Reddit post to add.')
args = parser.parse_args()
2023-12-16 18:31:34 +05:30
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
username=username,
password=reddit_pass,
user_agent=f"Automod reader by u/{username}",
)
2023-12-19 00:28:55 +05:30
saved_collection_posts = json.loads(get_gist_content(gist_id))
saved_collection_ids = [post["id"] for post in saved_collection_posts["posts"]]
2023-12-16 18:31:34 +05:30
2023-12-19 00:28:55 +05:30
print(f"Database was last updated on {saved_collection_posts['collection_last_updated']}")
posts = []
for submission_id in saved_collection_posts["posts"]:
post = {
"title": submission_id["title"],
"url": submission_id["url"],
"id": submission_id["id"],
"num_comments": submission_id["num_comments"],
"created_at": submission_id["created_at"],
"flair_text": submission_id["flair_text"],
}
posts.append(post)
new_post = get_post_data(reddit, args.post_url)
if new_post["id"] not in saved_collection_ids:
2024-04-13 18:24:49 +05:30
new_post["title"] = new_post["title"].strip()
posts.append(new_post)
2023-12-19 00:28:55 +05:30
posts = sorted(posts, key=lambda k: k["created_at"])
collection_json = {
"collection_last_updated": datetime.utcnow().isoformat(),
2023-12-19 00:28:55 +05:30
"posts": posts,
}
2023-12-16 18:31:34 +05:30
2023-12-19 00:28:55 +05:30
update_gist(gist_id, "collection.json", json.dumps(collection_json, indent=4))
print("Internal database updated successfully!")
update_wiki(reddit, "community-threads", posts)
2024-08-15 16:21:34 +05:30
if new_post["author"]:
send_message(reddit, new_post["author"], new_post["url"])
print("Message sent to the author!")
else:
print("Author username is empty. No message sent.")
2023-12-19 00:28:55 +05:30
else:
print("Post is already in the collection. No changes were made.")
2023-12-16 18:31:34 +05:30
if __name__ == "__main__":
main()