mirror of
https://github.com/developersIndia/deviras.git
synced 2025-05-31 14:11:43 +05:30
Compare commits
6 Commits
remove-col
...
dc7a04b9b1
Author | SHA1 | Date | |
---|---|---|---|
dc7a04b9b1 | |||
3b32cb1909 | |||
dc31a18089 | |||
dbe8db8816 | |||
7419fee0bd | |||
4325ecc230 |
54
.github/workflows/collection-thread-updater.yml
vendored
54
.github/workflows/collection-thread-updater.yml
vendored
@ -1,35 +1,37 @@
|
|||||||
name : Community Threads Wiki Updater
|
name : Community Threads Wiki Updater
|
||||||
|
|
||||||
on:
|
on:
|
||||||
schedule:
|
workflow_dispatch:
|
||||||
- cron: '0 0 * * *' # This cron expression triggers the workflow every day at midnight UTC
|
inputs:
|
||||||
workflow_dispatch:
|
post_url:
|
||||||
|
description: 'The URL of the Reddit post to add'
|
||||||
|
required: true
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- name: Set up Python 3.10
|
- name: Set up Python 3.10
|
||||||
uses: actions/setup-python@v3
|
uses: actions/setup-python@v3
|
||||||
with:
|
with:
|
||||||
python-version: "3.10"
|
python-version: "3.10"
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||||
- name: Update Wiki
|
- name: Update Wiki
|
||||||
env:
|
env:
|
||||||
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }}
|
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }}
|
||||||
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
|
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
|
||||||
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
|
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
|
||||||
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
|
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
|
||||||
GIST_ID: ${{ secrets.GIST_ID }}
|
GIST_ID: ${{ secrets.GIST_ID }}
|
||||||
GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
|
GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
cd community-threads
|
cd community-threads
|
||||||
python main.py
|
python main.py ${{ github.event.inputs.post_url }}
|
@ -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://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://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://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>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<!-- markdownlint-restore -->
|
<!-- markdownlint-restore -->
|
||||||
|
@ -51,14 +51,14 @@ def main():
|
|||||||
if question_text != "[deleted]":
|
if question_text != "[deleted]":
|
||||||
question_link = "https://reddit.com" + comment.parent().permalink
|
question_link = "https://reddit.com" + comment.parent().permalink
|
||||||
markdown_file += (
|
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
|
question_number += 1
|
||||||
|
|
||||||
with open("questions.md", "w", encoding="utf-8") as file:
|
with open("questions.md", "w", encoding="utf-8") as file:
|
||||||
file.write(markdown_file)
|
file.write(markdown_file)
|
||||||
|
|
||||||
print(f"{question_number} questions generated successfully.")
|
print(f"{question_number - 1} questions generated successfully.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -152,7 +152,7 @@ The collection is curated by our volunteer team & is independent of the number o
|
|||||||
print("No announcements found. Skipping")
|
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:
|
||||||
text += f"| [**{post.title}**]({post.url}) |\n"
|
text += f"| [**{post.title}**]({post.url}) |\n"
|
||||||
else:
|
else:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import praw
|
import praw
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
@ -36,13 +37,26 @@ def update_gist(gist_id, filename, content, description=""):
|
|||||||
)
|
)
|
||||||
return response.json()
|
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):
|
def get_post_data(reddit, post_url):
|
||||||
collection = reddit.subreddit(sub).collections(
|
submission = reddit.submission(url=post_url)
|
||||||
permalink="https://reddit.com/r/developersIndia/collection/958aef35-f9cb-414d-ab33-08bc639e47de"
|
post = {
|
||||||
)
|
"title": submission.title,
|
||||||
return collection
|
"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):
|
def update_wiki(reddit, wikipage, posts):
|
||||||
# Group posts by year
|
# 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"""
|
wiki_header = """# A collection of must read discussions started by community members"""
|
||||||
content = wiki_header + "\n\n"
|
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"
|
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):
|
for year in sorted(posts_by_year.keys(), reverse=True):
|
||||||
@ -78,6 +92,10 @@ def update_wiki(reddit, wikipage, posts):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
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(
|
reddit = praw.Reddit(
|
||||||
client_id=client_id,
|
client_id=client_id,
|
||||||
client_secret=client_secret,
|
client_secret=client_secret,
|
||||||
@ -86,70 +104,30 @@ def main():
|
|||||||
user_agent=f"Automod reader by u/{username}",
|
user_agent=f"Automod reader by u/{username}",
|
||||||
)
|
)
|
||||||
|
|
||||||
collection = get_collection(reddit)
|
|
||||||
|
|
||||||
saved_collection_posts = json.loads(get_gist_content(gist_id))
|
saved_collection_posts = json.loads(get_gist_content(gist_id))
|
||||||
saved_collection_ids = [post["id"] for post in saved_collection_posts["posts"]]
|
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"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 (
|
posts = []
|
||||||
saved_collection_posts["collection_last_updated"]
|
for submission_id in saved_collection_posts["posts"]:
|
||||||
!= datetime.utcfromtimestamp(collection.last_update_utc).isoformat()
|
post = {
|
||||||
):
|
"title": submission_id["title"],
|
||||||
print("Collection was updated, getting new posts data...")
|
"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
|
new_post = get_post_data(reddit, args.post_url)
|
||||||
db_posts = set(saved_collection_ids)
|
if new_post["id"] not in saved_collection_ids:
|
||||||
collection_posts = []
|
posts.append(new_post)
|
||||||
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
|
|
||||||
posts = sorted(posts, key=lambda k: k["created_at"])
|
posts = sorted(posts, key=lambda k: k["created_at"])
|
||||||
|
|
||||||
collection_json = {
|
collection_json = {
|
||||||
"collection_last_updated": datetime.utcfromtimestamp(
|
"collection_last_updated": datetime.utcnow().isoformat(),
|
||||||
collection.last_update_utc
|
|
||||||
).isoformat(),
|
|
||||||
"posts": posts,
|
"posts": posts,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +135,7 @@ def main():
|
|||||||
print("Internal database updated successfully!")
|
print("Internal database updated successfully!")
|
||||||
update_wiki(reddit, "community-threads", posts)
|
update_wiki(reddit, "community-threads", posts)
|
||||||
else:
|
else:
|
||||||
print("Wiki is up to date!")
|
print("Post is already in the collection. No changes were made.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import praw
|
import praw
|
||||||
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
client_id = os.environ["REDDIT_CLIENT_ID"]
|
client_id = os.environ["REDDIT_CLIENT_ID"]
|
||||||
@ -19,7 +20,7 @@ def get_last_activity_times(reddit, username):
|
|||||||
limit=100
|
limit=100
|
||||||
): # look at the user's 100 most recent comments
|
): # look at the user's 100 most recent comments
|
||||||
if comment.subreddit.display_name == sub:
|
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
|
break
|
||||||
|
|
||||||
# Get the user's last post creation time and title in the subreddit
|
# 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
|
limit=100
|
||||||
): # look at the user's 100 most recent posts
|
): # look at the user's 100 most recent posts
|
||||||
if submission.subreddit.display_name == sub:
|
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
|
last_post_title = submission.title
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -45,13 +46,25 @@ def get_current_flair(reddit, username):
|
|||||||
|
|
||||||
return flair["flair_text"], template["id"]
|
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):
|
def assign_user_flair(reddit, username, flair_text):
|
||||||
subreddit = reddit.subreddit(sub)
|
subreddit = reddit.subreddit(sub)
|
||||||
flair = next(subreddit.flair(username))
|
flair = next(subreddit.flair(username))
|
||||||
|
|
||||||
template = get_flair_template_from_text(reddit, flair["flair_text"])
|
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):
|
def get_flair_templates(reddit):
|
||||||
@ -66,6 +79,14 @@ def get_flair_template_from_text(reddit, flair_text):
|
|||||||
return template
|
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):
|
def send_message(reddit, username, flair_text):
|
||||||
message_subject = 'Woohoo! You are now a verified member of r/developersIndia! 🚀'
|
message_subject = 'Woohoo! You are now a verified member of r/developersIndia! 🚀'
|
||||||
message_text = """
|
message_text = """
|
||||||
@ -117,8 +138,14 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
# get current flair
|
# get current flair
|
||||||
current_flair_text, current_flair_template_id = get_current_flair(reddit, reddit_username)
|
current_flair_text, current_flair_template_id = get_flair(reddit, reddit_username)
|
||||||
if current_flair_text is None:
|
|
||||||
|
# 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")
|
print(f"{reddit_username} does not have a flair on r/developersIndia")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
@ -131,8 +158,11 @@ def main():
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
assign_user_flair(reddit, reddit_username, flair_text)
|
assign_user_flair(reddit, reddit_username, flair_text)
|
||||||
print(f"Updated {reddit_username}'s flair to \"{flair_text}\"")
|
# Ya I know, just don't ask
|
||||||
send_message(reddit, reddit_username, flair_text)
|
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}")
|
print(f"Sent verification confirmation message to {reddit_username}")
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user