Skip to content

Commit 0e00909

Browse files
committed
v1
1 parent dcbe203 commit 0e00909

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# build output
2+
dist/
3+
4+
# generated types
5+
.astro/
6+
7+
# dependencies
8+
node_modules/
9+
10+
# logs
11+
npm-debug.log*
12+
yarn-debug.log*
13+
yarn-error.log*
14+
pnpm-debug.log*
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# reddit-wholesome-counter
2+
23
An wholesome counter for Reddit

script.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import praw
2+
import os
3+
from dotenv import load_dotenv
4+
import requests
5+
import re
6+
import collections
7+
import time
8+
9+
# Open the text file containing the words
10+
with open('wholesomewords.txt', 'r') as f:
11+
# Initialize an empty set
12+
wholesome_set = set()
13+
14+
# Read the file line by line
15+
for line in f:
16+
# Split the line into words and add them to the set
17+
words = line.strip().split()
18+
wholesome_set.update(words)
19+
20+
load_dotenv()
21+
22+
# Authenticate with Reddit API
23+
reddit = praw.Reddit(
24+
client_id=os.environ['REDDIT_CLIENT_ID'],
25+
client_secret=os.environ['REDDIT_CLIENT_SECRET'],
26+
username=os.environ['REDDIT_USERNAME'],
27+
password=os.environ['REDDIT_PASSWORD'],
28+
user_agent=os.environ['REDDIT_USER_AGENT']
29+
)
30+
31+
# Cache dictionary for API responses
32+
cache = {}
33+
34+
# Subreddit and trigger phrase
35+
subreddit_name = 'flatapartmentcheck'
36+
trigger_phrase = '!wholesomenesscheck'
37+
38+
# Bot username
39+
bot_username = 'wholesome-counter'
40+
41+
# Read times_called from file or set to 0 if file is empty or doesn't exist
42+
try:
43+
with open('times_called.txt', 'r') as f:
44+
times_called = int(f.read())
45+
except (ValueError, FileNotFoundError):
46+
times_called = 0
47+
48+
# Listen for comments in subreddit
49+
for incoming_comment in reddit.subreddit(subreddit_name).stream.comments(skip_existing=True):
50+
# Check if comment contains trigger phrase
51+
if trigger_phrase in incoming_comment.body:
52+
# Get parent comment and author name
53+
parent_comment = incoming_comment.parent()
54+
user_name = parent_comment.author.name
55+
56+
# Check if bot is being called
57+
if user_name.lower() == bot_username.lower():
58+
# Construct reply text with total times bot was called
59+
reply_text = f'The wholesome counter bot has been called {times_called+1} times till date.'
60+
61+
# Reply to comment
62+
incoming_comment.reply(reply_text)
63+
64+
else:
65+
# Get top 1,000 comments of user
66+
# Check if cached response exists for user
67+
# 604800 seconds = 1 week
68+
if user_name in cache and 'timestamp' in cache[user_name] and time.time() - cache[user_name]['timestamp'] <= 604800:
69+
api_comments = cache[user_name]['response']
70+
else:
71+
# Make API request for user comments
72+
url = f'https://api.pushshift.io/reddit/comment/search?html_decode=true&after=0&author={user_name}&size=500'
73+
with requests.Session() as session:
74+
response = session.get(url)
75+
api_comments = response.json()['data']
76+
77+
# Store response in cache
78+
cache[user_name] = {
79+
'response': api_comments,
80+
'timestamp': time.time()
81+
}
82+
83+
# Initialize wholesome count
84+
wholesome_count = 0
85+
word_count = collections.Counter()
86+
87+
# Analyze comments for wholesome words
88+
for comment in api_comments:
89+
# Clean and tokenize comment
90+
comment_text = re.sub(r'[^\w\s]', '', comment['body']).lower()
91+
comment_tokens = comment_text.split()
92+
93+
# Count wholesome occurrences
94+
for token in comment_tokens:
95+
if token in wholesome_set:
96+
wholesome_count += 1
97+
word_count[token] += 1
98+
99+
# Construct table of wholesome words and counts
100+
table_rows = [
101+
f"| {word} | {count} |" for word, count in word_count.items()
102+
]
103+
table = "\n".join(table_rows)
104+
105+
# Construct reply text with wholesome count and table
106+
reply_text = f'The number of wholesome occurrences in the recent 500 comments of u/{user_name} is {wholesome_count}.\n\n| Word | Count |\n| --- | --- |\n{table} |'
107+
108+
# Reply to comment
109+
incoming_comment.reply(reply_text)
110+
111+
# Increment times_called counter and write to file
112+
times_called += 1
113+
with open('times_called.txt', 'w') as f:
114+
f.write(str(times_called))

times_called.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

wholesomewords.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
uwu
2+
cute
3+
aww
4+
thanks
5+
np
6+
noproblem
7+
love
8+
compassion
9+
hug

0 commit comments

Comments
 (0)