6 Commits

6 changed files with 112 additions and 100 deletions

View File

@ -1,35 +1,37 @@
name : Community Threads Wiki Updater
on:
schedule:
- cron: '0 0 * * *' # This cron expression triggers the workflow every day at midnight UTC
workflow_dispatch:
workflow_dispatch:
inputs:
post_url:
description: 'The URL of the Reddit post to add'
required: true
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Update Wiki
env:
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }}
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
GIST_ID: ${{ secrets.GIST_ID }}
GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
run: |
cd community-threads
python main.py
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Update Wiki
env:
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }}
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
GIST_ID: ${{ secrets.GIST_ID }}
GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
run: |
cd community-threads
python main.py ${{ github.event.inputs.post_url }}

View File

@ -79,8 +79,10 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://animesh-ghosh.github.io/"><img src="https://avatars.githubusercontent.com/u/34956994?v=4?s=100" width="100px;" alt="MaDDogx"/><br /><sub><b>MaDDogx</b></sub></a><br /><a href="https://github.com/developersIndia/deviras/commits?author=Animesh-Ghosh" title="Code">💻</a> <a href="https://github.com/developersIndia/deviras/commits?author=Animesh-Ghosh" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://pratham.cc"><img src="https://avatars.githubusercontent.com/u/67585967?v=4?s=100" width="100px;" alt="Pratham"/><br /><sub><b>Pratham</b></sub></a><br /><a href="https://github.com/developersIndia/deviras/commits?author=git-bruh" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://nisarga.me/"><img src="https://avatars.githubusercontent.com/u/45588772?v=4?s=100" width="100px;" alt="Nisarga Adhikary"/><br /><sub><b>Nisarga Adhikary</b></sub></a><br /><a href="https://github.com/developersIndia/deviras/commits?author=ni5arga" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://lineararray.nekoweb.org"><img src="https://github.com/lineararray.png" width="100px;" alt="LinearArray"/><br /><sub><b>LinearArray</b></sub></a><br /><a href="https://github.com/developersIndia/deviras/commits?author=LinearArray" title="Code">💻</a></td>
</tr>
</tbody>
</table>
<!-- markdownlint-restore -->

View File

@ -51,14 +51,14 @@ def main():
if question_text != "[deleted]":
question_link = "https://reddit.com" + comment.parent().permalink
markdown_file += (
f"{question_number}. [{question_text}]({question_link})\n"
)
f"{question_number}. [{question_text}]({question_link})\n\n"
) # Add an extra newline after each question
question_number += 1
with open("questions.md", "w", encoding="utf-8") as file:
file.write(markdown_file)
print(f"{question_number} questions generated successfully.")
print(f"{question_number - 1} questions generated successfully.")
if __name__ == "__main__":

View File

@ -152,7 +152,7 @@ The collection is curated by our volunteer team & is independent of the number o
print("No announcements found. Skipping")
if len(ama_posts) > 0:
text = "\n## AMAs\n||\n|--------|\n"
text += "\n## AMAs\n||\n|--------|\n"
for post in ama_posts:
text += f"| [**{post.title}**]({post.url}) |\n"
else:

View File

@ -1,5 +1,6 @@
import praw
import os
import argparse
from datetime import datetime
import json
from collections import defaultdict
@ -36,13 +37,26 @@ def update_gist(gist_id, filename, content, description=""):
)
return response.json()
# 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
def get_collection(reddit):
collection = reddit.subreddit(sub).collections(
permalink="https://reddit.com/r/developersIndia/collection/958aef35-f9cb-414d-ab33-08bc639e47de"
)
return collection
def get_post_data(reddit, post_url):
submission = reddit.submission(url=post_url)
post = {
"title": submission.title,
"url": submission.url,
"id": submission.id,
"num_comments": submission.num_comments,
"created_at": datetime.utcfromtimestamp(
submission.created_utc
).isoformat(),
"flair_text": submission.link_flair_text,
}
return post
def update_wiki(reddit, wikipage, posts):
# Group posts by year
@ -61,7 +75,7 @@ def update_wiki(reddit, wikipage, posts):
wiki_header = """# A collection of must read discussions started by community members"""
content = wiki_header + "\n\n"
content += f"A handpicked collection of **{total_posts}** interesting posts, discussions & high-quality threads gathered over **{total_years}** years & counting.\n\n"
content += f"A handpicked collection of **{total_posts}** interesting posts, discussions & high-quality threads gathered over **{total_years-1}** years & counting.\n\n"
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"
for year in sorted(posts_by_year.keys(), reverse=True):
@ -78,6 +92,10 @@ def update_wiki(reddit, wikipage, posts):
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()
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
@ -86,70 +104,30 @@ def main():
user_agent=f"Automod reader by u/{username}",
)
collection = get_collection(reddit)
saved_collection_posts = json.loads(get_gist_content(gist_id))
saved_collection_ids = [post["id"] for post in saved_collection_posts["posts"]]
print(f"Database was last updated on {saved_collection_posts['collection_last_updated']}")
print(f"Collection was last updated on {datetime.utcfromtimestamp(collection.last_update_utc).isoformat()}")
if (
saved_collection_posts["collection_last_updated"]
!= datetime.utcfromtimestamp(collection.last_update_utc).isoformat()
):
print("Collection was updated, getting new posts data...")
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)
# given 2 lists find non-common elements
db_posts = set(saved_collection_ids)
collection_posts = []
for submission in collection:
collection_posts.append(submission.id)
collection_posts = set(collection_posts)
new_posts = list(collection_posts - db_posts)
deleted_posts = list(db_posts - collection_posts)
print(f"Found {len(new_posts)} new posts!")
print(f"Found {len(deleted_posts)} deleted posts!")
posts = []
# load the saved collection posts data
for submission_id in saved_collection_posts["posts"]:
if submission_id["id"] in deleted_posts:
continue
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)
# get the new posts data
for submission_id in new_posts:
submission = reddit.submission(submission_id)
post = {
"title": submission.title,
"url": submission.url,
"id": submission.id,
"num_comments": submission.num_comments,
"created_at": datetime.utcfromtimestamp(
submission.created_utc
).isoformat(),
"flair_text": submission.link_flair_text,
}
posts.append(post)
# sort the posts by created_at
new_post = get_post_data(reddit, args.post_url)
if new_post["id"] not in saved_collection_ids:
posts.append(new_post)
posts = sorted(posts, key=lambda k: k["created_at"])
collection_json = {
"collection_last_updated": datetime.utcfromtimestamp(
collection.last_update_utc
).isoformat(),
"collection_last_updated": datetime.utcnow().isoformat(),
"posts": posts,
}
@ -157,7 +135,7 @@ def main():
print("Internal database updated successfully!")
update_wiki(reddit, "community-threads", posts)
else:
print("Wiki is up to date!")
print("Post is already in the collection. No changes were made.")
if __name__ == "__main__":

View File

@ -1,6 +1,7 @@
import os
import sys
import praw
import time
import datetime
client_id = os.environ["REDDIT_CLIENT_ID"]
@ -19,7 +20,7 @@ def get_last_activity_times(reddit, username):
limit=100
): # look at the user's 100 most recent comments
if comment.subreddit.display_name == sub:
last_comment_time = datetime.datetime.fromtimestamp(comment.created_utc)
last_comment_time = datetime.datetime.fromtimestamp(comment.created_utc).strftime('%d %B, %Y')
break
# Get the user's last post creation time and title in the subreddit
@ -29,7 +30,7 @@ def get_last_activity_times(reddit, username):
limit=100
): # look at the user's 100 most recent posts
if submission.subreddit.display_name == sub:
last_post_time = datetime.datetime.fromtimestamp(submission.created_utc)
last_post_time = datetime.datetime.fromtimestamp(submission.created_utc).strftime('%d %B, %Y')
last_post_title = submission.title
break
@ -45,13 +46,25 @@ def get_current_flair(reddit, username):
return flair["flair_text"], template["id"]
def get_flair(reddit, username):
subreddit = reddit.subreddit(sub)
flair = next(subreddit.flair(username))
template = get_template_from_flair_text(reddit, flair["flair_text"])
if template is None:
return None, None
return flair["flair_text"], template["id"]
def assign_user_flair(reddit, username, flair_text):
subreddit = reddit.subreddit(sub)
flair = next(subreddit.flair(username))
template = get_flair_template_from_text(reddit, flair["flair_text"])
subreddit.flair.set(username, text=flair_text, flair_template_id=template["id"])
# append YoE to the flair text
verified_text = f"{flair['flair_text']} | {flair_text}"
subreddit.flair.set(username, text=verified_text, flair_template_id=template["id"])
def get_flair_templates(reddit):
@ -66,6 +79,14 @@ def get_flair_template_from_text(reddit, flair_text):
return template
def get_template_from_flair_text(reddit, flair_text):
templates = get_flair_templates(reddit)
for template in templates:
# check if the flair text is in the template
if template["text"] in flair_text:
return template
def send_message(reddit, username, flair_text):
message_subject = 'Woohoo! You are now a verified member of r/developersIndia! 🚀'
message_text = """
@ -117,8 +138,14 @@ def main():
# get current flair
current_flair_text, current_flair_template_id = get_current_flair(reddit, reddit_username)
if current_flair_text is None:
current_flair_text, current_flair_template_id = get_flair(reddit, reddit_username)
# TODO figure out final flair text
if "Verified" in current_flair_text or "YoE" in current_flair_text:
print(f"{reddit_username} is already verified")
sys.exit(0)
if current_flair_text is None and current_flair_template_id is None:
print(f"{reddit_username} does not have a flair on r/developersIndia")
sys.exit(0)
else:
@ -131,8 +158,11 @@ def main():
sys.exit(0)
assign_user_flair(reddit, reddit_username, flair_text)
print(f"Updated {reddit_username}'s flair to \"{flair_text}\"")
send_message(reddit, reddit_username, flair_text)
# Ya I know, just don't ask
time.sleep(2)
updated_flair_text, _ = get_flair(reddit, reddit_username)
print(f"Updated {reddit_username}'s flair to \"{updated_flair_text}\"")
send_message(reddit, reddit_username, updated_flair_text)
print(f"Sent verification confirmation message to {reddit_username}")